From 3b171020c3a61d9dce4d8f0422adaf14c2b142af Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 25 Jul 2024 08:52:32 +0300 Subject: [PATCH] consolidate differences in lib/html_unit_test.py see Freecell Solver site sources and freenode programming FAQ --- Tests/fortunes-show-cgi.py | 2 +- Tests/lib/html_unit_test.py | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Tests/fortunes-show-cgi.py b/Tests/fortunes-show-cgi.py index f89b46a9f..2eaabd6c6 100755 --- a/Tests/fortunes-show-cgi.py +++ b/Tests/fortunes-show-cgi.py @@ -92,7 +92,7 @@ def test_raw_mode(self): 'type': 'text', 'fn': 'test_raw_mode', 'text': resp.text.encode('utf8'), - }) + }, filetype='html') doc.has_one( "//html/body/div[@class='fortune']/blockquote/" + "p[contains(text(), 'recorded')]" diff --git a/Tests/lib/html_unit_test.py b/Tests/lib/html_unit_test.py index b66209f3f..47405a5eb 100644 --- a/Tests/lib/html_unit_test.py +++ b/Tests/lib/html_unit_test.py @@ -14,7 +14,20 @@ from os.path import join import unittest +from lxml import etree from lxml import html +from lxml.html import XHTML_NAMESPACE + + +XML_NS = "http://www.w3.org/XML/1998/namespace" +dbns = "http://docbook.org/ns/docbook" +ns = { + "db": dbns, + "ncx": "http://www.daisy.org/z3986/2005/ncx/", + "xhtml": XHTML_NAMESPACE, + "xlink": "http://www.w3.org/1999/xlink", + "xml": XML_NS, +} class HtmlTestsDocQuery: @@ -30,18 +43,29 @@ def __len__(self): class HtmlTestsDoc: """A single HTML document wrapper""" - def __init__(self, harness, fn): + def __init__(self, harness, fn, filetype='html'): + self.filetype = filetype self.harness = harness if isinstance(fn, dict): assert fn['type'] == 'text' self.fn = fn['fn'] - self.root = html.document_fromstring(html=fn['text']) + if filetype == 'html': + self.root = html.document_fromstring(html=fn['text']) + elif filetype == 'docbook5': + assert False return self.fn = fn - self.root = html.parse(fn) + if filetype == 'html': + self.root = html.parse(fn) + elif filetype == 'docbook5': + self.root = etree.parse(fn) def xpath(self, xpath_s): - return HtmlTestsDocQuery(self, self.root.xpath(xpath_s)) + if self.filetype == 'html': + return HtmlTestsDocQuery(self, self.root.xpath(xpath_s)) + elif self.filetype == 'docbook5': + return HtmlTestsDocQuery(self, self.root.xpath( + xpath_s, namespaces=ns)) def has_count(self, xpath_s, count_, blurb=""): """is the length of xpath_s’s results count_""" @@ -57,8 +81,8 @@ def has_none(self, xpath_s, blurb=""): class TestCase(unittest.TestCase): - def doc(self, path): - return HtmlTestsDoc(self, path) + def doc(self, path, filetype='html'): + return HtmlTestsDoc(self, path, filetype=filetype) def _find_htmls(root):