Skip to content

Commit

Permalink
consolidate differences in lib/html_unit_test.py
Browse files Browse the repository at this point in the history
see Freecell Solver site sources and freenode programming FAQ
  • Loading branch information
shlomif committed Jul 25, 2024
1 parent 4bb9c57 commit 3b17102
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Tests/fortunes-show-cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]"
Expand Down
36 changes: 30 additions & 6 deletions Tests/lib/html_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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_"""
Expand All @@ -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):
Expand Down

0 comments on commit 3b17102

Please sign in to comment.