Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3065630

Browse files
committedMay 20, 2016
Get rid of last remenents of our usage of the stdlib unittest
1 parent 8cb66f2 commit 3065630

File tree

4 files changed

+315
-352
lines changed

4 files changed

+315
-352
lines changed
 

‎html5lib/tests/test_encoding.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from __future__ import absolute_import, division, unicode_literals
22

33
import os
4-
import unittest
5-
6-
try:
7-
unittest.TestCase.assertEqual
8-
except AttributeError:
9-
unittest.TestCase.assertEqual = unittest.TestCase.assertEquals
104

115
from .support import get_data_files, test_dir, errorMessage, TestData as _TestData
126
from html5lib import HTMLParser, inputstream

‎html5lib/tests/test_parser2.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,56 @@
22

33
import io
44

5+
import pytest
6+
57
from . import support # flake8: noqa
68
from html5lib import html5parser
79
from html5lib.constants import namespaces
810
from html5lib import treebuilders
911

10-
import unittest
1112

1213
# tests that aren't autogenerated from text files
14+
@pytest.fixture
15+
def dom_tree():
16+
return treebuilders.getTreeBuilder("dom")
1317

1418

15-
class MoreParserTests(unittest.TestCase):
16-
17-
def setUp(self):
18-
self.dom_tree = treebuilders.getTreeBuilder("dom")
19+
def test_assertDoctypeCloneable(dom_tree):
20+
parser = html5parser.HTMLParser(tree=dom_tree)
21+
doc = parser.parse('<!DOCTYPE HTML>')
22+
assert doc.cloneNode(True) is not None
1923

20-
def test_assertDoctypeCloneable(self):
21-
parser = html5parser.HTMLParser(tree=self.dom_tree)
22-
doc = parser.parse('<!DOCTYPE HTML>')
23-
self.assertTrue(doc.cloneNode(True))
2424

25-
def test_line_counter(self):
26-
# http://groups.google.com/group/html5lib-discuss/browse_frm/thread/f4f00e4a2f26d5c0
27-
parser = html5parser.HTMLParser(tree=self.dom_tree)
28-
parser.parse("<pre>\nx\n&gt;\n</pre>")
25+
def test_line_counter(dom_tree):
26+
# http://groups.google.com/group/html5lib-discuss/browse_frm/thread/f4f00e4a2f26d5c0
27+
parser = html5parser.HTMLParser(tree=dom_tree)
28+
parser.parse("<pre>\nx\n&gt;\n</pre>")
2929

30-
def test_namespace_html_elements_0_dom(self):
31-
parser = html5parser.HTMLParser(tree=self.dom_tree, namespaceHTMLElements=True)
32-
doc = parser.parse("<html></html>")
33-
self.assertTrue(doc.childNodes[0].namespaceURI == namespaces["html"])
3430

35-
def test_namespace_html_elements_1_dom(self):
36-
parser = html5parser.HTMLParser(tree=self.dom_tree, namespaceHTMLElements=False)
37-
doc = parser.parse("<html></html>")
38-
self.assertTrue(doc.childNodes[0].namespaceURI is None)
31+
def test_namespace_html_elements_0_dom(dom_tree):
32+
parser = html5parser.HTMLParser(tree=dom_tree, namespaceHTMLElements=True)
33+
doc = parser.parse("<html></html>")
34+
assert doc.childNodes[0].namespaceURI == namespaces["html"]
3935

40-
def test_namespace_html_elements_0_etree(self):
41-
parser = html5parser.HTMLParser(namespaceHTMLElements=True)
42-
doc = parser.parse("<html></html>")
43-
self.assertTrue(doc.tag == "{%s}html" % (namespaces["html"],))
4436

45-
def test_namespace_html_elements_1_etree(self):
46-
parser = html5parser.HTMLParser(namespaceHTMLElements=False)
47-
doc = parser.parse("<html></html>")
48-
self.assertTrue(doc.tag == "html")
37+
def test_namespace_html_elements_1_dom(dom_tree):
38+
parser = html5parser.HTMLParser(tree=dom_tree, namespaceHTMLElements=False)
39+
doc = parser.parse("<html></html>")
40+
assert doc.childNodes[0].namespaceURI is None
4941

