Add a way to assert the number of occurrences to htmldocck
This commit is contained in:
@@ -96,6 +96,9 @@ There are a number of supported commands:
|
|||||||
highlights for example. If you want to simply check the presence of
|
highlights for example. If you want to simply check the presence of
|
||||||
given node or attribute, use an empty string (`""`) as a `PATTERN`.
|
given node or attribute, use an empty string (`""`) as a `PATTERN`.
|
||||||
|
|
||||||
|
* `@count PATH XPATH COUNT' checks for the occurrence of given XPath
|
||||||
|
in the given file. The number of occurrences must match the given count.
|
||||||
|
|
||||||
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
|
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
|
||||||
checks if the given file does not exist, for example.
|
checks if the given file does not exist, for example.
|
||||||
|
|
||||||
@@ -333,6 +336,11 @@ def check_tree_text(tree, path, pat, regexp):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def check_tree_count(tree, path, count):
|
||||||
|
path = normalize_xpath(path)
|
||||||
|
return len(tree.findall(path)) == count
|
||||||
|
|
||||||
|
|
||||||
def check(target, commands):
|
def check(target, commands):
|
||||||
cache = CachedFiles(target)
|
cache = CachedFiles(target)
|
||||||
for c in commands:
|
for c in commands:
|
||||||
@@ -360,6 +368,13 @@ def check(target, commands):
|
|||||||
raise RuntimeError('Invalid number of @{} arguments \
|
raise RuntimeError('Invalid number of @{} arguments \
|
||||||
at line {}'.format(c.cmd, c.lineno))
|
at line {}'.format(c.cmd, c.lineno))
|
||||||
|
|
||||||
|
elif c.cmd == 'count': # count test
|
||||||
|
if len(c.args) == 3: # @count <path> <pat> <count> = count test
|
||||||
|
ret = check_tree_count(cache.get_tree(c.args[0]), c.args[1], int(c.args[2]))
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Invalid number of @{} arguments \
|
||||||
|
at line {}'.format(c.cmd, c.lineno))
|
||||||
|
|
||||||
elif c.cmd == 'valid-html':
|
elif c.cmd == 'valid-html':
|
||||||
raise RuntimeError('Unimplemented @valid-html at line {}'.format(c.lineno))
|
raise RuntimeError('Unimplemented @valid-html at line {}'.format(c.lineno))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user