forked from bpython/bpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repl.py
More file actions
200 lines (151 loc) · 6.57 KB
/
test_repl.py
File metadata and controls
200 lines (151 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import unittest
from itertools import islice
from bpython import config, repl
class TestHistory(unittest.TestCase):
def setUp(self):
self.history = repl.History('#%d' % x for x in range(1000))
def test_is_at_start(self):
self.history.first()
self.assertNotEqual(self.history.index, 0)
self.assertTrue(self.history.is_at_end)
self.history.forward()
self.assertFalse(self.history.is_at_end)
def test_is_at_end(self):
self.history.last()
self.assertEqual(self.history.index, 0)
self.assertTrue(self.history.is_at_start)
self.assertFalse(self.history.is_at_end)
def test_first(self):
self.history.first()
self.assertFalse(self.history.is_at_start)
self.assertTrue(self.history.is_at_end)
def test_last(self):
self.history.last()
self.assertTrue(self.history.is_at_start)
self.assertFalse(self.history.is_at_end)
def test_back(self):
self.assertEqual(self.history.back(), '#999')
self.assertNotEqual(self.history.back(), '#999')
self.assertEqual(self.history.back(), '#997')
for x in range(997):
self.history.back()
self.assertEqual(self.history.back(), '#0')
def test_forward(self):
self.history.first()
self.assertEqual(self.history.forward(), '#1')
self.assertNotEqual(self.history.forward(), '#1')
self.assertEqual(self.history.forward(), '#3')
# 1000 == entries 4 == len(range(1, 3) ===> '#1000' (so +1)
for x in range(1000 - 4 - 1):
self.history.forward()
self.assertEqual(self.history.forward(), '#999')
def test_append(self):
self.history.append('print "foo\n"\n')
self.history.append('\n')
self.assertEqual(self.history.back(), 'print "foo\n"')
def test_enter(self):
self.history.enter('#lastnumber!')
self.assertEqual(self.history.back(), '#999')
self.assertEqual(self.history.forward(), '#lastnumber!')
def test_reset(self):
self.history.enter('#lastnumber!')
self.history.reset()
self.assertEqual(self.history.back(), '#999')
self.assertEqual(self.history.forward(), '')
class TestMatchesIterator(unittest.TestCase):
def setUp(self):
self.matches = ['bobby', 'bobbies', 'bobberina']
self.matches_iterator = repl.MatchesIterator(current_word='bob',
matches=self.matches)
def test_next(self):
self.assertEqual(self.matches_iterator.next(), self.matches[0])
for x in range(len(self.matches) - 1):
self.matches_iterator.next()
self.assertEqual(self.matches_iterator.next(), self.matches[0])
self.assertEqual(self.matches_iterator.next(), self. matches[1])
self.assertNotEqual(self.matches_iterator.next(), self.matches[1])
def test_previous(self):
self.assertEqual(self.matches_iterator.previous(), self.matches[2])
for x in range(len(self.matches) - 1):
self.matches_iterator.previous()
self.assertNotEqual(self.matches_iterator.previous(), self.matches[0])
self.assertEqual(self.matches_iterator.previous(), self.matches[1])
self.assertEqual(self.matches_iterator.previous(), self.matches[0])
def test_nonzero(self):
"""self.matches_iterator should be False at start,
then True once we active a match.
"""
self.assertFalse(self.matches_iterator)
self.matches_iterator.next()
self.assertTrue(self.matches_iterator)
def test_iter(self):
slice = islice(self.matches_iterator, 0, 9)
self.assertEqual(list(slice), self.matches * 3)
def test_current(self):
self.assertRaises(ValueError, self.matches_iterator.current)
self.matches_iterator.next()
self.assertEqual(self.matches_iterator.current(), self.matches[0])
def test_update(self):
slice = islice(self.matches_iterator, 0, 3)
self.assertEqual(list(slice), self.matches)
newmatches = ['string', 'str', 'set']
self.matches_iterator.update('s', newmatches)
newslice = islice(newmatches, 0, 3)
self.assertNotEqual(list(slice), self.matches)
self.assertEqual(list(newslice), newmatches)
class TestRepl(repl.Repl):
def __init__(self):
config_struct = config.Struct()
config.loadini(config_struct, os.devnull)
repl.Repl.__init__(self, repl.Interpreter(), config_struct)
self.input_line = ""
def current_line(self):
return self.input_line
class TestArgspec(unittest.TestCase):
def setUp(self):
self.repl = TestRepl()
self.repl.push("def spam(a, b, c):\n", False)
self.repl.push(" pass\n", False)
self.repl.push("\n", False)
def setInputLine(self, line):
"""Set current input line of the test REPL."""
self.repl.input_line = line
def test_func_name(self):
for (line, expected_name) in [("spam(", "spam"),
("spam(map([]", "map"),
("spam((), ", "spam")]:
self.setInputLine(line)
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__, expected_name)
def test_syntax_error_parens(self):
for line in ["spam(]", "spam([)", "spam())"]:
self.setInputLine(line)
# Should not explode
self.repl.get_args()
def test_kw_arg_position(self):
self.setInputLine("spam(a=0")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.argspec[3], "a")
self.setInputLine("spam(1, b=1")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.argspec[3], "b")
self.setInputLine("spam(1, c=2")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.argspec[3], "c")
def test_lambda_position(self):
self.setInputLine("spam(lambda a, b: 1, ")
self.assertTrue(self.repl.get_args())
self.assertTrue(self.repl.argspec)
# Argument position
self.assertEqual(self.repl.argspec[3], 1)
def test_name_in_assignment_without_spaces(self):
# Issue #127
self.setInputLine("x=range(")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__, "range")
def test_nonexistent_name(self):
self.setInputLine("spamspamspam(")
self.assertFalse(self.repl.get_args())
if __name__ == '__main__':
unittest.main()