Make the HTML xpath tests generator tests.

This commit is contained in:
Georg Brandl
2009-02-19 22:12:47 +01:00
parent 4c81b055c8
commit 8bf6fa5a74
2 changed files with 41 additions and 20 deletions

View File

@@ -138,7 +138,26 @@ class NslessParser(ET.XMLParser):
return name
@with_app(buildername='html', warning=html_warnfile, tags=['testtag'])
def check_xpath(etree, fname, path, check):
nodes = list(etree.findall(path))
assert nodes != [], ('did not find any node matching xpath '
'%r in file %s' % (path, fname))
if hasattr(check, '__call__'):
check(nodes)
elif not check:
# only check for node presence
pass
else:
rex = re.compile(check)
for node in nodes:
if node.text and rex.search(node.text):
break
else:
assert False, ('%r not found in any node matching '
'path %s in %s: %r' % (check, path, fname,
[node.text for node in nodes]))
@gen_with_app(buildername='html', warning=html_warnfile, tags=['testtag'])
def test_html(app):
app.builder.build_all()
html_warnings = html_warnfile.getvalue().replace(os.sep, '/')
@@ -152,23 +171,7 @@ def test_html(app):
parser.entity.update(htmlentitydefs.entitydefs)
etree = ET.parse(os.path.join(app.outdir, fname), parser)
for path, check in paths.iteritems():
nodes = list(etree.findall(path))
assert nodes != [], ('did not find any node matching xpath '
'%r in file %s' % (path, fname))
if hasattr(check, '__call__'):
check(nodes)
elif not check:
# only check for node presence
continue
else:
rex = re.compile(check)
for node in nodes:
if node.text and rex.search(node.text):
break
else:
assert False, ('%r not found in any node matching '
'path %s in %s: %r' % (check, path, fname,
[node.text for node in nodes]))
yield check_xpath, etree, fname, path, check
@with_app(buildername='latex', warning=latex_warnfile)

View File

@@ -30,7 +30,7 @@ from nose import tools
__all__ = [
'test_root',
'raises', 'raises_msg', 'Struct',
'ListOutput', 'TestApp', 'with_app',
'ListOutput', 'TestApp', 'with_app', 'gen_with_app',
'path', 'with_tempdir', 'write_file',
'sprint',
]
@@ -160,7 +160,25 @@ def with_app(*args, **kwargs):
def deco(*args2, **kwargs2):
app = TestApp(*args, **kwargs)
try:
func(app, *args2, **kwargs2)
return func(app, *args2, **kwargs2)
finally:
app.cleanup()
return deco
return generator
def gen_with_app(*args, **kwargs):
"""
Make a TestApp with args and kwargs, pass it to the test and clean up
properly.
"""
def generator(func):
@wraps(func)
def deco(*args2, **kwargs2):
app = TestApp(*args, **kwargs)
try:
for item in func(app, *args2, **kwargs2):
yield item
finally:
app.cleanup()
return deco