Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(11)

Side by Side Diff: Lib/idlelib/idle_test/test_hyperparser.py

Issue 21686: IDLE - Test hyperparser
Patch Set: Created 5 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/idlelib/HyperParser.py ('K') | « Lib/idlelib/HyperParser.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 """Unittest for idlelib.HyperParser"""
2 import unittest
3 from test.support import requires
4 from idlelib.HyperParser import HyperParser
5 from tkinter import Tk, Text
6
7 class DummyEditwin:
8 def __init__(self, text):
9 self.text = text
10 self.indentwidth = 8
11 self.tabwidth = 8
12 self.context_use_ps1 = True
13
14 # existing code from EditorWindow
terry.reedy 2014/06/15 19:44:47 In Python, we can copy the object instead of the c
15 def _build_char_in_string_func(self, startindex):
16 def inner(offset, _startindex=startindex,
17 _icis=self.is_char_in_string):
18 return _icis(_startindex + "+%dc" % offset)
19 return inner
20
21 # existing code from EditorWindow
22 def is_char_in_string(self, text_index):
23 if self.color:
24 # Return true iff colorizer hasn't (re)gotten this far
25 # yet, or the character is tagged as being in a string
26 return self.text.tag_prevrange("TODO", text_index) or \
27 "STRING" in self.text.tag_names(text_index)
28 else:
29 # The colorizer is missing: assume the worst
30 return 1
31
32
33 class HyperParserTest(unittest.TestCase):
34
35 @classmethod
36 def setUpClass(cls):
37 requires('gui')
38 cls.root = Tk()
39 cls.text = Text(cls.root)
40 cls.editwin = DummyEditwin(cls.text)
41
42 @classmethod
43 def tearDownClass(cls):
44 cls.root.destroy()
45 del cls.text, cls.root
46
47 def setUp(self):
48 code = []
49 code.append('"""This is a module docstring"""')
50 code.append('# this line is a comment')
51 code.append('x = "this is a string"')
52 code.append("y = 'this is also a string'")
53 code.append('l = [i for i in range(10)]')
54 code.append('m = [py*py for # comment')
55 code.append(' py in l]')
56 code.append('x.__len__')
57 code.append("z = ((r'asdf')+('a')))")
58 code.append('[x for x in')
59 code.append('False =')
60 self.text.insert('insert', '\n'.join(code))
61
62 def tearDown(self):
63 self.text.delete('1.0', 'end')
64 self.editwin.context_use_ps1 = True
65
66 def get_parser(self, index):
67 """
68 Return a parser object with index at 'index'
69 """
70 return HyperParser(self.editwin, index)
71
72 def test_init(self):
73 """
74 test corner cases in the init method
75 """
76 with self.assertRaises(ValueError) as ve:
77 self.text.tag_add('console', '1.0', '1.end')
78 p = self.get_parser('1.5')
79 self.assertIn('before the analyzed', str(ve.exception))
80
81 # test without ps1
82 self.editwin.context_use_ps1 = False
83 # reason for the constants explained in EditorWindow
84 self.editwin.num_context_lines = 50, 500, 1000
sahutd 2014/06/14 16:38:34 @ Tal Einat: Is the this line and the block below
taleinat 2014/06/14 17:45:21 Indeed. However, to avoid side effects on other te
terry.reedy 2014/06/15 19:44:47 The dummy editwin did not have this attribute, The
85
86 # number of lines lesser than 50
87 p = self.get_parser('end')
88 self.assertEqual(p.rawtext, self.text.get('1.0', 'end'))
89
90 # number of lines greater than 50
91 self.text.insert('end', self.text.get('1.0', 'end')*4)
92 p = self.get_parser('54.5')
93
94 def test_is_in_string(self):
95 get = self.get_parser
96
97 p = get('1.0')
98 self.assertFalse(p.is_in_string())
99 p = get('1.4')
100 self.assertTrue(p.is_in_string())
101 p = get('2.3')
102 self.assertFalse(p.is_in_string())
103 p = get('3.3')
104 self.assertFalse(p.is_in_string())
105 p = get('3.7')
106 self.assertTrue(p.is_in_string())
107 p = get('4.6')
108 self.assertTrue(p.is_in_string())
109
110 def test_is_in_code(self):
111 get = self.get_parser
112
113 p = get('1.0')
114 self.assertTrue(p.is_in_code())
115 p = get('1.1')
116 self.assertFalse(p.is_in_code())
117 p = get('2.5')
118 self.assertFalse(p.is_in_code())
119 p = get('3.4')
120 self.assertTrue(p.is_in_code())
121 p = get('3.6')
122 self.assertFalse(p.is_in_code())
123 p = get('4.14')
124 self.assertFalse(p.is_in_code())
125
126 def test_get_surrounding_bracket(self):
127 get = self.get_parser
128
129 def without_mustclose(parser):
130 # a utility function to get surrounding bracket
131 # with mustclose=False
132 return parser.get_surrounding_brackets(mustclose=False)
133
134 def with_mustclose(parser):
135 # a utility function to get surrounding bracket
136 # with mustclose=True
137 return parser.get_surrounding_brackets(mustclose=True)
138
139 p = get('3.2')
140 self.assertIsNone(with_mustclose(p))
141 self.assertIsNone(without_mustclose(p))
142
143 p = get('5.6')
144 self.assertTupleEqual(without_mustclose(p), ('5.4', '5.25'))
145 self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
146
147 p = get('5.23')
148 self.assertTupleEqual(without_mustclose(p), ('5.21', '5.24'))
149 self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
150
151 p = get('6.15')
152 self.assertTupleEqual(without_mustclose(p), ('6.4', '6.end'))
153 self.assertIsNone(with_mustclose(p))
154
155 p = get('9.end')
156 self.assertIsNone(with_mustclose(p))
157 self.assertIsNone(without_mustclose(p))
158
159 def test_get_expression(self):
160 get = self.get_parser
161
162 p = get('4.2')
163 self.assertEqual(p.get_expression(), 'y ')
164
165 p = get('4.7')
166 with self.assertRaises(ValueError) as ve:
167 p.get_expression()
168 self.assertIn('is inside a code', str(ve.exception))
169
170 p = get('5.25')
171 self.assertEqual(p.get_expression(), 'range(10)')
172
173 p = get('6.7')
174 self.assertEqual(p.get_expression(), 'py')
175
176 p = get('6.8')
177 self.assertEqual(p.get_expression(), '')
178
179 p = get('7.9')
180 self.assertEqual(p.get_expression(), 'py')
181
182 p = get('8.end')
183 self.assertEqual(p.get_expression(), 'x.__len__')
184
185 p = get('9.13')
186 self.assertEqual(p.get_expression(), "r'asdf'")
187
188 p = get('9.17')
189 with self.assertRaises(ValueError) as ve:
190 p.get_expression()
191 self.assertIn('is inside a code', str(ve.exception))
192
193 p = get('10.0')
194 self.assertEqual(p.get_expression(), '')
195
196 p = get('11.5')
197 self.assertEqual(p.get_expression(), '')
198
199 if __name__ == '__main__':
200 unittest.main(verbosity=2)
OLDNEW
« Lib/idlelib/HyperParser.py ('K') | « Lib/idlelib/HyperParser.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+