1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 |
|
4 | import re
|
5 | import unittest
|
6 |
|
7 | from doctools import html_old as html
|
8 | from doctools.util import log
|
9 |
|
10 |
|
11 | class RegexTest(unittest.TestCase):
|
12 |
|
13 | def testDotAll(self):
|
14 | # type: () -> None
|
15 |
|
16 | # Note that $ matches end of line, not end of string
|
17 | p1 = re.compile(r'.')
|
18 | print(p1.match('\n'))
|
19 |
|
20 | p2 = re.compile(r'.', re.DOTALL)
|
21 | print(p2.match('\n'))
|
22 |
|
23 | #p3 = re.compile(r'[.\n]', re.VERBOSE)
|
24 | p3 = re.compile(r'[.\n]')
|
25 | print(p3.match('\n'))
|
26 |
|
27 | print('Negation')
|
28 |
|
29 | p4 = re.compile(r'[^>]')
|
30 | print(p4.match('\n'))
|
31 |
|
32 | def testAttrRe(self):
|
33 | # type: () -> None
|
34 | _ATTR_RE = html._ATTR_RE
|
35 | m = _ATTR_RE.match(' empty= val')
|
36 | print(m.groups())
|
37 |
|
38 |
|
39 | class FunctionsTest(unittest.TestCase):
|
40 |
|
41 | def testToText(self):
|
42 | # type: () -> None
|
43 | t = html.ToText('<b name="&"> three < four && five </b>')
|
44 | self.assertEqual(' three < four && five ', t)
|
45 |
|
46 |
|
47 | def _MakeTagLexer(s):
|
48 | # type: (str) -> html.TagLexer
|
49 | lex = html.TagLexer(s)
|
50 | lex.Reset(0, len(s))
|
51 | return lex
|
52 |
|
53 |
|
54 | def _PrintTokens(lex):
|
55 | # type: (html.TagLexer) -> None
|
56 | log('')
|
57 | log('tag = %r', lex.GetTagName())
|
58 | for tok, start, end in lex.Tokens():
|
59 | log('%s %r', tok, lex.s[start:end])
|
60 |
|
61 |
|
62 | class TagLexerTest(unittest.TestCase):
|
63 |
|
64 | def testTagName_DEPRECATED(self):
|
65 | # type: () -> None
|
66 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
67 | self.assertEqual('a', lex.GetTagName())
|
68 |
|
69 | def testGetAttrRaw(self):
|
70 | # type: () -> None
|
71 | lex = _MakeTagLexer('<a>')
|
72 | _PrintTokens(lex)
|
73 | self.assertEqual(None, lex.GetAttrRaw('oops'))
|
74 |
|
75 | # <a novalue> means lex.Get('novalue') == ''
|
76 | # https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute
|
77 | # We are not distinguishing <a novalue=""> from <a novalue> in this API
|
78 | lex = _MakeTagLexer('<a novalue>')
|
79 | _PrintTokens(lex)
|
80 | self.assertEqual('', lex.GetAttrRaw('novalue'))
|
81 |
|
82 | lex = _MakeTagLexer('<a href="double quoted">')
|
83 | _PrintTokens(lex)
|
84 |
|
85 | self.assertEqual('double quoted', lex.GetAttrRaw('href'))
|
86 | self.assertEqual(None, lex.GetAttrRaw('oops'))
|
87 |
|
88 | lex = _MakeTagLexer('<a href=foo class="bar">')
|
89 | _PrintTokens(lex)
|
90 | self.assertEqual('bar', lex.GetAttrRaw('class'))
|
91 |
|
92 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
93 | _PrintTokens(lex)
|
94 | self.assertEqual('bar', lex.GetAttrRaw('class'))
|
95 |
|
96 | lex = _MakeTagLexer('<a href="?foo=1&bar=2" />')
|
97 | self.assertEqual('?foo=1&bar=2', lex.GetAttrRaw('href'))
|
98 |
|
99 | def testAllAttrs(self):
|
100 | # type: () -> None
|
101 | """
|
102 | [('key', 'value')] for all
|
103 | """
|
104 | # closed
|
105 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
106 | self.assertEqual([('href', 'foo'), ('class', 'bar')],
|
107 | lex.AllAttrsRaw())
|
108 |
|
109 | lex = _MakeTagLexer('<a href="?foo=1&bar=2" />')
|
110 | self.assertEqual([('href', '?foo=1&bar=2')], lex.AllAttrsRaw())
|
111 |
|
112 | def testEmptyMissingValues(self):
|
113 | # type: () -> None
|
114 | # equivalent to <button disabled="">
|
115 | lex = _MakeTagLexer('<button disabled>')
|
116 | all_attrs = lex.AllAttrsRaw()
|
117 | self.assertEqual([('disabled', '')], all_attrs)
|
118 |
|
119 | slices = lex.AllAttrsRawSlice()
|
120 | log('slices %s', slices)
|
121 |
|
122 | lex = _MakeTagLexer(
|
123 | '''<p double="" single='' empty= value missing empty2=>''')
|
124 | all_attrs = lex.AllAttrsRaw()
|
125 | self.assertEqual([
|
126 | ('double', ''),
|
127 | ('single', ''),
|
128 | ('empty', 'value'),
|
129 | ('missing', ''),
|
130 | ('empty2', ''),
|
131 | ], all_attrs)
|
132 | # TODO: should have
|
133 | log('all %s', all_attrs)
|
134 |
|
135 | slices = lex.AllAttrsRawSlice()
|
136 | log('slices %s', slices)
|
137 |
|
138 | def testInvalidTag(self):
|
139 | # type: () -> None
|
140 | try:
|
141 | lex = _MakeTagLexer('<a foo=bar !></a>')
|
142 | all_attrs = lex.AllAttrsRaw()
|
143 | except html.LexError as e:
|
144 | print(e)
|
145 | else:
|
146 | self.fail('Expected LexError')
|
147 |
|
148 |
|
149 | if __name__ == '__main__':
|
150 | unittest.main()
|