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

Delta Between Two Patch Sets: Lib/idlelib/idle_test/test_hyperparser.py

Issue 21686: IDLE - Test hyperparser
Left Patch Set: Created 5 years, 8 months ago
Right Patch Set: Created 5 years, 8 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/idlelib/HyperParser.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Unittest for idlelib.HyperParser""" 1 """Unittest for idlelib.HyperParser"""
2 import unittest
2 from test.support import requires 3 from test.support import requires
3 requires('gui') 4 from tkinter import Tk, Text
4 5 from idlelib.EditorWindow import EditorWindow
5 import unittest
6 from idlelib.HyperParser import HyperParser 6 from idlelib.HyperParser import HyperParser
7 from tkinter import Text
8 7
9 class DummyEditwin: 8 class DummyEditwin:
10 def __init__(self, text): 9 def __init__(self, text):
11 self.text = text 10 self.text = text
12 self.indentwidth = 8 11 self.indentwidth = 8
13 self.tabwidth = 8 12 self.tabwidth = 8
14 self.context_use_ps1 = True 13 self.context_use_ps1 = True
14 self.num_context_lines = 50, 500, 1000
15 15
16 # existing code from EditorWindow 16 _build_char_in_string_func = EditorWindow._build_char_in_string_func
17 def _build_char_in_string_func(self, startindex): 17 is_char_in_string = EditorWindow.is_char_in_string
18 def inner(offset, _startindex=startindex,
19 _icis=self.is_char_in_string):
20 return _icis(_startindex + "+%dc" % offset)
21 return inner
22 18
23 # existing code from EditorWindow
24 def is_char_in_string(self, text_index):
25 if self.color:
26 # Return true iff colorizer hasn't (re)gotten this far
27 # yet, or the character is tagged as being in a string
28 return self.text.tag_prevrange("TODO", text_index) or \
29 "STRING" in self.text.tag_names(text_index)
30 else:
31 # The colorizer is missing: assume the worst
32 return 1
33 19
34 class HyperParserTest(unittest.TestCase): 20 class HyperParserTest(unittest.TestCase):
21 code = (
22 '"""This is a module docstring"""\n'
23 '# this line is a comment\n'
24 'x = "this is a string"\n'
25 "y = 'this is also a string'\n"
26 'l = [i for i in range(10)]\n'
27 'm = [py*py for # comment\n'
28 ' py in l]\n'
29 'x.__len__\n'
30 "z = ((r'asdf')+('a')))\n"
31 '[x for x in\n'
32 'for = False\n'
33 )
35 34
36 @classmethod 35 @classmethod
37 def setUpClass(cls): 36 def setUpClass(cls):
38 cls.text = Text() 37 requires('gui')
38 cls.root = Tk()
39 cls.text = Text(cls.root)
39 cls.editwin = DummyEditwin(cls.text) 40 cls.editwin = DummyEditwin(cls.text)
40 41
41 @classmethod 42 @classmethod
42 def tearDownClass(cls): 43 def tearDownClass(cls):
43 cls.text.destroy() 44 del cls.text, cls.editwin
44 del cls.text 45 cls.root.destroy()
46 del cls.root
45 47
46 def setUp(self): 48 def setUp(self):
47 code = [] 49 self.text.insert('insert', self.code)
48 code.append('"""This is a module docstring"""')
49 code.append('# this line is a comment')
50 code.append('x = "this is a string"')
51 code.append("y = 'this is also a string'")
52 code.append('l = [i for i in range(10)]')
53 code.append('m = [py*py for')
54 code.append(' py in l]')
55 code.append('x.__len__')
56 code.append("z = ((r'asdf')+('asdf')))")
57 self.text.insert('insert', '\n'.join(code))
58 50
59 def tearDown(self): 51 def tearDown(self):
60 self.text.delete('1.0', 'end') 52 self.text.delete('1.0', 'end')
61 self.editwin.context_use_ps1 = True 53 self.editwin.context_use_ps1 = True
62 54
63 def get_parser(self, index): 55 def get_parser(self, index):
64 """ 56 """
65 Return a parser object with index at 'index' 57 Return a parser object with index at 'index'
66 """ 58 """
67 return HyperParser(self.editwin, index) 59 return HyperParser(self.editwin, index)
68 60
69 def test_init(self): 61 def test_init(self):
70 """ 62 """
71 test corner cases in the init method 63 test corner cases in the init method
72 """ 64 """
73 with self.assertRaises(ValueError) as ve: 65 with self.assertRaises(ValueError) as ve:
74 self.text.tag_add('console', '1.0', '1.end') 66 self.text.tag_add('console', '1.0', '1.end')
75 p = self.get_parser('1.5') 67 p = self.get_parser('1.5')
76 self.assertIn('before the analyzed', str(ve.exception)) 68 self.assertIn('precedes', str(ve.exception))
77 69
78 # test without ps1 70 # test without ps1
79 self.editwin.context_use_ps1 = False 71 self.editwin.context_use_ps1 = False
80 # reason for the constants explained in EditorWindow 72
81 self.editwin.num_context_lines = 50, 500, 5000000 73 # number of lines lesser than 50
82 p = self.get_parser('end') 74 p = self.get_parser('end')
83 self.assertEqual(p.rawtext, self.text.get('1.0', 'end')) 75 self.assertEqual(p.rawtext, self.text.get('1.0', 'end'))
76
77 # number of lines greater than 50
78 self.text.insert('end', self.text.get('1.0', 'end')*4)
79 p = self.get_parser('54.5')
84 80
85 def test_is_in_string(self): 81 def test_is_in_string(self):
86 get = self.get_parser 82 get = self.get_parser
87 83
88 p = get('1.0') 84 p = get('1.0')
89 self.assertFalse(p.is_in_string()) 85 self.assertFalse(p.is_in_string())
90 p = get('1.4') 86 p = get('1.4')
91 self.assertTrue(p.is_in_string()) 87 self.assertTrue(p.is_in_string())
92 p = get('2.3') 88 p = get('2.3')
93 self.assertFalse(p.is_in_string()) 89 self.assertFalse(p.is_in_string())
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 self.assertTupleEqual(without_mustclose(p), ('6.4', '6.end')) 139 self.assertTupleEqual(without_mustclose(p), ('6.4', '6.end'))
144 self.assertIsNone(with_mustclose(p)) 140 self.assertIsNone(with_mustclose(p))
145 141
146 p = get('9.end') 142 p = get('9.end')
147 self.assertIsNone(with_mustclose(p)) 143 self.assertIsNone(with_mustclose(p))
148 self.assertIsNone(without_mustclose(p)) 144 self.assertIsNone(without_mustclose(p))
149 145
150 def test_get_expression(self): 146 def test_get_expression(self):
151 get = self.get_parser 147 get = self.get_parser
152 148
149 p = get('4.2')
150 self.assertEqual(p.get_expression(), 'y ')
151
153 p = get('4.7') 152 p = get('4.7')
154 with self.assertRaises(ValueError) as ve: 153 with self.assertRaises(ValueError) as ve:
155 p.get_expression() 154 p.get_expression()
156 self.assertIn('is inside a code', str(ve.exception)) 155 self.assertIn('is inside a code', str(ve.exception))
157
158 p = get('4.2')
159 self.assertEqual(p.get_expression(), 'y ')
160 156
161 p = get('5.25') 157 p = get('5.25')
162 self.assertEqual(p.get_expression(), 'range(10)') 158 self.assertEqual(p.get_expression(), 'range(10)')
163 159
164 p = get('6.7') 160 p = get('6.7')
165 self.assertEqual(p.get_expression(), 'py') 161 self.assertEqual(p.get_expression(), 'py')
166 162
167 p = get('6.8') 163 p = get('6.8')
168 self.assertEqual(p.get_expression(), '') 164 self.assertEqual(p.get_expression(), '')
169 165
166 p = get('7.9')
167 self.assertEqual(p.get_expression(), 'py')
168
170 p = get('8.end') 169 p = get('8.end')
171 self.assertEqual(p.get_expression(), 'x.__len__') 170 self.assertEqual(p.get_expression(), 'x.__len__')
172 171
173 p = get('9.13') 172 p = get('9.13')
174 self.assertEqual(p.get_expression(), "r'asdf'") 173 self.assertEqual(p.get_expression(), "r'asdf'")
175 174
175 p = get('9.17')
176 with self.assertRaises(ValueError) as ve:
177 p.get_expression()
178 self.assertIn('is inside a code', str(ve.exception))
179
180 p = get('10.0')
181 self.assertEqual(p.get_expression(), '')
182
183 p = get('11.3')
184 self.assertEqual(p.get_expression(), '')
185
186 p = get('11.11')
187 self.assertEqual(p.get_expression(), 'False')
188
189
176 if __name__ == '__main__': 190 if __name__ == '__main__':
177 unittest.main(verbosity=2) 191 unittest.main(verbosity=2)
LEFTRIGHT

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