|
1 | 1 | from __future__importabsolute_import,division,unicode_literals |
2 | 2 |
|
3 | 3 | importitertools |
| 4 | +importsys |
4 | 5 |
|
| 6 | +fromsiximportunichr,text_type |
5 | 7 | importpytest |
6 | 8 |
|
7 | 9 | try: |
@@ -135,3 +137,65 @@ def test_lxml_xml(): |
135 | 137 | output=Lint(walker(lxmltree)) |
136 | 138 |
|
137 | 139 | assertlist(output)==expected |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.parametrize("treeName", |
| 143 | + [pytest.param(treeName,marks=[getattr(pytest.mark,treeName), |
| 144 | +pytest.mark.skipif(sys.version_info< (3,7),reason="dict order undef")]) |
| 145 | +fortreeNameinsorted(treeTypes.keys())]) |
| 146 | +deftest_maintain_attribute_order(treeName): |
| 147 | +treeAPIs=treeTypes[treeName] |
| 148 | +iftreeAPIsisNone: |
| 149 | +pytest.skip("Treebuilder not loaded") |
| 150 | + |
| 151 | +# generate loads to maximize the chance a hash-based mutation will occur |
| 152 | +attrs= [(unichr(x),text_type(i))fori,xinenumerate(range(ord('a'),ord('z')))] |
| 153 | +data="<span "+" ".join("%s='%s'"% (x,i)forx,iinattrs)+">" |
| 154 | + |
| 155 | +parser=html5parser.HTMLParser(tree=treeAPIs["builder"]) |
| 156 | +document=parser.parseFragment(data) |
| 157 | + |
| 158 | +document=treeAPIs.get("adapter",lambdax:x)(document) |
| 159 | +output=list(Lint(treeAPIs["walker"](document))) |
| 160 | + |
| 161 | +assertlen(output)==2 |
| 162 | +assertoutput[0]['type']=='StartTag' |
| 163 | +assertoutput[1]['type']=="EndTag" |
| 164 | + |
| 165 | +attrs_out=output[0]['data'] |
| 166 | +assertlen(attrs)==len(attrs_out) |
| 167 | + |
| 168 | +for (in_name,in_value), (out_name,out_value)inzip(attrs,attrs_out.items()): |
| 169 | +assert (None,in_name)==out_name |
| 170 | +assertin_value==out_value |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.parametrize("treeName", |
| 174 | + [pytest.param(treeName,marks=[getattr(pytest.mark,treeName), |
| 175 | +pytest.mark.skipif(sys.version_info< (3,7),reason="dict order undef")]) |
| 176 | +fortreeNameinsorted(treeTypes.keys())]) |
| 177 | +deftest_maintain_attribute_order_adjusted(treeName): |
| 178 | +treeAPIs=treeTypes[treeName] |
| 179 | +iftreeAPIsisNone: |
| 180 | +pytest.skip("Treebuilder not loaded") |
| 181 | + |
| 182 | +# generate loads to maximize the chance a hash-based mutation will occur |
| 183 | +data="<svg a=1 refx=2 b=3 xml:lang=4 c=5>" |
| 184 | + |
| 185 | +parser=html5parser.HTMLParser(tree=treeAPIs["builder"]) |
| 186 | +document=parser.parseFragment(data) |
| 187 | + |
| 188 | +document=treeAPIs.get("adapter",lambdax:x)(document) |
| 189 | +output=list(Lint(treeAPIs["walker"](document))) |
| 190 | + |
| 191 | +assertlen(output)==2 |
| 192 | +assertoutput[0]['type']=='StartTag' |
| 193 | +assertoutput[1]['type']=="EndTag" |
| 194 | + |
| 195 | +attrs_out=output[0]['data'] |
| 196 | + |
| 197 | +assertlist(attrs_out.items())== [((None,'a'),'1'), |
| 198 | + ((None,'refX'),'2'), |
| 199 | + ((None,'b'),'3'), |
| 200 | + (('http://www.w3.org/XML/1998/namespace','lang'),'4'), |
| 201 | + ((None,'c'),'5')] |