50-
def test_unicode_file(self):
51-
parser = html5parser.HTMLParser()
52-
parser.parse(io.StringIO("a"))
5342

43+
def test_namespace_html_elements_0_etree():
44+
parser = html5parser.HTMLParser(namespaceHTMLElements=True)
45+
doc = parser.parse("<html></html>")
46+
assert doc.tag == "{%s}html" % (namespaces["html"],)
5447

55-
def buildTestSuite():
56-
return unittest.defaultTestLoader.loadTestsFromName(__name__)
5748

49+
def test_namespace_html_elements_1_etree():
50+
parser = html5parser.HTMLParser(namespaceHTMLElements=False)
51+
doc = parser.parse("<html></html>")
52+
assert doc.tag == "html"
5853

59-
def main():
60-
buildTestSuite()
61-
unittest.main()
6254

63-
if __name__ == '__main__':
64-
main()
55+
def test_unicode_file():
56+
parser = html5parser.HTMLParser()
57+
parser.parse(io.StringIO("a"))

‎html5lib/tests/test_stream.py

Lines changed: 164 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import, division, unicode_literals
22

33
from . import support # flake8: noqa
4-
import unittest
54
import codecs
65
from io import BytesIO
76
import socket
@@ -12,66 +11,65 @@
1211
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
1312
HTMLUnicodeInputStream, HTMLBinaryInputStream)
1413

15-
class BufferedStreamTest(unittest.TestCase):
16-
def test_basic(self):
17-
s = b"abc"
18-
fp = BufferedStream(BytesIO(s))
19-
read = fp.read(10)
20-
assert read == s
21-
22-
def test_read_length(self):
23-
fp = BufferedStream(BytesIO(b"abcdef"))
24-
read1 = fp.read(1)
25-
assert read1 == b"a"
26-
read2 = fp.read(2)
27-
assert read2 == b"bc"
28-
read3 = fp.read(3)
29-
assert read3 == b"def"
30-
read4 = fp.read(4)
31-
assert read4 == b""
32-
33-
def test_tell(self):
34-
fp = BufferedStream(BytesIO(b"abcdef"))
35-
read1 = fp.read(1)
36-
assert fp.tell() == 1
37-
read2 = fp.read(2)
38-
assert fp.tell() == 3
39-
read3 = fp.read(3)
40-
assert fp.tell() == 6
41-
read4 = fp.read(4)
42-
assert fp.tell() == 6
43-
44-
def test_seek(self):
45-
fp = BufferedStream(BytesIO(b"abcdef"))
46-
read1 = fp.read(1)
47-
assert read1 == b"a"
48-
fp.seek(0)
49-
read2 = fp.read(1)
50-
assert read2 == b"a"
51-
read3 = fp.read(2)
52-
assert read3 == b"bc"
53-
fp.seek(2)
54-
read4 = fp.read(2)
55-
assert read4 == b"cd"
56-
fp.seek(4)
57-
read5 = fp.read(2)
58-
assert read5 == b"ef"
59-
60-
def test_seek_tell(self):
61-
fp = BufferedStream(BytesIO(b"abcdef"))
62-
read1 = fp.read(1)
63-
assert fp.tell() == 1
64-
fp.seek(0)
65-
read2 = fp.read(1)
66-
assert fp.tell() == 1
67-
read3 = fp.read(2)
68-
assert fp.tell() == 3
69-
fp.seek(2)
70-
read4 = fp.read(2)
71-
assert fp.tell() == 4
72-
fp.seek(4)
73-
read5 = fp.read(2)
74-
assert fp.tell() == 6
14+
def test_basic():
15+
s = b"abc"
16+
fp = BufferedStream(BytesIO(s))
17+
read = fp.read(10)
18+
assert read == s
19+
20+
def test_read_length():
21+
fp = BufferedStream(BytesIO(b"abcdef"))
22+
read1 = fp.read(1)
23+
assert read1 == b"a"
24+
read2 = fp.read(2)
25+
assert read2 == b"bc"
26+
read3 = fp.read(3)
27+
assert read3 == b"def"
28+
read4 = fp.read(4)
29+
assert read4 == b""
30+
31+
def test_tell():
32+
fp = BufferedStream(BytesIO(b"abcdef"))
33+
read1 = fp.read(1)
34+
assert fp.tell() == 1
35+
read2 = fp.read(2)
36+
assert fp.tell() == 3
37+
read3 = fp.read(3)
38+
assert fp.tell() == 6
39+
read4 = fp.read(4)
40+
assert fp.tell() == 6
41+
42+
def test_seek():
43+
fp = BufferedStream(BytesIO(b"abcdef"))
44+
read1 = fp.read(1)
45+
assert read1 == b"a"
46+
fp.seek(0)
47+
read2 = fp.read(1)
48+
assert read2 == b"a"
49+
read3 = fp.read(2)
50+
assert read3 == b"bc"
51+
fp.seek(2)
52+
read4 = fp.read(2)
53+
assert read4 == b"cd"
54+
fp.seek(4)
55+
read5 = fp.read(2)
56+
assert read5 == b"ef"
57+
58+
def test_seek_tell():
59+
fp = BufferedStream(BytesIO(b"abcdef"))
60+
read1 = fp.read(1)
61+
assert fp.tell() == 1
62+
fp.seek(0)
63+
read2 = fp.read(1)
64+
assert fp.tell() == 1
65+
read3 = fp.read(2)
66+
assert fp.tell() == 3
67+
fp.seek(2)
68+
read4 = fp.read(2)
69+
assert fp.tell() == 4
70+
fp.seek(4)
71+
read5 = fp.read(2)
72+
assert fp.tell() == 6
7573

