-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
list_test.py
executable file
·271 lines (225 loc) · 7.95 KB
/
list_test.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python
from parser_test_helper import *
from nodes import *
context.use_tree = False
class ListTest(ParserBaseTest, unittest.TestCase):
def setUp(self):
parser.clear()
# # context.use_tree=False
# parser.do_interpret()
# super(ListTest, self).setUp()
# super(ParserBaseTest, self).setUp()
def test_natural_array_index(self):
assert_equals(parse('second element in [1,2,3]'), 2)
def test_natural_array_index_var(self):
parse('x=[1,2,3]')
assert_equals(parse('third element in x'), 3)
def test_lists(self):
x = parse('1 , 2 , 3')
assert_equals(x, [1, 2, 3])
x = parse('1,2,3')
assert_equals(x, [1, 2, 3])
x = parse('[1,2,3]')
assert_equals(x, [1, 2, 3])
x = parse('{1,2,3}')
assert_equals(x, [1, 2, 3])
x = parse('1,2 and 3')
assert_equals(x, [1, 2, 3])
x = parse('[1,2 and 3]')
assert_equals(x, [1, 2, 3])
x = parse('{1,2 and 3}')
assert_equals(x, [1, 2, 3])
def test_type0(self):
init('1 , 2 , 3')
assert_equals(parser.liste(), [1, 2, 3])
init('1,2,3')
assert_equals(parser.liste(), [1, 2, 3])
init('[1,2,3]')
assert_equals(parser.liste(), [1, 2, 3])
init('{1,2,3}')
assert_equals(parser.liste(), [1, 2, 3])
init('1,2 and 3')
assert_equals(parser.liste(), [1, 2, 3])
init('[1,2 and 3]')
assert_equals(parser.liste(), [1, 2, 3])
init('{1,2 and 3}')
assert_equals(parser.liste(), [1, 2, 3])
def test_list_methods(self):
parse('invert [1,2,3]')
assert_equals(result(), [3, 2, 1])
def test_error(self):
assert_has_error("first item in 'hi,'you' is 'hi'")
assert_has_error("first item in 'hi,'you' is 'hi'")
def test_last(self):
self.assert_that("last item in 'hi','you' is equal to 'you'")
self.assert_that("last item in 'hi','you' is equal to 'you'")
def test_select2(self):
self.assert_that("first item in 'hi','you' is 'hi'")
self.assert_that("second item in 'hi','you' is 'you'")
self.assert_that("last item in 'hi','you' is 'you'")
def test_select3(self):
assert_equals(parse("1st word of 'hi','you'"), 'hi')
self.assert_that("1st word of 'hi','you' is 'hi'")
self.assert_that("2nd word of 'hi','you' is 'you'")
self.assert_that("3rd word of 'hi','my','friend' is 'friend'")
def test_select4(self):
self.assert_that("first word of 'hi','you' is 'hi'")
self.assert_that("second word of 'hi','you' is 'you'")
self.assert_that("last word of 'hi','you' is 'you'")
def test_select5(self):
skip()
self.assert_that('numbers are 1,2,3. second number is 2')
self.assert_that('my friends are a,b and c. my second friend is b')
def test_select6(self):
self.assert_that("last character of 'howdy' is 'y'")
self.assert_that("first character of 'howdy' is 'h'")
self.assert_that("second character of 'howdy' is 'o'")
def test_list_syntax(self):
self.assert_that('1,2 is [1,2]')
self.assert_that('1,2 is {1,2}')
self.assert_that('1,2 == [1,2]')
self.assert_that('[1,2] is {1,2}')
self.assert_that('1,2 = [1,2]')
def test_list_syntax2(self):
self.assert_that('1,2,3 is the same as [1,2,3]')
self.assert_that('1,2 and 3 is the same as [1,2,3]')
self.assert_that('1,2 and 3 are the same as [1,2,3]')
self.assert_that('1,2 and 3 is [1,2,3]')
def test_concatenation(self):
parse('x is 1,2,3;y=4,5,6')
assert (equals([1, 2, 3], the.variableValues['x'], ))
assert (equals(3, len(the.variableValues['y'], ), ))
init('x + y')
z = parser.algebra()
if context.use_tree:
z=parser.eval_ast(z)
assert_equals(len(z), 6)
z = parse('x + y')
assert_equals(len(z), 6)
assert_equals(len(result(), ), 6)
z = parse('x plus y')
assert_equals(len(z), 6)
def test_concatenation_plus(self):
parse('x is 1,2;y=3,4')
z = parse('x plus y')
assert_equals(z, [1, 2, 3, 4])
def test_concatenation2(self):
parser.clear()
parse('x is 1,2,3;y=4,5,6')
parse('x + y')
assert (equals(6, len(result(), )))
parse('z is x + y')
assert_equals(the.variables['z'], [1, 2, 3, 4, 5, 6])
def test_concatenation2c(self):
# raise Exception("SERIOUS BUG: LOOP!")
# skip()
parse('x is 1,2\n y is 3,4\n z is x + y')
assert_equals(the.variables['z'], [1, 2, 3, 4])
def test_concatenation3(self):
variables['x'] = [1, 2]
variables['y'] = [3, 4]
init('x + y == 1,2,3,4')
parser.condition()
assert ('x + y == 1,2,3,4')
assert_equals(parse('x plus y'), [1, 2, 3, 4])
assert ('x plus y == [1,2,3,4]')
def test_concatenation4(self):
assert ('1,2 and 3 == 1,2,3')
assert ('1,2 and 3 == 1,2,3')
def test_type1(self):
init('class of 1,2,3')
parser.evaluate_property()
assert_equals(result(), list)
def test_type1b(self):
init('class of [1,2,3]')
parser.expression()
assert_equals(result(), list)
def test_type1c(self):
skip()
parse('class of 1,2,3')
assert_equals(result(), list)
def test_type2(self):
parse('x=1,2,3;class of x')
assert_equals(result(), list)
def test_type(self):
self.setUp()
parse('x=1,2,3;')
assert ('type of x is Array')
def test_type3(self):
parse('x be 1,2,3;y= class of x')
assert (equals(list, the.variables['y'].value))
assert (isinstance(the.variables['x'].value, list))
assert_equals(the.variables['x'].type, list)
def test_type3b(self):
parse('x be 1,2,3;y= class of x')
assert_equals(type(the.variableValues['x']), list)
# assert_equals(kind(the.variableValues['x'], list)
assert_equals(the.variables['y'].value, list)
self.assert_that('y is a Array')
self.assert_that('y is an Array')
self.assert_that('y is Array')
self.assert_that('Array == class of x')
self.assert_that('class of x is Array')
self.assert_that('kind of x is Array')
self.assert_that('type of x is Array')
def test_type4(self):
variables['x'] = Variable({'name': 'x', 'value': [1, 2, 3], })
self.assert_that('class of x is Array')
self.assert_that('kind of x is Array')
self.assert_that('type of x is Array')
def test_len(self):
variables['xs'] = Variable({'value': [1, 2, 3], 'name': 'xs', })
self.assert_that('length of xs is 3')
self.assert_that('size of xs is 3')
self.assert_that('count of xs is 3')
def test_map(self):
# parse("def square x:x*x")
assert_equals(parse('square [2,3,4]'), [4, 9, 16])
assert_equals(parse('square [1,2 and 3]'), [1, 4, 9])
def test_and(self):
skip()
self.assert_that('square of 1,2 and 3 == 1,4,9')
assert_equals(parse('square 1,2,3'), [1, 4, 9])
assert_equals(parse('square 1,2 and 3'), [1, 4, 9])
def test_and2(self):
skip()
# parse("def square x:x*x")
# parse("def square(x:int)->int:x*x")
# parse("def square(xs:list)->list:square all in xs")
assert_result_is('square 1,2 and 3', [1, 4, 9])
self.assert_that('square of [1,2 and 3] equals 1,4,9')
def test_and3(self):
skip()
parse('assert square of [1,2 and 3] equals 1,4,9')
def test_and4(self):
skip()
self.assert_that('square 1,2 and 3 == 1,4,9')
def test_every_selector(self):
# DAMN PLURAL!
self.assert_result_is('all letter in 1,"a",2,"c",3', ["a", "c"])
self.assert_that('(all letter in 1,"a",2,"c",3) == "a","c"')
# self.assert_result_is('all letters in 1,"a",2,"c",3',["a","c"])
# self.assert_that('(all letters in 1,"a",2,"c",3) == "a","c"')
def test_every_selector2(self):
self.assert_that('(all number in 1,"a",2,3) == 1,2,3')
self.assert_that('(all numbers in 1,"a",2,3) == 1,2,3')
self.assert_that('(every number in 1,"a",2,3) == 1,2,3')
def test_every_selector_no_braces(self):
self.assert_that('all number in 1,"a",2,3 == 1,2,3')
self.assert_that('all numbers in 1,"a",2,3 == 1,2,3')
self.assert_that('every number in 1,"a",2,3 == 1,2,3')
def test_map3(self):
skip()
self.assert_that('square every number in 1,"a",2,3 == 1,4,9')
# wrong operator precedence:
# square(number, [1,"a",2,3]) vs
# square(numbers in [1,"a",2,3])
def test_map4(self):
skip() # too hard
self.assert_that('add one to every number in 1,2,3 ==2,3,4')
self.assert_that("square every number in 1,'a',3 ==1,9")
def test_hasht(self):
init('{1,2,3}')
assert_equals(parser.liste(), [1, 2, 3])
init('{a:1,b:2,c:3}')
assert_equals(parser.hash_map(), {'b': 2, 'a': 1, 'c': 3, })