OILS / lazylex / html_test.py View on Github | oils.pub

260 lines, 165 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3
4import unittest
5
6from lazylex import html # module under test log = html.log
7
8log = html.log
9
10with open('lazylex/testdata.html') as f:
11 TEST_HTML = f.read()
12
13
14def _MakeTagLexer(s):
15 lex = html.TagLexer(s)
16 lex.Reset(0, len(s))
17 return lex
18
19
20def _PrintTokens(lex):
21 log('')
22 log('tag = %r', lex.TagName())
23 for tok, start, end in lex.Tokens():
24 log('%s %r', tok, lex.s[start:end])
25
26
27class RegexTest(unittest.TestCase):
28
29 def testDotAll(self):
30 import re
31
32 # Note that $ matches end of line, not end of string
33 p1 = re.compile(r'.')
34 print(p1.match('\n'))
35
36 p2 = re.compile(r'.', re.DOTALL)
37 print(p2.match('\n'))
38
39 #p3 = re.compile(r'[.\n]', re.VERBOSE)
40 p3 = re.compile(r'[.\n]')
41 print(p3.match('\n'))
42
43 print('Negation')
44
45 p4 = re.compile(r'[^>]')
46 print(p4.match('\n'))
47
48
49class FunctionsTest(unittest.TestCase):
50
51 def testFindLineNum(self):
52 s = 'foo\n' * 3
53 for pos in [1, 5, 10, 50]: # out of bounds
54 line_num = html.FindLineNum(s, pos)
55 print(line_num)
56
57
58class TagLexerTest(unittest.TestCase):
59
60 def testTagLexer(self):
61 # Invalid!
62 #lex = _MakeTagLexer('< >')
63 #print(lex.Tag())
64
65 lex = _MakeTagLexer('<a>')
66 _PrintTokens(lex)
67
68 lex = _MakeTagLexer('<a novalue>')
69 _PrintTokens(lex)
70
71 # Note: we could have a different HasAttr() method
72 # <a novalue> means lex.Get('novalue') == None
73 # https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute
74 self.assertEqual(None, lex.GetAttrRaw('novalue'))
75
76 lex = _MakeTagLexer('<a href="double quoted">')
77 _PrintTokens(lex)
78
79 self.assertEqual('double quoted', lex.GetAttrRaw('href'))
80 self.assertEqual(None, lex.GetAttrRaw('oops'))
81
82 lex = _MakeTagLexer('<a href=foo class="bar">')
83 _PrintTokens(lex)
84
85 lex = _MakeTagLexer('<a href=foo class="bar" />')
86 _PrintTokens(lex)
87
88 lex = _MakeTagLexer('<a href="?foo=1&amp;bar=2" />')
89 self.assertEqual('?foo=1&amp;bar=2', lex.GetAttrRaw('href'))
90
91 def testTagName(self):
92 lex = _MakeTagLexer('<a href=foo class="bar" />')
93 self.assertEqual('a', lex.TagName())
94
95 def testAllAttrs(self):
96 """
97 [('key', 'value')] for all
98 """
99 # closed
100 lex = _MakeTagLexer('<a href=foo class="bar" />')
101 self.assertEqual([('href', 'foo'), ('class', 'bar')],
102 lex.AllAttrsRaw())
103
104 lex = _MakeTagLexer('<a href="?foo=1&amp;bar=2" />')
105 self.assertEqual([('href', '?foo=1&amp;bar=2')], lex.AllAttrsRaw())
106
107
108def Lex(h):
109 print(repr(h))
110 lex = html.ValidTokens(h)
111 tokens = list(lex)
112 for tok_id, end_pos in tokens:
113 log('%d %s', end_pos, html.TokenName(tok_id))
114 return tokens
115
116
117class LexerTest(unittest.TestCase):
118
119 # IndexLinker in devtools/make_help.py
120 # <pre> sections in doc/html_help.py
121 # TocExtractor in devtools/cmark.py
122
123 def testPstrip(self):
124 """Remove anything like this.
125
126 <p><pstrip> </pstrip></p>
127 """
128 pass
129
130 def testCommentParse(self):
131 n = len(TEST_HTML)
132 for tok_id, end_pos in html._Tokens(TEST_HTML, 0, n):
133 if tok_id == html.Invalid:
134 raise RuntimeError()
135 print(tok_id)
136
137 def testCommentParse2(self):
138
139 Tok = html.Tok
140 h = '''
141 hi <!-- line 1
142 line 2 --><br/>'''
143 tokens = Lex(h)
144
145 self.assertEqual(
146 [
147 (Tok.RawData, 12),
148 (Tok.Comment, 50), # <? err ?>
149 (Tok.StartEndTag, 55),
150 (Tok.EndOfStream, 55),
151 ],
152 tokens)
153
154 def testProcessingInstruction(self):
155 # <?xml ?> header
156 Tok = html.Tok
157 h = 'hi <? err ?>'
158 tokens = Lex(h)
159
160 self.assertEqual(
161 [
162 (Tok.RawData, 3),
163 (Tok.Processing, 12), # <? err ?>
164 (Tok.EndOfStream, 12),
165 ],
166 tokens)
167
168 def testScriptStyle(self):
169 Tok = html.Tok
170 h = '''
171 hi <script src=""> if (x < 1 && y > 2 ) { console.log(""); }
172 </script>
173 '''
174 tokens = Lex(h)
175
176 self.assertEqual(
177 [
178 (Tok.RawData, 12),
179 (Tok.StartTag, 27), # <script>
180 (Tok.HtmlCData, 78), # JavaScript code is HTML CData
181 (Tok.EndTag, 87), # </script>
182 (Tok.RawData, 96), # \n
183 (Tok.EndOfStream, 96), # \n
184 ],
185 tokens)
186
187 def testCData(self):
188 Tok = html.Tok
189
190 # from
191 # /home/andy/src/languages/Python-3.11.5/Lib/test/xmltestdata/c14n-20/inC14N4.xml
192 h = '<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>'
193 tokens = Lex(h)
194
195 self.assertEqual([
196 (Tok.StartTag, 9),
197 (Tok.CData, 61),
198 (Tok.EndTag, 71),
199 (Tok.EndOfStream, 71),
200 ], tokens)
201
202 def testEntity(self):
203 Tok = html.Tok
204
205 # from
206 # /home/andy/src/Python-3.12.4/Lib/test/xmltestdata/c14n-20/inC14N5.xml
207 h = '&ent1;, &ent2;!'
208
209 tokens = Lex(h)
210
211 self.assertEqual([
212 (Tok.CharEntity, 6),
213 (Tok.RawData, 8),
214 (Tok.CharEntity, 14),
215 (Tok.RawData, 15),
216 (Tok.EndOfStream, 15),
217 ], tokens)
218
219 def testStartTag(self):
220 Tok = html.Tok
221
222 h = '<a>hi</a>'
223 tokens = Lex(h)
224
225 self.assertEqual([
226 (Tok.StartTag, 3),
227 (Tok.RawData, 5),
228 (Tok.EndTag, 9),
229 (Tok.EndOfStream, 9),
230 ], tokens)
231
232 def testInvalid(self):
233 Tok = html.Tok
234
235 INVALID = [
236 # Should be &amp;
237 '<a>&',
238 '&amp', # not finished
239 '&#', # not finished
240 # Hm > is allowed?
241 #'a > b',
242 'a < b',
243 '<!-- unfinished comment',
244 '<? unfinished processing',
245 '</div bad=attr> <a> <b>',
246 ]
247
248 for s in INVALID:
249 lex = html.ValidTokens(s)
250 try:
251 for i in xrange(5):
252 tok_id, pos = next(lex)
253 except html.LexError as e:
254 print(e)
255 else:
256 self.fail('Expected LexError')
257
258
259if __name__ == '__main__':
260 unittest.main()