7674

7775
class HTMLUnicodeInputStreamShortChunk(HTMLUnicodeInputStream):
@@ -82,122 +80,108 @@ class HTMLBinaryInputStreamShortChunk(HTMLBinaryInputStream):
8280
_defaultChunkSize = 2
8381

8482

85-
class HTMLInputStreamTest(unittest.TestCase):
86-
87-
def test_char_ascii(self):
88-
stream = HTMLInputStream(b"'", encoding='ascii')
89-
self.assertEqual(stream.charEncoding[0].name, 'windows-1252')
90-
self.assertEqual(stream.char(), "'")
91-
92-
def test_char_utf8(self):
93-
stream = HTMLInputStream('\u2018'.encode('utf-8'), encoding='utf-8')
94-
self.assertEqual(stream.charEncoding[0].name, 'utf-8')
95-
self.assertEqual(stream.char(), '\u2018')
96-
97-
def test_char_win1252(self):
98-
stream = HTMLInputStream("\xa9\xf1\u2019".encode('windows-1252'))
99-
self.assertEqual(stream.charEncoding[0].name, 'windows-1252')
100-
self.assertEqual(stream.char(), "\xa9")
101-
self.assertEqual(stream.char(), "\xf1")
102-
self.assertEqual(stream.char(), "\u2019")
103-
104-
def test_bom(self):
105-
stream = HTMLInputStream(codecs.BOM_UTF8 + b"'")
106-
self.assertEqual(stream.charEncoding[0].name, 'utf-8')
107-
self.assertEqual(stream.char(), "'")
108-
109-
def test_utf_16(self):
110-
stream = HTMLInputStream((' ' * 1025).encode('utf-16'))
111-
self.assertTrue(stream.charEncoding[0].name in ['utf-16le', 'utf-16be'], stream.charEncoding)
112-
self.assertEqual(len(stream.charsUntil(' ', True)), 1025)
113-
114-
def test_newlines(self):
115-
stream = HTMLBinaryInputStreamShortChunk(codecs.BOM_UTF8 + b"a\nbb\r\nccc\rddddxe")
116-
self.assertEqual(stream.position(), (1, 0))
117-
self.assertEqual(stream.charsUntil('c'), "a\nbb\n")
118-
self.assertEqual(stream.position(), (3, 0))
119-
self.assertEqual(stream.charsUntil('x'), "ccc\ndddd")
120-
self.assertEqual(stream.position(), (4, 4))
121-
self.assertEqual(stream.charsUntil('e'), "x")
122-
self.assertEqual(stream.position(), (4, 5))
123-
124-
def test_newlines2(self):
125-
size = HTMLUnicodeInputStream._defaultChunkSize
126-
stream = HTMLInputStream("\r" * size + "\n")
127-
self.assertEqual(stream.charsUntil('x'), "\n" * size)
128-
129-
def test_position(self):
130-
stream = HTMLBinaryInputStreamShortChunk(codecs.BOM_UTF8 + b"a\nbb\nccc\nddde\nf\ngh")
131-
self.assertEqual(stream.position(), (1, 0))
132-
self.assertEqual(stream.charsUntil('c'), "a\nbb\n")
133-
self.assertEqual(stream.position(), (3, 0))
134-
stream.unget("\n")
135-
self.assertEqual(stream.position(), (2, 2))
136-
self.assertEqual(stream.charsUntil('c'), "\n")
137-
self.assertEqual(stream.position(), (3, 0))
138-
stream.unget("\n")
139-
self.assertEqual(stream.position(), (2, 2))
140-
self.assertEqual(stream.char(), "\n")
141-
self.assertEqual(stream.position(), (3, 0))
142-
self.assertEqual(stream.charsUntil('e'), "ccc\nddd")
143-
self.assertEqual(stream.position(), (4, 3))
144-
self.assertEqual(stream.charsUntil('h'), "e\nf\ng")
145-
self.assertEqual(stream.position(), (6, 1))
146-
147-
def test_position2(self):
148-
stream = HTMLUnicodeInputStreamShortChunk("abc\nd")
149-
self.assertEqual(stream.position(), (1, 0))
150-
self.assertEqual(stream.char(), "a")
151-
self.assertEqual(stream.position(), (1, 1))
152-
self.assertEqual(stream.char(), "b")
153-
self.assertEqual(stream.position(), (1, 2))
154-
self.assertEqual(stream.char(), "c")
155-
self.assertEqual(stream.position(), (1, 3))
156-
self.assertEqual(stream.char(), "\n")
157-
self.assertEqual(stream.position(), (2, 0))
158-
self.assertEqual(stream.char(), "d")
159-
self.assertEqual(stream.position(), (2, 1))
160-
161-
def test_python_issue_20007(self):
162-
"""
163-
Make sure we have a work-around for Python bug #20007
164-
http://bugs.python.org/issue20007
165-
"""
166-
class FakeSocket(object):
167-
def makefile(self, _mode, _bufsize=None):
168-
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
169-
170-
source = http_client.HTTPResponse(FakeSocket())
171-
source.begin()
172-
stream = HTMLInputStream(source)
173-
self.assertEqual(stream.charsUntil(" "), "Text")
174-
175-
def test_python_issue_20007_b(self):
176-
"""
177-
Make sure we have a work-around for Python bug #20007
178-
http://bugs.python.org/issue20007
179-
"""
180-
if six.PY2:
181-
return
182-
183-
class FakeSocket(object):
184-
def makefile(self, _mode, _bufsize=None):
185-
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
186-
187-
source = http_client.HTTPResponse(FakeSocket())
188-
source.begin()
189-
wrapped = urllib.response.addinfourl(source, source.msg, "http://example.com")
190-
stream = HTMLInputStream(wrapped)
191-
self.assertEqual(stream.charsUntil(" "), "Text")
192-
193-
194-
def buildTestSuite():
195-
return unittest.defaultTestLoader.loadTestsFromName(__name__)
196-
197-
198-
def main():
199-
buildTestSuite()
200-
unittest.main()
201-
202-
if __name__ == '__main__':
203-
main()
83+
def test_char_ascii():
84+
stream = HTMLInputStream(b"'", encoding='ascii')
85+
assert stream.charEncoding[0].name == 'windows-1252'
86+
assert stream.char() == "'"
87+
88+
def test_char_utf8():
89+
stream = HTMLInputStream('\u2018'.encode('utf-8'), encoding='utf-8')
90+
assert stream.charEncoding[0].name == 'utf-8'
91+
assert stream.char() == '\u2018'
92+
93+
def test_char_win1252():
94+
stream = HTMLInputStream("\xa9\xf1\u2019".encode('windows-1252'))
95+
assert stream.charEncoding[0].name == 'windows-1252'
96+
assert stream.char() == "\xa9"
97+
assert stream.char() == "\xf1"
98+
assert stream.char() == "\u2019"
99+
100+
def test_bom():
101+
stream = HTMLInputStream(codecs.BOM_UTF8 + b"'")
102+
assert stream.charEncoding[0].name == 'utf-8'
103+
assert stream.char() == "'"
104+
105+
def test_utf_16():
106+
stream = HTMLInputStream((' ' * 1025).encode('utf-16'))
107+
assert stream.charEncoding[0].name in ['utf-16le', 'utf-16be']
108+
assert len(stream.charsUntil(' ', True)) == 1025
109+
110+
def test_newlines():
111+
stream = HTMLBinaryInputStreamShortChunk(codecs.BOM_UTF8 + b"a\nbb\r\nccc\rddddxe")
112+
assert stream.position() == (1, 0)
113+
assert stream.charsUntil('c') == "a\nbb\n"
114+
assert stream.position() == (3, 0)
115+
assert stream.charsUntil('x') == "ccc\ndddd"
116+
assert stream.position() == (4, 4)
117+
assert stream.charsUntil('e') == "x"
118+
assert stream.position() == (4, 5)
119+
120+
def test_newlines2():
121+
size = HTMLUnicodeInputStream._defaultChunkSize
122+
stream = HTMLInputStream("\r" * size + "\n")
123+
assert stream.charsUntil('x') == "\n" * size
124+
125+
def test_position():
126+
stream = HTMLBinaryInputStreamShortChunk(codecs.BOM_UTF8 + b"a\nbb\nccc\nddde\nf\ngh")
127+
assert stream.position() == (1, 0)
128+
assert stream.charsUntil('c') == "a\nbb\n"
129+
assert stream.position() == (3, 0)
130+
stream.unget("\n")
131+
assert stream.position() == (2, 2)
132+
assert stream.charsUntil('c') == "\n"
133+
assert stream.position() == (3, 0)
134+
stream.unget("\n")
135+
assert stream.position() == (2, 2)
136+
assert stream.char() == "\n"
137+
assert stream.position() == (3, 0)
138+
assert stream.charsUntil('e') == "ccc\nddd"
139+
assert stream.position() == (4, 3)
140+
assert stream.charsUntil('h') == "e\nf\ng"
141+
assert stream.position() == (6, 1)
142+
143+
def test_position2():
144+
stream = HTMLUnicodeInputStreamShortChunk("abc\nd")
145+
assert stream.position() == (1, 0)
146+
assert stream.char() == "a"
147+
assert stream.position() == (1, 1)
148+
assert stream.char() == "b"
149+
assert stream.position() == (1, 2)
150+
assert stream.char() == "c"
151+
assert stream.position() == (1, 3)
152+
assert stream.char() == "\n"
153+
assert stream.position() == (2, 0)
154+
assert stream.char() == "d"
155+
assert stream.position() == (2, 1)
156+
157+
def test_python_issue_20007():
158+
"""
159+
Make sure we have a work-around for Python bug #20007
160+
http://bugs.python.org/issue20007
161+
"""
162+
class FakeSocket(object):
163+
def makefile(self, _mode, _bufsize=None):
164+
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
165+
166+
source = http_client.HTTPResponse(FakeSocket())
167+
source.begin()
168+
stream = HTMLInputStream(source)
169+
assert stream.charsUntil(" ") == "Text"
170+
171+
def test_python_issue_20007_b():
172+
"""
173+
Make sure we have a work-around for Python bug #20007
174+
http://bugs.python.org/issue20007
175+
"""
176+
if six.PY2:
177+
return
178+
179+
class FakeSocket(object):
180+
def makefile(self, _mode, _bufsize=None):
181+
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
182+
183+
source = http_client.HTTPResponse(FakeSocket())
184+
source.begin()
185+
wrapped = urllib.response.addinfourl(source, source.msg, "http://example.com")
186+
stream = HTMLInputStream(wrapped)
187+
assert stream.charsUntil(" ") == "Text"
Lines changed: 119 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,125 @@
11
from __future__ import absolute_import, division, unicode_literals
22

3-
import unittest
4-
53
from html5lib.filters.whitespace import Filter
64
from html5lib.constants import spaceCharacters
75
spaceCharacters = "".join(spaceCharacters)
86

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

Comments
 (0)
Please sign in to comment.