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

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

Issue 21686: IDLE - Test hyperparser
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:
View unified diff | Download patch
« no previous file with comments | « 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 tkinter import Tk, Text
5 from idlelib.EditorWindow import EditorWindow
6 from idlelib.HyperParser import HyperParser
7
8 class DummyEditwin:
9 def __init__(self, text):
10 self.text = text
11 self.indentwidth = 8
12 self.tabwidth = 8
13 self.context_use_ps1 = True
14 self.num_context_lines = 50, 500, 1000
15
16 _build_char_in_string_func = EditorWindow._build_char_in_string_func
17 is_char_in_string = EditorWindow.is_char_in_string
18
19
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 )
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 del cls.text, cls.editwin
45 cls.root.destroy()
46 del cls.root
47
48 def setUp(self):
49 self.text.insert('insert', self.code)
50
51 def tearDown(self):
52 self.text.delete('1.0', 'end')
53 self.editwin.context_use_ps1 = True
54
55 def get_parser(self, index):
56 """
57 Return a parser object with index at 'index'
58 """
59 return HyperParser(self.editwin, index)
60
61 def test_init(self):
62 """
63 test corner cases in the init method
64 """
65 with self.assertRaises(ValueError) as ve:
66 self.text.tag_add('console', '1.0', '1.end')
67 p = self.get_parser('1.5')
68 self.assertIn('precedes', str(ve.exception))
69
70 # test without ps1
71 self.editwin.context_use_ps1 = False
72
73 # number of lines lesser than 50
74 p = self.get_parser('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')
80
81 def test_is_in_string(self):
82 get = self.get_parser
83
84 p = get('1.0')
85 self.assertFalse(p.is_in_string())
86 p = get('1.4')
87 self.assertTrue(p.is_in_string())
88 p = get('2.3')
89 self.assertFalse(p.is_in_string())
90 p = get('3.3')
91 self.assertFalse(p.is_in_string())
92 p = get('3.7')
93 self.assertTrue(p.is_in_string())
94 p = get('4.6')
95 self.assertTrue(p.is_in_string())
96
97 def test_is_in_code(self):
98 get = self.get_parser
99
100 p = get('1.0')
101 self.assertTrue(p.is_in_code())
102 p = get('1.1')
103 self.assertFalse(p.is_in_code())
104 p = get('2.5')
105 self.assertFalse(p.is_in_code())
106 p = get('3.4')
107 self.assertTrue(p.is_in_code())
108 p = get('3.6')
109 self.assertFalse(p.is_in_code())
110 p = get('4.14')
111 self.assertFalse(p.is_in_code())
112
113 def test_get_surrounding_bracket(self):
114 get = self.get_parser
115
116 def without_mustclose(parser):
117 # a utility function to get surrounding bracket
118 # with mustclose=False
119 return parser.get_surrounding_brackets(mustclose=False)
120
121 def with_mustclose(parser):
122 # a utility function to get surrounding bracket
123 # with mustclose=True
124 return parser.get_surrounding_brackets(mustclose=True)
125
126 p = get('3.2')
127 self.assertIsNone(with_mustclose(p))
128 self.assertIsNone(without_mustclose(p))
129
130 p = get('5.6')
131 self.assertTupleEqual(without_mustclose(p), ('5.4', '5.25'))
132 self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
133
134 p = get('5.23')
135 self.assertTupleEqual(without_mustclose(p), ('5.21', '5.24'))
136 self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
137
138 p = get('6.15')
139 self.assertTupleEqual(without_mustclose(p), ('6.4', '6.end'))
140 self.assertIsNone(with_mustclose(p))
141
142 p = get('9.end')
143 self.assertIsNone(with_mustclose(p))
144 self.assertIsNone(without_mustclose(p))
145
146 def test_get_expression(self):
147 get = self.get_parser
148
149 p = get('4.2')
150 self.assertEqual(p.get_expression(), 'y ')
151
152 p = get('4.7')
153 with self.assertRaises(ValueError) as ve:
154 p.get_expression()
155 self.assertIn('is inside a code', str(ve.exception))
156
157 p = get('5.25')
158 self.assertEqual(p.get_expression(), 'range(10)')
159
160 p = get('6.7')
161 self.assertEqual(p.get_expression(), 'py')
162
163 p = get('6.8')
164 self.assertEqual(p.get_expression(), '')
165
166 p = get('7.9')
167 self.assertEqual(p.get_expression(), 'py')
168
169 p = get('8.end')
170 self.assertEqual(p.get_expression(), 'x.__len__')
171
172 p = get('9.13')
173 self.assertEqual(p.get_expression(), "r'asdf'")
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
190 if __name__ == '__main__':
191 unittest.main(verbosity=2)
OLDNEW
« no previous file with comments | « Lib/idlelib/HyperParser.py ('k') | no next file » | no next file with comments »

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