Added support for at-expressions to validator
[voodoo-lang.git] / test / test_validator.rb
blob529dfdedfba93efbf89e642469b54f47c8530775
1 #! /usr/bin/env ruby
3 require 'voodoo/validator'
5 include Voodoo::Validator
7 $errors = 0
9 # Runs block and verifies that it returns true.
10 # If it doesn't, prints an error message and increments $errors.
11 def expect_true name, &block
12   begin
13     print "#{name}..."
14     result = yield
15     if result
16       puts "pass"
17       true
18     else
19       puts "FAIL: expected true, but got #{result.inspect}"
20       $errors = $errors + 1
21     end
22   rescue Exception => e
23     puts "FAIL: Unexpected #{e.class}\n#{e.message}"
24     $errors = $errors + 1
25   end
26 end
28 def expect_exception name, exception_type, &block
29   begin
30     print "#{name}..."
31     yield
32     puts "FAIL: Expected exception of type #{exception_type}," +
33       " but no exception was raised"
34     $errors = $errors + 1
35   rescue Exception => e
36     if e.kind_of? exception_type
37       puts "pass"
38       true
39     else
40       puts "FAIL: Expected #{exception_type}, but got #{e.class}"
41       $errors = $errors + 1
42     end
43   end
44 end
46 def expect_ValidationError name, &block
47   expect_exception name, ValidationError, &block
48 end
50 [:add, :and, :asr, :bsr, :div, :mod, :mul,
51  :or, :rol, :ror, :shl, :shr, :sub, :xor].each do |binop|
52   expect_true("#{binop}_ints") {
53     validate_expression [binop, -14, 3]
54   }
56   expect_ValidationError("#{binop}_missing_arg") {
57     validate_expression [binop, 3]
58   }
60   expect_ValidationError("#{binop}_no_args") {
61     validate_expression [binop]
62   }
64   expect_ValidationError("#{binop}_too_many_args") {
65     validate_expression [binop, -14, 3, 28]
66   }
67 end
69 expect_true("align_no_parameters") {
70   validate_toplevel [:align]
73 expect_ValidationError("align_no_integer") {
74   validate_toplevel [:align, "wrong"]
77 expect_true("align_with_parameter") {
78   validate_toplevel [:align, 16]
81 expect_ValidationError("align_too_many_parameters") {
82   validate_toplevel [:align, 16, "wrong"]
85 expect_true("at_number_expression") {
86   validate_expression [:'@', 40]
89 expect_ValidationError("at_string_expression") {
90   validate_expression [:'@', "foo"]
93 expect_true("at_symbol_expression") {
94   validate_expression [:'@', :foo]
97 expect_ValidationError("block_expression") {
98   validate_expression [:block, [:call, :foo]]
101 expect_true("block_statement") {
102   validate_statement [:block, [:call, :foo]]
105 expect_true("block_level") {
106   validate_toplevel [:block, [:call, :foo]]
109 expect_true("byte_toplevel") {
110   validate_toplevel [:byte, 42]
113 expect_ValidationError("byte_toplevel_no_integer") {
114   validate_toplevel [:byte, "wrong"]
117 [:call, :"tail-call"].each do |call|
118   expect_true("#{call}_expression") {
119     validate_expression [call, :foo]
120   }
122   expect_true("#{call}_expression_multiple_parameters") {
123     validate_expression [call, :foo, :bar, 42]
124   }
126   expect_true("#{call}_statement") {
127     validate_statement [call, :foo]
128   }
130   expect_true("#{call}_toplevel") {
131     validate_toplevel [call, :foo]
132   }
134   expect_ValidationError("#{call}_without_parameters") {
135     validate_expression [call]
136   }
139 expect_ValidationError("function_as_expression") {
140   validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
143 expect_ValidationError("function_as_statement") {
144   validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
147 expect_ValidationError("function_missing_formals") {
148   validate_toplevel [:function]
151 expect_true("function_ok") {
152   validate_toplevel [:function, [:x, :y], [:return, [:add, :x, :y]]]
155 [:byte, :word].each do |thing|
156   expect_true("get-#{thing}_expression") {
157     validate_expression [:"get-#{thing}", :foo, 12]
158   }
160   expect_ValidationError("get-#{thing}_expression_missing_parameter") {
161     validate_expression [:"get-#{thing}", :foo]
162   }
164   expect_ValidationError("get-#{thing}_expression_no_parameters") {
165     validate_expression [:"get-#{thing}"]
166   }
168   expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
169     validate_expression [:"get-#{thing}", :foo, 12, 13]
170   }
172   expect_ValidationError("get-#{thing}_statement") {
173     validate_statement [:"get-#{thing}", :foo, 12]
174   }
176   expect_ValidationError("set-#{thing}_expression") {
177     validate_expression [:"set-#{thing}", :foo, 12, 18]
178   }
180   expect_true("set-#{thing}_statement") {
181     validate_statement [:"set-#{thing}", :foo, 12, 18]
182   }
184   expect_ValidationError("set-#{thing}_statement_missing_parameters") {
185     validate_statement [:"set-#{thing}", :foo]
186   }
188   expect_ValidationError("set-#{thing}_statement_no_parameters") {
189     validate_statement [:"set-#{thing}"]
190   }
192   expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
193     validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
194   }
197 expect_ValidationError("goto_expression") {
198   validate_expression [:goto, 8888]
201 expect_true("goto_statement_int") {
202   validate_statement [:goto, 8888]
205 expect_true("goto_statement_label") {
206   validate_statement [:goto, :foo]
209 expect_ValidationError("goto_statement_no_parameters") {
210   validate_statement [:goto]
213 expect_ValidationError("goto_statement_too_many_parameters") {
214   validate_statement [:goto, :foo, 42]
217 expect_true("goto_toplevel") {
218   validate_toplevel [:goto, :foo]
221 [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
222   expect_ValidationError("#{cnd}_expression") {
223     validate_expression [cnd, [:x, :y], [[:call, :foo]]]
224   }
226   expect_true("#{cnd}_else_statement") {
227     validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
228   }
230   expect_true("#{cnd}_statement") {
231     validate_statement [cnd, [:x, :y], [[:call, :foo]]]
232   }
234   expect_true("#{cnd}_toplevel") {
235     validate_toplevel [cnd, [:x, :y], [[:call, :foo]]]
236   }
238   expect_ValidationError("let_inside_#{cnd}_statement") {
239     validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
240   }
242   expect_true("let_inside_block_inside_#{cnd}_statement") {
243     validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
244   }
247 [:export, :import].each do |directive|
248   expect_true("#{directive}_toplevel") {
249     validate_toplevel [directive, :foo]
250   }
252   expect_true("#{directive}_toplevel_multiple_parameters") {
253     validate_toplevel [directive, :foo, :bar, :baz]
254   }
256   expect_ValidationError("#{directive}_toplevel_no_parameters") {
257     validate_toplevel [directive]
258   }
260   expect_ValidationError("#{directive}_statement") {
261     validate_statement [directive, :foo]
262   }
265 expect_true("label_statement") {
266   validate_statement [:label, :foo]
269 expect_ValidationError("label_statement_no_parameters") {
270   validate_statement [:label]
273 expect_ValidationError("label_statement_too_many_parameters") {
274   validate_statement [:label, :foo, 18]
277 expect_ValidationError("label_statement_parameter_no_symbol") {
278   validate_statement [:label, 18]
281 expect_true("label_toplevel") {
282   validate_toplevel [:label, :foo]
285 expect_true("let_inside_block") {
286   validate_toplevel [:block, [:let, :foo, 42], [:call, :foo]]
289 expect_true("let_inside_function") {
290   validate_toplevel [:function, [:n],
291                      [:let, :foo, [:mul, :n, :n]],
292                      [:call, :foo]]
295 expect_true("let_statement_int") {
296   validate_statement [:let, :x, 12]
299 expect_true("let_statement_expression") {
300   validate_statement [:let, :x, [:add, 12, 3]]
303 expect_ValidationError("let_statement_no_symbol") {
304   validate_statement [:let, 12, 12]
307 expect_ValidationError("let_statement_without_parameters") {
308   validate_statement [:let]
311 expect_true("int_is_expression") {
312   validate_expression 12
315 expect_ValidationError("no_symbol_expression") {
316   validate_expression ["wrong"]
319 expect_ValidationError("no_symbol_statement") {
320   validate_statement ["wrong"]
323 expect_ValidationError("no_symbol_toplevel") {
324   validate_toplevel ["wrong"]
327 expect_ValidationError("no_array_statement") {
328   validate_statement :wrong
331 expect_ValidationError("no_array_toplevel") {
332   validate_toplevel :wrong
335 expect_true("not_expression") {
336   validate_expression [:not, :x]
339 expect_ValidationError("not_expression_no_parameters") {
340   validate_expression [:not]
343 expect_ValidationError("not_expression_too_many_parameters") {
344   validate_expression [:not, :x, :y]
347 expect_ValidationError("section_missing_name") {
348   validate_toplevel [:section]
351 expect_ValidationError("section_number") {
352   validate_toplevel [:section, 12]
355 expect_true("section_string") { validate_toplevel [:section, :code] }
357 expect_true("section_symbol") { validate_toplevel [:section, :code] }
359 expect_true("set_statement_int") {
360   validate_statement [:set, :x, 12]
363 expect_ValidationError("set_statement_no_symbol") {
364   validate_statement [:set, 12, 12]
367 expect_true("set_statement_expression") {
368   validate_statement [:set, :x, [:add, 12, 3]]
371 expect_ValidationError("set_statement_without_parameters") {
372   validate_statement [:set]
375 expect_true("string_toplevel") {
376   validate_toplevel [:string, "test"]
379 expect_ValidationError("string_toplevel_no_string") {
380   validate_toplevel [:string, 42]
383 expect_true("symbol_is_expression") {
384   validate_expression :foo
387 expect_true("word_toplevel") {
388   validate_toplevel [:word, 42]
391 expect_ValidationError("word_toplevel_no_integer") {
392   validate_toplevel [:word, "wrong"]
395 if $errors == 0
396   puts "All tests passed"
397   exit
398 else
399   puts "#{$errors} tests failed"
400   exit 1