forked from DiamondLightSource/diffcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ipython.py
More file actions
172 lines (131 loc) · 4.36 KB
/
test_ipython.py
File metadata and controls
172 lines (131 loc) · 4.36 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
from nose.tools import assert_sequence_equal # @UnresolvedImport
from nose.tools import eq_
from nose import SkipTest
try:
import IPython.core.magic # @UnusedImport
from diffcmd.ipython import parse
IPYTHON = True
except ImportError:
IPYTHON = False
class CallableNamedObject(object):
def __init__(self, name, val):
self._name = name
self._val = val
def method(self):
return self._val
def __repr__(self):
return self._name
def __call__(self):
return self._val
o1 = CallableNamedObject('o1', 1)
o2 = CallableNamedObject('o2', 2)
o3 = CallableNamedObject('o3', 3)
c = 4
d = {
'o1' : o1,
'o2' : o2,
'o3' : o3,
'c' : 4
}
def check(s, *expected):
assert_sequence_equal(parse(s, d), expected)
def assert_raises_syntax_error_with_message(msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
raise AssertionError()
except SyntaxError as e:
eq_(e.message, msg)
def setup_module():
if not IPYTHON:
raise SkipTest('ipython not available')
def test_parse_spaces():
check('')
check(' ')
check(' ')
def test_parse_commas():
assert_raises_syntax_error_with_message(
'unexpected comma', parse, '1 2, 3', d)
def test_parse_numbers():
check('1', 1)
check('10', 10)
check('10 2 3', 10, 2, 3)
def test_parse_negative_numbers():
check('-1', -1)
check('-10', -10)
check('-10 2 -3', -10, 2, -3)
def test_parse_negative_number_with_space_between_sign_and_digit():
msg = 'could not evaluate: "-"'
assert_raises_syntax_error_with_message(msg, parse, '- 1', d)
assert_raises_syntax_error_with_message(msg, parse, '1 - 1', d)
def test_parse_strings():
check("'s1'", 's1')
check("'s1' 's2'", 's1', 's2')
check('"s1"', 's1')
check('"s1" "s2"', 's1', 's2')
def test_parse_strings_containing_spaces():
check("'ab cd'", 'ab cd')
check("' ab cd '", ' ab cd ')
def test_parse_strings_containing_commas():
check("'ab,cd'", 'ab,cd')
check("', ab, cd, '", ', ab, cd, ')
def test_parse_strings_containing_hashes():
check("'ab #cd'", 'ab #cd')
check("' ab cd# '", ' ab cd# ')
def test_parse_strings_containing_strings():
check(''' 1 'ab cd' 2''', 1, 'ab cd', 2)
check(''' 1 '"ab" cd' 2''', 1, '"ab" cd', 2)
def test_parse_strings_containing_square_brackets():
check("'ab[cd'", 'ab[cd')
check("'[ ab cd ]'", '[ ab cd ]')
def test_parse_objects_and_numbers():
check('o1', o1)
check('c', 4)
check('o1 o2', o1, o2)
check('1 o1 o2 2 c', 1, o1, o2, 2, 4)
def test_parse_numbers_with_math():
check('1+1', 2)
check('-1 1-1 1 1+1', -1, 0, 1, 2)
def test_parse_callables_and_methods():
check('o1()', 1)
check('o1.method()', 1)
def test_parse_objects_numbers_and_strings():
check(" o1 's2'", o1, 's2')
check("1 's1' 's2' -2 o2", 1, 's1', 's2', -2, o2)
def test_parse_objects_and_numbers_and_extra_spaces():
check(' o1 ', o1)
check(' o1 o2 ', o1, o2)
check('1 o1 o2 -2 ', 1, o1, o2, -2)
def test_parse_lists():
check('[]', [])
check('[1]', [1])
check('[-1 2 3]', [-1, 2, 3])
check('[1 2 -3][4]', [1, 2, -3], [4])
check('[1 2 3][4 -5]', [1, 2, 3], [4, -5])
def test_parse_nested_lists():
check('[[]]', [[]])
check('[[1]]', [[1]])
check('[[1 2 3]]', [[1, 2, 3]])
check('[[1 2 3] [4 5 6]]', [[1, 2, 3], [4, 5, 6]])
check('[[1 2 3][4 5 6]]', [[1, 2, 3], [4, 5, 6]])
check('[[1 2 3][4 5 [6 7]]]', [[1, 2, 3], [4, 5, [6, 7]]])
def test_parse_lists_and_others():
check('[[]]', [[]])
check('[[o1]]', [[o1]])
check('[[o1 o2() -3]]', [[o1, 2, -3]])
def test_parse_unexpected_list_closure():
msg = 'could not evaluate: "1, 2], 3"'
assert_raises_syntax_error_with_message(msg, parse, '1 2] 3', d)
assert_raises_syntax_error_with_message(msg, parse, '1 2 ] 3', d)
assert_raises_syntax_error_with_message(msg, parse, '1 2 ]3', d)
def test_parse_with_trailing_comments():
check('1# comment', 1)
check('1 # comment', 1)
check('1# comment # comment2', 1)
check('o1# comment', o1)
check('o1()# comment', 1)
check('-1# comment', -1)
check('1-1# comment', 0)
def test_parse_with_only_comments():
check('# comment')
check(' # comment')
check(' # comment')