Debian Codegraph @

tests/test_util.py

raw ◀ ▶
1 Debian patch touch this file
1 """
2 Test suite for the util module
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
6 :license: BSD, see LICENSE for details.
7 """
8
9 from pytest import raises
10
11 from pygments import util, console
12
13
14 class FakeLexer:
15 def analyse(text):
16 return text
17 analyse = util.make_analysator(analyse)
18
19
20 def test_getoptions():
21 assert util.get_bool_opt({}, 'a', True) is True
22 assert util.get_bool_opt({}, 'a', 1) is True
23 assert util.get_bool_opt({}, 'a', 'true') is True
24 assert util.get_bool_opt({}, 'a', 'no') is False
25 assert raises(util.OptionError, util.get_bool_opt, {}, 'a', [])
26 assert raises(util.OptionError, util.get_bool_opt, {}, 'a', 'foo')
27
28 assert util.get_int_opt({}, 'a', 1) == 1
29 assert raises(util.OptionError, util.get_int_opt, {}, 'a', [])
30 assert raises(util.OptionError, util.get_int_opt, {}, 'a', 'bar')
31
32 assert util.get_list_opt({}, 'a', [1]) == [1]
33 assert util.get_list_opt({}, 'a', '1 2') == ['1', '2']
34 assert raises(util.OptionError, util.get_list_opt, {}, 'a', 1)
35
36 assert util.get_choice_opt({}, 'a', ['foo', 'bar'], 'bar') == 'bar'
37 assert util.get_choice_opt({}, 'a', ['foo', 'bar'], 'Bar', True) == 'bar'
38 assert raises(util.OptionError, util.get_choice_opt, {}, 'a',
39 ['foo', 'bar'], 'baz')
40
41
42 def test_docstring_headline():
43 def f1():
44 """
45 docstring headline
46
47 other text
48 """
49 def f2():
50 """
51 docstring
52 headline
53
54 other text
55 """
56 def f3():
57 pass
58
59 assert util.docstring_headline(f1) == 'docstring headline'
60 assert util.docstring_headline(f2) == 'docstring headline'
61 assert util.docstring_headline(f3) == ''
62
63
64 def test_analysator_returns_float():
65 # If an analysator wrapped by make_analysator returns a floating point
66 # number, then that number will be returned by the wrapper.
67 assert FakeLexer.analyse('0.5') == 0.5
68
69
70 def test_analysator_returns_boolean():
71 # If an analysator wrapped by make_analysator returns a boolean value,
72 # then the wrapper will return 1.0 if the boolean was True or 0.0 if
73 # it was False.
74 assert FakeLexer.analyse(True) == 1.0
75 assert FakeLexer.analyse(False) == 0.0
76
77
78 def test_analysator_raises_exception():
79 # If an analysator wrapped by make_analysator raises an exception,
80 # then the wrapper will return 0.0.
81 class ErrorLexer:
82 def analyse(text):
83 raise RuntimeError('something bad happened')
84 analyse = util.make_analysator(analyse)
85 assert ErrorLexer.analyse('') == 0.0
86
87
88 def test_analysator_value_error():
89 # When converting the analysator's return value to a float a
90 # ValueError may occur. If that happens 0.0 is returned instead.
91 assert FakeLexer.analyse('bad input') == 0.0
92
93
94 def test_analysator_type_error():
95 # When converting the analysator's return value to a float a
96 # TypeError may occur. If that happens 0.0 is returned instead.
97 assert FakeLexer.analyse('xxx') == 0.0
98
99
100 def test_shebang_matches():
101 assert util.shebang_matches('#!/usr/bin/env python\n', r'python(2\.\d)?')
102 assert util.shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')
103 assert util.shebang_matches('#!/usr/bin/startsomethingwith python',
104 r'python(2\.\d)?')
105 assert util.shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')
106
107 assert not util.shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')
108 assert not util.shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')
109 assert not util.shebang_matches('#!', r'python')
110
111
112 def test_doctype_matches():
113 assert util.doctype_matches('<!DOCTYPE html> <html>', 'html.*')
114 assert not util.doctype_matches(
115 '<?xml ?> <DOCTYPE html PUBLIC "a"> <html>', 'html.*')
116 assert util.html_doctype_matches(
117 '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')
118
119
120 def test_xml():
121 assert util.looks_like_xml(
122 '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')
123 assert util.looks_like_xml('<html xmlns>abc</html>')
124 assert not util.looks_like_xml('<html>')
125
126
127 def test_format_lines():
128 lst = ['cat', 'dog']
129 output = util.format_lines('var', lst)
130 d = {}
131 exec(output, d)
132 assert isinstance(d['var'], tuple)
133 assert ('cat', 'dog') == d['var']
134
135
136 def test_duplicates_removed_seq_types():
137 # tuple
138 x = util.duplicates_removed(('a', 'a', 'b'))
139 assert ['a', 'b'] == x
140 # list
141 x = util.duplicates_removed(['a', 'a', 'b'])
142 assert ['a', 'b'] == x
143 # iterator
144 x = util.duplicates_removed(iter(('a', 'a', 'b')))
145 assert ['a', 'b'] == x
146
147
148 def test_duplicates_removed_nonconsecutive():
149 # keeps first
150 x = util.duplicates_removed(('a', 'b', 'a'))
151 assert ['a', 'b'] == x
152
153
154 def test_guess_decode():
155 # UTF-8 should be decoded as UTF-8
156 s = util.guess_decode('\xff'.encode())
157 assert s == ('\xff', 'utf-8')
158
159 # otherwise, it could be latin1 or the locale encoding...
160 import locale
161 s = util.guess_decode(b'\xff')
162 assert s[1] in ('latin1', locale.getpreferredencoding())
163
164
165 def test_guess_decode_from_terminal():
166 class Term:
167 encoding = 'utf-7'
168
169 s = util.guess_decode_from_terminal('\xff'.encode('utf-7'), Term)
170 assert s == ('\xff', 'utf-7')
171
172 s = util.guess_decode_from_terminal('\xff'.encode(), Term)
173 assert s == ('\xff', 'utf-8')
174
175
176 def test_console_ansiformat():
177 f = console.ansiformat
178 c = console.codes
179 all_attrs = f('+*_blue_*+', 'text')
180 assert c['blue'] in all_attrs and c['blink'] in all_attrs
181 assert c['bold'] in all_attrs and c['underline'] in all_attrs
182 assert c['reset'] in all_attrs
183 assert raises(KeyError, f, '*mauve*', 'text')
184
185
186 def test_console_functions():
187 assert console.reset_color() == console.codes['reset']
188 assert console.colorize('blue', 'text') == \
189 console.codes['blue'] + 'text' + console.codes['reset']
•190
191 def test_html_escape():
192 assert util.html_escape('test') == 'test'
193 assert util.html_escape('<>&"\'') == '&lt;&gt;&amp;&quot;&#x27;'
194 assert util.html_escape('<>&"\'', quote=False) == '&lt;&gt;&amp;"\''
195 assert util.html_escape(None) == ''