Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9337b00

Browse files
committed
Use py.test to generate tests from the data files themselves.
1 parent9a10a4c commit9337b00

File tree

6 files changed

+121
-103
lines changed

6 files changed

+121
-103
lines changed

‎.pytest.expect‎

14.1 KB
Binary file not shown.

‎html5lib/tests/conftest.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
importos.path
2+
3+
from .tree_constructionimportTreeConstructionFile
4+
5+
_dir=os.path.abspath(os.path.dirname(__file__))
6+
_testdata=os.path.join(_dir,"testdata")
7+
_tree_construction=os.path.join(_testdata,"tree-construction")
8+
9+
10+
defpytest_collectstart():
11+
"""check to see if the git submodule has been init'd"""
12+
pass
13+
14+
15+
defpytest_collect_file(path,parent):
16+
dir=os.path.abspath(path.dirname)
17+
ifdir==_tree_construction:
18+
ifpath.basename=="template.dat":
19+
return
20+
ifpath.ext==".dat":
21+
returnTreeConstructionFile(path,parent)

‎html5lib/tests/support.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@
2727
try:
2828
importxml.etree.cElementTreeascElementTree
2929
exceptImportError:
30-
pass
30+
treeTypes['cElementTree']=None
3131
else:
3232
# On Python 3.3 and above cElementTree is an alias, don't run them twice.
33-
ifcElementTree.ElementisnotElementTree.Element:
33+
ifcElementTree.ElementisElementTree.Element:
34+
treeTypes['cElementTree']=None
35+
else:
3436
treeTypes['cElementTree']=treebuilders.getTreeBuilder("etree",cElementTree,fullTree=True)
3537

3638
try:
3739
importlxml.etreeaslxml# flake8: noqa
3840
exceptImportError:
39-
pass
41+
treeTypes['lxml']=None
4042
else:
4143
treeTypes['lxml']=treebuilders.getTreeBuilder("lxml")
4244

@@ -63,9 +65,6 @@ def __init__(self, filename, newTestHeading="data", encoding="utf8"):
6365
self.encoding=encoding
6466
self.newTestHeading=newTestHeading
6567

66-
def__del__(self):
67-
self.f.close()
68-
6968
def__iter__(self):
7069
data=DefaultDict(None)
7170
key=None

‎html5lib/tests/test_parser.py‎

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from __future__importabsolute_import,division,unicode_literals
2+
3+
importwarnings
4+
importre
5+
6+
importpytest
7+
8+
from .supportimportTestData,convert,convertExpected,treeTypes
9+
fromhtml5libimporthtml5parser,constants
10+
11+
12+
classTreeConstructionFile(pytest.File):
13+
defcollect(self):
14+
tests=TestData(str(self.fspath),"data")
15+
fori,testinenumerate(tests):
16+
fortreeName,treeClassinsorted(treeTypes.items()):
17+
fornamespaceHTMLElementsin (True,False):
18+
ifnamespaceHTMLElements:
19+
nodeid="%d::%s::namespaced"% (i,treeName)
20+
else:
21+
nodeid="%d::%s::void-namespace"% (i,treeName)
22+
item=ParserTest(nodeid,self,
23+
test,treeClass,namespaceHTMLElements)
24+
item.add_marker(getattr(pytest.mark,treeName))
25+
ifnamespaceHTMLElements:
26+
item.add_marker(pytest.mark.namespaced)
27+
iftreeClassisNone:
28+
item.add_marker(pytest.mark.skipif(True,reason="Treebuilder not loaded"))
29+
yielditem
30+
31+
32+
defconvertTreeDump(data):
33+
return"\n".join(convert(3)(data).split("\n")[1:])
34+
35+
namespaceExpected=re.compile(r"^(\s*)<(\S+)>",re.M).sub
36+
37+
38+
classParserTest(pytest.Item):
39+
def__init__(self,name,parent,test,treeClass,namespaceHTMLElements):
40+
super(ParserTest,self).__init__(name,parent)
41+
self.obj=lambda:1# this is to hack around skipif needing a function!
42+
self.test=test
43+
self.treeClass=treeClass
44+
self.namespaceHTMLElements=namespaceHTMLElements
45+
46+
defruntest(self):
47+
p=html5parser.HTMLParser(tree=self.treeClass,
48+
namespaceHTMLElements=self.namespaceHTMLElements)
49+
50+
input=self.test['data']
51+
fragmentContainer=self.test['document-fragment']
52+
expected=self.test['document']
53+
expectedErrors=self.test['errors'].split("\n")ifself.test['errors']else []
54+
55+
withwarnings.catch_warnings():
56+
warnings.simplefilter("error")
57+
try:
58+
iffragmentContainer:
59+
document=p.parseFragment(input,fragmentContainer)
60+
else:
61+
document=p.parse(input)
62+
exceptconstants.DataLossWarning:
63+
pytest.skip("data loss warning")
64+
65+
output=convertTreeDump(p.tree.testSerializer(document))
66+
67+
expected=convertExpected(expected)
68+
ifself.namespaceHTMLElements:
69+
expected=namespaceExpected(r"\1<html \2>",expected)
70+
71+
errorMsg="\n".join(["\n\nInput:",input,"\nExpected:",expected,
72+
"\nReceived:",output])
73+
assertexpected==output,errorMsg
74+
75+
errStr= []
76+
for (line,col),errorcode,datavarsinp.errors:
77+
assertisinstance(datavars,dict),"%s, %s"% (errorcode,repr(datavars))
78+
errStr.append("Line: %i Col: %i %s"% (line,col,
79+
constants.E[errorcode]%datavars))
80+
81+
errorMsg2="\n".join(["\n\nInput:",input,
82+
"\nExpected errors ("+str(len(expectedErrors))+"):\n"+"\n".join(expectedErrors),
83+
"\nActual errors ("+str(len(p.errors))+"):\n"+"\n".join(errStr)])
84+
ifFalse:# we're currently not testing parse errors
85+
assertlen(p.errors)==len(expectedErrors),errorMsg2
86+
87+
defrepr_failure(self,excinfo):
88+
traceback=excinfo.traceback
89+
ntraceback=traceback.cut(path=__file__)
90+
excinfo.traceback=ntraceback.filter()
91+
92+
returnexcinfo.getrepr(funcargs=True,
93+
showlocals=False,
94+
style="short",tbfilter=False)

‎pytest.ini‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
addopts = -rXw -p no:doctest
2+
addopts = -rXw -p no:doctest

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp