forked from pannous/english-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.rb
executable file
·371 lines (309 loc) · 6.63 KB
/
parser_test.rb
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env ruby
$use_tree=false
# require_relative '../parser_base_test'
# require_relative '../test_helper'
require_relative '../test_helper'
#DIFFERENT TO OTHER TEST: TEST EnglishParser methods DIRECTLY!
class EnglishParserTestParser<EnglishParser
#@@parser=EnglishParser.new
def initialize
super
#@@parser=EnglishParser.new
end
def NOmethod_missing(sym, *args, &block) # <- NoMethodError use node.blah to get blah!
syms=sym.to_s
if @@parser and @@parser.methods.contains(sym)#(syms.end_with?"?")
x= maybe { @@parser.send(sym) } if args.count==0
x= maybe { @@parser.send(sym,args[0]) } if args.count==1
x= maybe { @@parser.send(sym, args) } if args.count>0
return x
end
super(sym, *args, &block)
end
def test_substitute_variables
@variableValues={"x" => 3}
assert(" 3 "== substitute_variables(' #{x} '))
assert('"3"'== substitute_variables('"#{x}"'))
assert(" 3 "== substitute_variables(" $x "))
assert("'3'"== substitute_variables("'$x'"))
assert("3"== substitute_variables('#{x}'))
assert do ("3"== substitute_variables("$x")) end
end
def test_default_setter_dont_overwrite
s "set color to blue; set default color to green"
setter
assert(@variableValues["color"]=="blue")
end
def test_default_setter
s "set the default color to green"
setter
assert(@variableValues.contains("color"))
end
# grammar : 'hello' QUESTION ('does'| QUESTION)* 'the world'? VERB
def test_root
s "hello who does the world end"
token "hello"
question
star {
maybe { token 'does' } || maybe { question }
}
_? 'the world'
assert verb
puts "Parsed successfully!"
end
def aa
puts "aa"
end
def bb
raise NotMatching.new(test)
#throw NotMatching.new NOT rescued!!!
end
def cc
puts "cc"
return "cc"
end
def dd
puts "dd"
throw "dd"
end
def test_any
s "a b c d"
one :aa, :bb, :cc
assert any {
maybe { puts "a" }
maybe { puts "b" }
maybe { raise NotMatching.new }
maybe { puts "c" }
maybe { throw "b" }
maybe { puts "b" }
}
end
def test_action
s "eat a sandwich; done"
#s "bash 'ls'"
#verb and nod
assert action
assert(!string.match("sandwich"))
end
def test_methods
test_method2
#test_method3
test_method4
end
def test_method
s "how to integrate a bug do test done"
assert method_definition
end
def test_method2
s "how to print: eat a sandwich; done"
assert method_definition
#any{method_definition}
end
def test_method3
s "how to integrate a bug\ntest\nok"
assert method_definition
end
def test_method4
s "how to integrate a bug
test
ok"
assert method_definition
end
def test_expression
s "eat a sandwich;"
assert action
puts x
end
def raise_test
raise "test"
end
def test_block
s "let the initial value of I be x;\nstep size is the length of the interval,
divided by the number of steps\nvar x = 8;"
block
end
def test_quote
s 'msg = "heee"'
setter
end
def test_while
allow_rollback
s "while i is smaller or less then y do
evaluate the function at point I
add the result to the sum
increase I by the step size
done"
looper
end
def test_setter3
s "step size is the length of the interval, divided by the number of steps"
setter
end
def test_setter2
s "var x = 8;"
setter
end
def test_setter
s "let the initial value of I be x"
setter
end
def test_looper
s "while i is smaller or less then y do\nyawn\nend"
looper
end
def test_method_call
s "evaluate the function at point I"
method_call
#action
end
def test_verb
s "help"
verb
end
def test_comment
s "#ok"
statement
s "z3=13 //ok"
assert statement
s "z4=23 -- ok"
assert statement
s "z5=33 # ok"
assert statement
s "z6=/* dfsfds */3 "
end
def test_js
s "js alert('hi')"
assert javascript
#puts @javascript
end
def test_ruby_method_call
test_ruby_def
parse "NOW CALL via english"
s "call ruby_block_test 'yeah'"
assert ruby_method_call
end
def test_ruby_def
s "def ruby_block_test x='yuhu'
puts x
return x+'yay'
end"
assert ruby_def
# ^^ defines:
ruby_block_test
end
def test_ruby_all
s "
def ruby_block_test x='yuhu'
puts x
return x+'yay'
end
call ruby_block_test 'yeah'
"
root
end
def test_ruby_variables
s "x=7;puts x;x+1;"
root
end
def test_ruby
s "def ruby_block_test
puts 'ooooo'
return 'yay'
end"
execute_ruby_block
# ^^ defines:
ruby_block_test
end
def test_algebra
s "2* ( 3 + 10 ) "
#s "2*(3+10)"
puts "Parse #{@string} as algebra?"
tree=algebra
assert tree
#assert @result==26
#assert{@result==26}
#puts eval good_node_values @root if @root #== @string
end
#ScriptError = Class.new StandardError
def assert x=nil, &block
x=yield if not x and block
#raise Exception.new (to_source(block)) if not x
raise ScriptError.new to_source(block) if not x
puts x
puts "!!OK!!"
end
#def assert x
# raise ScriptError if not x
# puts x
# puts "!!OK!!"
#end
def test_args
s "eat an mp3"
assert(endNode)
end
def test
puts "Starting tests!"
begin
s "a bug"
test_method3
test_method4
assert endNode
test_ruby_variables
test_args
test_algebra
test_ruby
test_ruby_def
test_ruby_method_call
test_ruby_all
#return
test_js
test_verb
test_setter2
test_setter3
test_comment
#star { arg }
test_block
test_quote
test_while
test_method_call
#test_methods
# test_looper
show_tree
puts "++++++++++++++++++\nPARSED successfully!"
rescue => e
error e
end
end
end
class EnglishParserTest < ParserBaseTest
@@testParser=EnglishParserTestParser.new
def initialize args
@parser=EnglishParserTestParser.new
super args
end
def self._test x
puts "NOT testing "+x.to_s
end
def test_all
@parser.methods.each{|m|
if m.to_s.start_with?"test"
@parser.send(m)
end
}
end
_test "setter" do
@parser.test_default_setter
@parser.test_default_setter_dont_overwrite
end
_test "substitute_variables" do
@parser.test_substitute_variables
#@@testParser.test_substitute_variables
assert "yay"
end
_test "jeannie" do
r= @parser.jeannie ("3 plus 3")
puts "jeannie : 3 plus 3 = "+r.to_s
assert(r=="6")
puts "OK!!!!!!"
end
end