|
1 | 1 | from __future__ import absolute_import, division, unicode_literals
|
2 | 2 |
|
3 |
| -import unittest |
4 |
| - |
5 | 3 | from html5lib.filters.whitespace import Filter
|
6 | 4 | from html5lib.constants import spaceCharacters
|
7 | 5 | spaceCharacters = "".join(spaceCharacters)
|
8 | 6 |
|
9 |
| -try: |
10 |
| - unittest.TestCase.assertEqual |
11 |
| -except AttributeError: |
12 |
| - unittest.TestCase.assertEqual = unittest.TestCase.assertEquals |
13 |
| - |
14 |
| - |
15 |
| -class TestCase(unittest.TestCase): |
16 |
| - def runTest(self, input, expected): |
17 |
| - output = list(Filter(input)) |
18 |
| - errorMsg = "\n".join(["\n\nInput:", str(input), |
19 |
| - "\nExpected:", str(expected), |
20 |
| - "\nReceived:", str(output)]) |
21 |
| - self.assertEqual(output, expected, errorMsg) |
22 |
| - |
23 |
| - def runTestUnmodifiedOutput(self, input): |
24 |
| - self.runTest(input, input) |
25 |
| - |
26 |
| - def testPhrasingElements(self): |
27 |
| - self.runTestUnmodifiedOutput( |
28 |
| - [{"type": "Characters", "data": "This is a "}, |
29 |
| - {"type": "StartTag", "name": "span", "data": []}, |
30 |
| - {"type": "Characters", "data": "phrase"}, |
31 |
| - {"type": "EndTag", "name": "span", "data": []}, |
32 |
| - {"type": "SpaceCharacters", "data": " "}, |
33 |
| - {"type": "Characters", "data": "with"}, |
34 |
| - {"type": "SpaceCharacters", "data": " "}, |
35 |
| - {"type": "StartTag", "name": "em", "data": []}, |
36 |
| - {"type": "Characters", "data": "emphasised text"}, |
37 |
| - {"type": "EndTag", "name": "em", "data": []}, |
38 |
| - {"type": "Characters", "data": " and an "}, |
39 |
| - {"type": "StartTag", "name": "img", "data": [["alt", "image"]]}, |
40 |
| - {"type": "Characters", "data": "."}]) |
41 |
| - |
42 |
| - def testLeadingWhitespace(self): |
43 |
| - self.runTest( |
44 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
45 |
| - {"type": "SpaceCharacters", "data": spaceCharacters}, |
46 |
| - {"type": "Characters", "data": "foo"}, |
47 |
| - {"type": "EndTag", "name": "p", "data": []}], |
48 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
49 |
| - {"type": "SpaceCharacters", "data": " "}, |
50 |
| - {"type": "Characters", "data": "foo"}, |
51 |
| - {"type": "EndTag", "name": "p", "data": []}]) |
52 |
| - |
53 |
| - def testLeadingWhitespaceAsCharacters(self): |
54 |
| - self.runTest( |
55 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
56 |
| - {"type": "Characters", "data": spaceCharacters + "foo"}, |
57 |
| - {"type": "EndTag", "name": "p", "data": []}], |
58 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
59 |
| - {"type": "Characters", "data": " foo"}, |
60 |
| - {"type": "EndTag", "name": "p", "data": []}]) |
61 |
| - |
62 |
| - def testTrailingWhitespace(self): |
63 |
| - self.runTest( |
64 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
65 |
| - {"type": "Characters", "data": "foo"}, |
66 |
| - {"type": "SpaceCharacters", "data": spaceCharacters}, |
67 |
| - {"type": "EndTag", "name": "p", "data": []}], |
68 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
69 |
| - {"type": "Characters", "data": "foo"}, |
70 |
| - {"type": "SpaceCharacters", "data": " "}, |
71 |
| - {"type": "EndTag", "name": "p", "data": []}]) |
72 |
| - |
73 |
| - def testTrailingWhitespaceAsCharacters(self): |
74 |
| - self.runTest( |
75 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
76 |
| - {"type": "Characters", "data": "foo" + spaceCharacters}, |
77 |
| - {"type": "EndTag", "name": "p", "data": []}], |
78 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
79 |
| - {"type": "Characters", "data": "foo "}, |
80 |
| - {"type": "EndTag", "name": "p", "data": []}]) |
81 |
| - |
82 |
| - def testWhitespace(self): |
83 |
| - self.runTest( |
84 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
85 |
| - {"type": "Characters", "data": "foo" + spaceCharacters + "bar"}, |
86 |
| - {"type": "EndTag", "name": "p", "data": []}], |
87 |
| - [{"type": "StartTag", "name": "p", "data": []}, |
88 |
| - {"type": "Characters", "data": "foo bar"}, |
89 |
| - {"type": "EndTag", "name": "p", "data": []}]) |
90 |
| - |
91 |
| - def testLeadingWhitespaceInPre(self): |
92 |
| - self.runTestUnmodifiedOutput( |
93 |
| - [{"type": "StartTag", "name": "pre", "data": []}, |
94 |
| - {"type": "SpaceCharacters", "data": spaceCharacters}, |
95 |
| - {"type": "Characters", "data": "foo"}, |
96 |
| - {"type": "EndTag", "name": "pre", "data": []}]) |
97 |
| - |
98 |
| - def testLeadingWhitespaceAsCharactersInPre(self): |
99 |
| - self.runTestUnmodifiedOutput( |
100 |
| - [{"type": "StartTag", "name": "pre", "data": []}, |
101 |
| - {"type": "Characters", "data": spaceCharacters + "foo"}, |
102 |
| - {"type": "EndTag", "name": "pre", "data": []}]) |
103 |
| - |
104 |
| - def testTrailingWhitespaceInPre(self): |
105 |
| - self.runTestUnmodifiedOutput( |
106 |
| - [{"type": "StartTag", "name": "pre", "data": []}, |
107 |
| - {"type": "Characters", "data": "foo"}, |
108 |
| - {"type": "SpaceCharacters", "data": spaceCharacters}, |
109 |
| - {"type": "EndTag", "name": "pre", "data": []}]) |
110 |
| - |
111 |
| - def testTrailingWhitespaceAsCharactersInPre(self): |
112 |
| - self.runTestUnmodifiedOutput( |
113 |
| - [{"type": "StartTag", "name": "pre", "data": []}, |
114 |
| - {"type": "Characters", "data": "foo" + spaceCharacters}, |
115 |
| - {"type": "EndTag", "name": "pre", "data": []}]) |
116 |
| - |
117 |
| - def testWhitespaceInPre(self): |
118 |
| - self.runTestUnmodifiedOutput( |
119 |
| - [{"type": "StartTag", "name": "pre", "data": []}, |
120 |
| - {"type": "Characters", "data": "foo" + spaceCharacters + "bar"}, |
121 |
| - {"type": "EndTag", "name": "pre", "data": []}]) |
122 |
| - |
123 |
| - |
124 |
| -def buildTestSuite(): |
125 |
| - return unittest.defaultTestLoader.loadTestsFromName(__name__) |
126 |
| - |
127 |
| - |
128 |
| -def main(): |
129 |
| - buildTestSuite() |
130 |
| - unittest.main() |
131 |
| - |
132 |
| -if __name__ == "__main__": |
133 |
| - main() |
| 7 | + |
| 8 | +def runTest(input, expected): |
| 9 | + output = list(Filter(input)) |
| 10 | + errorMsg = "\n".join(["\n\nInput:", str(input), |
| 11 | + "\nExpected:", str(expected), |
| 12 | + "\nReceived:", str(output)]) |
| 13 | + assert expected == output, errorMsg |
| 14 | + |
| 15 | + |
| 16 | +def runTestUnmodifiedOutput(input): |
| 17 | + runTest(input, input) |
| 18 | + |
| 19 | + |
| 20 | +def testPhrasingElements(): |
| 21 | + runTestUnmodifiedOutput( |
| 22 | + [{"type": "Characters", "data": "This is a "}, |
| 23 | + {"type": "StartTag", "name": "span", "data": []}, |
| 24 | + {"type": "Characters", "data": "phrase"}, |
| 25 | + {"type": "EndTag", "name": "span", "data": []}, |
| 26 | + {"type": "SpaceCharacters", "data": " "}, |
| 27 | + {"type": "Characters", "data": "with"}, |
| 28 | + {"type": "SpaceCharacters", "data": " "}, |
| 29 | + {"type": "StartTag", "name": "em", "data": []}, |
| 30 | + {"type": "Characters", "data": "emphasised text"}, |
| 31 | + {"type": "EndTag", "name": "em", "data": []}, |
| 32 | + {"type": "Characters", "data": " and an "}, |
| 33 | + {"type": "StartTag", "name": "img", "data": [["alt", "image"]]}, |
| 34 | + {"type": "Characters", "data": "."}]) |
| 35 | + |
| 36 | + |
| 37 | +def testLeadingWhitespace(): |
| 38 | + runTest( |
| 39 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 40 | + {"type": "SpaceCharacters", "data": spaceCharacters}, |
| 41 | + {"type": "Characters", "data": "foo"}, |
| 42 | + {"type": "EndTag", "name": "p", "data": []}], |
| 43 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 44 | + {"type": "SpaceCharacters", "data": " "}, |
| 45 | + {"type": "Characters", "data": "foo"}, |
| 46 | + {"type": "EndTag", "name": "p", "data": []}]) |
| 47 | + |
| 48 | + |
| 49 | +def testLeadingWhitespaceAsCharacters(): |
| 50 | + runTest( |
| 51 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 52 | + {"type": "Characters", "data": spaceCharacters + "foo"}, |
| 53 | + {"type": "EndTag", "name": "p", "data": []}], |
| 54 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 55 | + {"type": "Characters", "data": " foo"}, |
| 56 | + {"type": "EndTag", "name": "p", "data": []}]) |
| 57 | + |
| 58 | + |
| 59 | +def testTrailingWhitespace(): |
| 60 | + runTest( |
| 61 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 62 | + {"type": "Characters", "data": "foo"}, |
| 63 | + {"type": "SpaceCharacters", "data": spaceCharacters}, |
| 64 | + {"type": "EndTag", "name": "p", "data": []}], |
| 65 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 66 | + {"type": "Characters", "data": "foo"}, |
| 67 | + {"type": "SpaceCharacters", "data": " "}, |
| 68 | + {"type": "EndTag", "name": "p", "data": []}]) |
| 69 | + |
| 70 | + |
| 71 | +def testTrailingWhitespaceAsCharacters(): |
| 72 | + runTest( |
| 73 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 74 | + {"type": "Characters", "data": "foo" + spaceCharacters}, |
| 75 | + {"type": "EndTag", "name": "p", "data": []}], |
| 76 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 77 | + {"type": "Characters", "data": "foo "}, |
| 78 | + {"type": "EndTag", "name": "p", "data": []}]) |
| 79 | + |
| 80 | + |
| 81 | +def testWhitespace(): |
| 82 | + runTest( |
| 83 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 84 | + {"type": "Characters", "data": "foo" + spaceCharacters + "bar"}, |
| 85 | + {"type": "EndTag", "name": "p", "data": []}], |
| 86 | + [{"type": "StartTag", "name": "p", "data": []}, |
| 87 | + {"type": "Characters", "data": "foo bar"}, |
| 88 | + {"type": "EndTag", "name": "p", "data": []}]) |
| 89 | + |
| 90 | + |
| 91 | +def testLeadingWhitespaceInPre(): |
| 92 | + runTestUnmodifiedOutput( |
| 93 | + [{"type": "StartTag", "name": "pre", "data": []}, |
| 94 | + {"type": "SpaceCharacters", "data": spaceCharacters}, |
| 95 | + {"type": "Characters", "data": "foo"}, |
| 96 | + {"type": "EndTag", "name": "pre", "data": []}]) |
| 97 | + |
| 98 | + |
| 99 | +def testLeadingWhitespaceAsCharactersInPre(): |
| 100 | + runTestUnmodifiedOutput( |
| 101 | + [{"type": "StartTag", "name": "pre", "data": []}, |
| 102 | + {"type": "Characters", "data": spaceCharacters + "foo"}, |
| 103 | + {"type": "EndTag", "name": "pre", "data": []}]) |
| 104 | + |
| 105 | + |
| 106 | +def testTrailingWhitespaceInPre(): |
| 107 | + runTestUnmodifiedOutput( |
| 108 | + [{"type": "StartTag", "name": "pre", "data": []}, |
| 109 | + {"type": "Characters", "data": "foo"}, |
| 110 | + {"type": "SpaceCharacters", "data": spaceCharacters}, |
| 111 | + {"type": "EndTag", "name": "pre", "data": []}]) |
| 112 | + |
| 113 | + |
| 114 | +def testTrailingWhitespaceAsCharactersInPre(): |
| 115 | + runTestUnmodifiedOutput( |
| 116 | + [{"type": "StartTag", "name": "pre", "data": []}, |
| 117 | + {"type": "Characters", "data": "foo" + spaceCharacters}, |
| 118 | + {"type": "EndTag", "name": "pre", "data": []}]) |
| 119 | + |
| 120 | + |
| 121 | +def testWhitespaceInPre(): |
| 122 | + runTestUnmodifiedOutput( |
| 123 | + [{"type": "StartTag", "name": "pre", "data": []}, |
| 124 | + {"type": "Characters", "data": "foo" + spaceCharacters + "bar"}, |
| 125 | + {"type": "EndTag", "name": "pre", "data": []}]) |
0 commit comments