Skip to content

Commit 5e1d4ff

Browse files
committed
Add a way to assert the number of occurrences to htmldocck
1 parent f7aafcc commit 5e1d4ff

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/etc/htmldocck.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
highlights for example. If you want to simply check the presence of
9797
given node or attribute, use an empty string (`""`) as a `PATTERN`.
9898
99+
* `@count PATH XPATH COUNT' checks for the occurrence of given XPath
100+
in the given file. The number of occurrences must match the given count.
101+
99102
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
100103
checks if the given file does not exist, for example.
101104
@@ -333,6 +336,11 @@ def check_tree_text(tree, path, pat, regexp):
333336
return ret
334337

335338

339+
def check_tree_count(tree, path, count):
340+
path = normalize_xpath(path)
341+
return len(tree.findall(path)) == count
342+
343+
336344
def check(target, commands):
337345
cache = CachedFiles(target)
338346
for c in commands:
@@ -360,6 +368,13 @@ def check(target, commands):
360368
raise RuntimeError('Invalid number of @{} arguments \
361369
at line {}'.format(c.cmd, c.lineno))
362370

371+
elif c.cmd == 'count': # count test
372+
if len(c.args) == 3: # @count <path> <pat> <count> = count test
373+
ret = check_tree_count(cache.get_tree(c.args[0]), c.args[1], int(c.args[2]))
374+
else:
375+
raise RuntimeError('Invalid number of @{} arguments \
376+
at line {}'.format(c.cmd, c.lineno))
377+
363378
elif c.cmd == 'valid-html':
364379
raise RuntimeError('Unimplemented @valid-html at line {}'.format(c.lineno))
365380

0 commit comments

Comments
 (0)