-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
function_test.py
executable file
·273 lines (209 loc) · 8.59 KB
/
function_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
272
273
#!/usr/bin/env python
from parser_test_helper import *
import nodes
from nodes import *
class FunctionTest(ParserBaseTest):
# TURNED INTO EMITTER TEST, cause BODY is better done via AST!!
def setUp(self):
parser.clear()
if 'use_tree' in os.environ:
print("NO FunctionTest in interpreter mode (yet)")
skip()
# context.use_tree = True
# context.interpret = False
# context._verbose = True never set manually here!
def test_identity2(self):
parse("define identity number n\n\tn\nassert identity(1) is 1")
# parse("define identity number n\nn\nend\nassert identity(1) is 1")
def test_assert(self):
self.assert_that("assert 1+1==2")
self.assert_that("1+1==2")
parse("assert 1+1==2")
def test_assert2(self):
self.assert_that("assert 2 + 4 is 6")
def test_add2(self):
parse("define add2 number n\nn+2\nend\nassert add2(1) is 3")
def test_short_add2(self):
parse("add2:=it+2\nassert add2(1) is 3")
def test_as(self):
assert_result_is("'41' as int + 1", 42)
def test_string_add(self):
assert_result_is("'41' + 1", 411) # todo warn via IDE
assert_result_is("1 + '41' ", 141)
assert_result_is("2 * '41' ", 4141)
assert_result_is("'41' * 2 ", 4141)
assert_equals(41,"41","BAD TRANSIENCY")
# assert_equals(411,"41"+1,"BAD TRANSIENCY")
# assert_result_is("'41' as int + 1 is 42")
# parse("define add number n\nn+2\nend\nassert add(1,2) is 3")
# parse("define add number n to number m\nn+2\nend\nassert add(1,2) is 3")
# parse("define add number n to number m\nn+m\nend\nassert add(1,2) is 3")
def test_add3(self):
parse("define add number n to number m\nn+m\nend\nassert add 2 to 4 is 6")
def test_opencv(self):
i = parse("to create a fullscreen window with name n: return cv2.namedWindow(n, cv.CV_WINDOW_FULLSCREEN)")
fbody = []
f = FunctionDef(name="create fullscreen window", arguments=[Argument(name="n")], body=fbody)
assert_equals(the.result, f)
parse("create a fullscreen window with name \"test\"")
def test_dedent(self):
parse("define identity number n\n\tn\nassert identity(1) is 1")
# parse("define fibonacci number n\nn+1\nend\nassert fibonacci(1) is 2")
def test_fibonacci_0(self):
parse(
"define fibonacci number n\nif n<2 then 1 else fibonacci(n-1)+ fibonacci(n-2)\nend\n assert fibonacci(5) is 8")
# parse("define fibonacci number n\nif n<2 then 1 else fibonacci n-1 + fibonacci n-2\nend")
def test_fibonacci(self):
dir = 'samples/'
code = read(dir + ('fibonacci.e'))
code = fix_encoding(code)
print(code)
print((parse(code)))
copy_variables()
fib = the.methods['fibonacci']
print(fib)
assert (equals('n', fib.arguments[0].name)) # name(args[0], )))
# assert (equals('number', fib.arguments[0].name)) # name(args[0], )))
assert_equals(parse('fibonacci of 10'), 89)
print((parse('assert fibonacci of 10 is 89'))) # 55
# f10 = fib.call(10)
# print(f10)
# assert_equals(f10, 89)
def test_identity(self):
dir = 'samples/'
code = read(dir + ('identity.e'))
code = fix_encoding(code)
print(code)
print((parse(code)))
identity = the.methods['identity']
assert (equals('x', identity.arguments[0].name))
print(identity)
print((identity.call(5)))
print((identity(5)))
assert (equals(5, identity(5)))
print((parse('identity(5)')))
assert ('identity(5) is 5')
def test_basic_syntax(self):
assert_result_is("print 'hi'", 'hi')
assert_result_is("print 'hi'", 'hi')
def test_complex_syntax(self):
init('here is how to define a method: done')
def test_block(self):
parser.clear()
the.variables['x'] = Variable(name='x', value=1)
the.variables['y'] = Variable(name='y', value=2)
z = parse('x+y;')
assert_equals(z, 3)
print(parser.variables)
assert_equals(len(parser.variables), 2+2) # it,__builtins__ !?
def test_go_threads(self):
parse('go print "hi"')
# Module([Import([alias('threading', None)]), Assign([Name('t', Store())], Call(Attribute(Name('threading', Load()), 'Thread', Load()), [], [keyword('target', Name('a', Load()))], None, None)), Expr(Call(Attribute(Name('t', Load()), 'start', Load()), [], [], None, None))])
# Module([Import([alias('threading', None)]), Assign([Name('_t', Store())], Call(Attribute(Name('threading', Load()), 'Thread', Load()), [keyword('target', Name('_tmp', Load()))], [], None, None)), Assign([Name('it', Store())], Call(Attribute(Name('_t', Load()), 'start', Load()), [], [], None, None)), Print(None, [Name('it', Load())], True)])
def test_params(self):
parse('how to increase x by y: x+y;')
g = the.methods['increase']
# big python headache: starting from position 0 or 1 ?? (self,x,y) etc
# args = [Argument({'name': 'x', 'preposition': None, 'position': 1, }),
# Argument({'preposition': 'by', 'name': 'y', 'position': 2, })]
args = [Argument({'name': 'x', 'preposition': None, 'position': 0, }),
Argument({'preposition': 'by', 'name': 'y', 'position': 1, })]
f = FunctionDef({'body': 'x+y;', 'name': 'increase', 'arguments': args, })
assert_equals(f, g)
return f
def test_params_call(self):
f = self.test_params()
assert_equals(parser.do_send(f, {'x': 1, 'y': 2, }), 3)
def test_function_object(self):
parse('how to increase a number x by y: x+y;')
g = the.methods['increase']
arg1 = Argument({'type': 'number', 'position': 1, 'name': 'x', 'preposition': None, })
arg2 = Argument({'name': 'y', 'preposition': 'by', 'position': 2, })
body = 'x+y;'
body = ast.BinOp(left=ast.Name('x'), op=ast.Add, right=ast.Name('y'))
f = FunctionDef({'arguments': arg2, 'name': 'increase', 'body': body, 'object': arg1, })
assert_equals(f, g)
assert_equals(parser.do_call_function(f, {'x': 1, 'y': 2, }), 3)
def test_blue_yay(self):
parser.do_interpret()
assert_result_is("def test{puts 'yay'};test", 'yay')
assert_result_is("def test{puts 'yay'};test", 'yay')
def test_class_method(self):
# parse('how to list all numbers smaller x: [1..x]')
# parse('to get numbers smaller x: [1..x]')
parse('to get numbers smaller x: return 1 to x - 1')
g = the.methods['get']
# g = the.methods['get numbers smaller eeek']
f = FunctionDef({'body': '[1..x]', 'name': 'list'}) # , 'arguments': arg2(), 'object': arg1(), })
assert_equals(f, g)
assert_equals(parser.call_function(f, 4), [1, 2, 3])
def test_simple_parameters(self):
parse("puts 'hi'")
def test_to_do_something(self):
pass
def test_svg(self):
skip()
parse('svg <circle cx="$x" cy="50" r="$radius" stroke="black" fill="$color" id="circle"/>')
parse('what is that')
def test_java_style(self):
parse('1.add(0)')#confused with 1.0 like ruby
assert_result_is('3.add(4)', 7)
def test_ruby_style(self):
parse('1..add(0)')
assert_result_is('3..add(4)', 7.)
def test_dot(self):
parse("x='hi'")
# assert_result_is('reverse of x', 'ih')
assert_result_is('x.reverse', 'ih')
# assert_result_is('reverse x', 'ih')
def test_rubyStyle(self):
parse('Math.hypot(3,4)')
parse('Math.sqrt 8')
parse('Math.sqrt( 8 )')
# parse('Math.e ~= 2.71828')
def test_x_name(self):
variables['x'] = nodes.Variable(name='x', value=7)
init('x')
assert_equals(name(parser.nod(), ), 'x')
def test_add_to_zero(self):
context.use_tree = False # not yet
parser.do_interpret()
parse('counter is zero; repeat three times: increase counter by 1; done repeating;')
assert_equals(variables['counter'], 3)
def test_var_check(self):
variables['counter'] = Variable(name='counter', value=3)
assert ('the counter is 3')
def test_array_arg(self):
assert_equals(parse('rest of [1,2,3]'), [2, 3])
def test_array_index(self):
assert_equals(parse('x=[1,2,3];x[2]'), 3)
def test_array_index_set(self):
parser.clear()
assert_equals(parse('x=[1,2,3];x[2]=0;x'), [1, 2, 0])
def test_x(self):
the.variables['x'] = Variable(name='x', value=1)
assert_equals(parse('x'), 1)
def test_natural_array_index_setter(self):
parse('x=[1,2,3]')
assert_equals(parse('set third element in x to 8'), 8)
assert_equals(parse('x'), [1, 2, 8])
def test_array_arg(self):
assert_equals(parse('rest of [1,2,3]'), [2, 3])
assert_equals(parse('rest of [1,2,3]'), [2, 3])
def test_add_time(self):
pass
def test_increase(self):
# todo: copying method invocation logic to AST
parse('counter is one; repeat three times: increase counter; done repeating;')
assert_equals(the.variableValues['counter'], 4)
def _test_svg_dom(self):
init('<svg><circle cx="$x" cy="50" r="$radius" stroke="black" fill="$color" id="circle"/></svg>')
# print(svg(parser.interpretation(), ))
parse('circle.color=green')
assert_equals('circle.color', 'green')
def test_incr(self):
assert ('increase 1 == 2')
assert ('increase 1 by 1 == 2')
assert ('x=1; x+1 == 2')
assert ('x=1; ++x == 2')
# assert ('1++ == 2')