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

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 import unittest
3 from test.support import requires 3 from test.support import requires
4 from tkinter import Tk, Text
5 from idlelib.EditorWindow import EditorWindow
4 from idlelib.HyperParser import HyperParser 6 from idlelib.HyperParser import HyperParser
5 from tkinter import Tk, Text
6 7
7 class DummyEditwin: 8 class DummyEditwin:
8 def __init__(self, text): 9 def __init__(self, text):
9 self.text = text 10 self.text = text
10 self.indentwidth = 8 11 self.indentwidth = 8
11 self.tabwidth = 8 12 self.tabwidth = 8
12 self.context_use_ps1 = True 13 self.context_use_ps1 = True
14 self.num_context_lines = 50, 500, 1000
13 15
14 # existing code from EditorWindow 16 _build_char_in_string_func = EditorWindow._build_char_in_string_func
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): 17 is_char_in_string = EditorWindow.is_char_in_string
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 18
32 19
33 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 )
34 34
35 @classmethod 35 @classmethod
36 def setUpClass(cls): 36 def setUpClass(cls):
37 requires('gui') 37 requires('gui')
38 cls.root = Tk() 38 cls.root = Tk()
39 cls.text = Text(cls.root) 39 cls.text = Text(cls.root)
40 cls.editwin = DummyEditwin(cls.text) 40 cls.editwin = DummyEditwin(cls.text)
41 41
42 @classmethod 42 @classmethod
43 def tearDownClass(cls): 43 def tearDownClass(cls):
44 del cls.text, cls.editwin
44 cls.root.destroy() 45 cls.root.destroy()
45 del cls.text, cls.root 46 del cls.root
46 47
47 def setUp(self): 48 def setUp(self):
48 code = [] 49 self.text.insert('insert', self.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 50
62 def tearDown(self): 51 def tearDown(self):
63 self.text.delete('1.0', 'end') 52 self.text.delete('1.0', 'end')
64 self.editwin.context_use_ps1 = True 53 self.editwin.context_use_ps1 = True
65 54
66 def get_parser(self, index): 55 def get_parser(self, index):
67 """ 56 """
68 Return a parser object with index at 'index' 57 Return a parser object with index at 'index'
69 """ 58 """
70 return HyperParser(self.editwin, index) 59 return HyperParser(self.editwin, index)
71 60
72 def test_init(self): 61 def test_init(self):
73 """ 62 """
74 test corner cases in the init method 63 test corner cases in the init method
75 """ 64 """
76 with self.assertRaises(ValueError) as ve: 65 with self.assertRaises(ValueError) as ve:
77 self.text.tag_add('console', '1.0', '1.end') 66 self.text.tag_add('console', '1.0', '1.end')
78 p = self.get_parser('1.5') 67 p = self.get_parser('1.5')
79 self.assertIn('before the analyzed', str(ve.exception)) 68 self.assertIn('precedes', str(ve.exception))
80 69
81 # test without ps1 70 # test without ps1
82 self.editwin.context_use_ps1 = False 71 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 72
86 # number of lines lesser than 50 73 # number of lines lesser than 50
87 p = self.get_parser('end') 74 p = self.get_parser('end')
88 self.assertEqual(p.rawtext, self.text.get('1.0', 'end')) 75 self.assertEqual(p.rawtext, self.text.get('1.0', 'end'))
89 76
90 # number of lines greater than 50 77 # number of lines greater than 50
91 self.text.insert('end', self.text.get('1.0', 'end')*4) 78 self.text.insert('end', self.text.get('1.0', 'end')*4)
92 p = self.get_parser('54.5') 79 p = self.get_parser('54.5')
93 80
94 def test_is_in_string(self): 81 def test_is_in_string(self):
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 self.assertEqual(p.get_expression(), "r'asdf'") 173 self.assertEqual(p.get_expression(), "r'asdf'")
187 174
188 p = get('9.17') 175 p = get('9.17')
189 with self.assertRaises(ValueError) as ve: 176 with self.assertRaises(ValueError) as ve:
190 p.get_expression() 177 p.get_expression()
191 self.assertIn('is inside a code', str(ve.exception)) 178 self.assertIn('is inside a code', str(ve.exception))
192 179
193 p = get('10.0') 180 p = get('10.0')
194 self.assertEqual(p.get_expression(), '') 181 self.assertEqual(p.get_expression(), '')
195 182
196 p = get('11.5') 183 p = get('11.3')
197 self.assertEqual(p.get_expression(), '') 184 self.assertEqual(p.get_expression(), '')
185
186 p = get('11.11')
187 self.assertEqual(p.get_expression(), 'False')
188
198 189
199 if __name__ == '__main__': 190 if __name__ == '__main__':
200 unittest.main(verbosity=2) 191 unittest.main(verbosity=2)
LEFTRIGHT

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