Report number of passed and failed tests
[voodoo-lang.git] / test / test_validator.rb
blob7806970b005e5bdb54f911e265a135361c8bea27
1 #! /usr/bin/env ruby
3 require File.dirname(__FILE__) + '/test'
4 require 'voodoo/validator'
6 include Voodoo::Validator
8 # Runs block and verifies that it throws a ValidationError.
9 # If the block doesn't throw such an exception,
10 # prints an error message and increments $errors.
11 def expect_ValidationError name, &block
12   expect_exception name, ValidationError, &block
13 end
15 def test_validator
16   [:add, :and, :asr, :bsr, :div, :mod, :mul,
17    :or, :rol, :ror, :shl, :shr, :sub, :xor].each do |binop|
18     expect_true("#{binop}_ints") {
19       validate_expression [binop, -14, 3]
20     }
22     expect_ValidationError("#{binop}_missing_arg") {
23       validate_expression [binop, 3]
24     }
26     expect_ValidationError("#{binop}_no_args") {
27       validate_expression [binop]
28     }
30     expect_ValidationError("#{binop}_too_many_args") {
31       validate_expression [binop, -14, 3, 28]
32     }
33   end
35   expect_true("align_no_parameters") {
36     validate_top_level [:align]
37   }
39   expect_ValidationError("align_no_integer") {
40     validate_top_level [:align, "wrong"]
41   }
43   expect_true("align_with_parameter") {
44     validate_top_level [:align, 16]
45   }
47   expect_ValidationError("align_too_many_parameters") {
48     validate_top_level [:align, 16, "wrong"]
49   }
51   expect_ValidationError("auto-bytes_missing_parameter") {
52     validate_expression [:'auto-bytes']
53   }
55   expect_true("auto-bytes_expression") {
56     validate_expression [:'auto-bytes', 23456]
57   }
59   expect_ValidationError("auto-bytes_too_many_parameters") {
60     validate_expression [:'auto-bytes', 23456, :oops]
61   }
63   expect_true("at_number_expression") {
64     validate_expression [:'@', 40]
65   }
67   expect_ValidationError("at_string_expression") {
68     validate_expression [:'@', "foo"]
69   }
71   expect_true("at_symbol_expression") {
72     validate_expression [:'@', :foo]
73   }
75   expect_ValidationError("block_expression") {
76     validate_expression [:block, [:call, :foo]]
77   }
79   expect_true("block_statement") {
80     validate_statement [:block, [:call, :foo]]
81   }
83   expect_true("block_level") {
84     validate_top_level [:block, [:call, :foo]]
85   }
87   expect_true("byte_top_level") {
88     validate_top_level [:byte, 42]
89   }
91   expect_ValidationError("byte_top_level_no_integer") {
92     validate_top_level [:byte, "wrong"]
93   }
95   [:call, :"tail-call"].each do |call|
96     expect_true("#{call}_expression") {
97       validate_expression [call, :foo]
98     }
100     expect_true("#{call}_expression_multiple_parameters") {
101       validate_expression [call, :foo, :bar, 42]
102     }
104     expect_true("#{call}_statement") {
105       validate_statement [call, :foo]
106     }
108     expect_true("#{call}_top_level") {
109       validate_top_level [call, :foo]
110     }
112     expect_ValidationError("#{call}_without_parameters") {
113       validate_expression [call]
114     }
115   end
117   expect_ValidationError("function_as_expression") {
118     validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
119   }
121   expect_ValidationError("function_as_statement") {
122     validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
123   }
125   expect_ValidationError("function_missing_formals") {
126     validate_top_level [:function]
127   }
129   expect_true("function_ok") {
130     validate_top_level [:function, [:x, :y], [:return, [:add, :x, :y]]]
131   }
133   [:byte, :word].each do |thing|
134     expect_true("get-#{thing}_expression") {
135       validate_expression [:"get-#{thing}", :foo, 12]
136     }
138     expect_ValidationError("get-#{thing}_expression_missing_parameter") {
139       validate_expression [:"get-#{thing}", :foo]
140     }
142     expect_ValidationError("get-#{thing}_expression_no_parameters") {
143       validate_expression [:"get-#{thing}"]
144     }
146     expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
147       validate_expression [:"get-#{thing}", :foo, 12, 13]
148     }
150     expect_ValidationError("get-#{thing}_statement") {
151       validate_statement [:"get-#{thing}", :foo, 12]
152     }
154     expect_ValidationError("set-#{thing}_expression") {
155       validate_expression [:"set-#{thing}", :foo, 12, 18]
156     }
158     expect_true("set-#{thing}_statement") {
159       validate_statement [:"set-#{thing}", :foo, 12, 18]
160     }
162     expect_ValidationError("set-#{thing}_statement_missing_parameters") {
163       validate_statement [:"set-#{thing}", :foo]
164     }
166     expect_ValidationError("set-#{thing}_statement_no_parameters") {
167       validate_statement [:"set-#{thing}"]
168     }
170     expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
171       validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
172     }
173   end
175   expect_ValidationError("goto_expression") {
176     validate_expression [:goto, 8888]
177   }
179   expect_true("goto_statement_int") {
180     validate_statement [:goto, 8888]
181   }
183   expect_true("goto_statement_label") {
184     validate_statement [:goto, :foo]
185   }
187   expect_ValidationError("goto_statement_no_parameters") {
188     validate_statement [:goto]
189   }
191   expect_ValidationError("goto_statement_too_many_parameters") {
192     validate_statement [:goto, :foo, 42]
193   }
195   expect_true("goto_top_level") {
196     validate_top_level [:goto, :foo]
197   }
199   [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
200     expect_ValidationError("#{cnd}_expression") {
201       validate_expression [cnd, [:x, :y], [[:call, :foo]]]
202     }
204     expect_true("#{cnd}_else_statement") {
205       validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
206     }
208     expect_true("#{cnd}_statement") {
209       validate_statement [cnd, [:x, :y], [[:call, :foo]]]
210     }
212     expect_true("#{cnd}_top_level") {
213       validate_top_level [cnd, [:x, :y], [[:call, :foo]]]
214     }
216     expect_ValidationError("let_inside_#{cnd}_statement") {
217       validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
218     }
220     expect_true("let_inside_block_inside_#{cnd}_statement") {
221       validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
222     }
223   end
225   [:export, :import].each do |directive|
226     expect_true("#{directive}_top_level") {
227       validate_top_level [directive, :foo]
228     }
230     expect_true("#{directive}_top_level_multiple_parameters") {
231       validate_top_level [directive, :foo, :bar, :baz]
232     }
234     expect_ValidationError("#{directive}_top_level_no_parameters") {
235       validate_top_level [directive]
236     }
238     expect_ValidationError("#{directive}_statement") {
239       validate_statement [directive, :foo]
240     }
241   end
243   expect_true("label_statement") {
244     validate_statement [:label, :foo]
245   }
247   expect_ValidationError("label_statement_no_parameters") {
248     validate_statement [:label]
249   }
251   expect_ValidationError("label_statement_too_many_parameters") {
252     validate_statement [:label, :foo, 18]
253   }
255   expect_ValidationError("label_statement_parameter_no_symbol") {
256     validate_statement [:label, 18]
257   }
259   expect_true("label_top_level") {
260     validate_top_level [:label, :foo]
261   }
263   expect_true("let_inside_block") {
264     validate_top_level [:block, [:let, :foo, 42], [:call, :foo]]
265   }
267   expect_true("let_inside_function") {
268     validate_top_level [:function, [:n],
269                        [:let, :foo, [:mul, :n, :n]],
270                        [:call, :foo]]
271   }
273   expect_true("let_statement_int") {
274     validate_statement [:let, :x, 12]
275   }
277   expect_true("let_statement_expression") {
278     validate_statement [:let, :x, [:add, 12, 3]]
279   }
281   expect_ValidationError("let_statement_no_symbol") {
282     validate_statement [:let, 12, 12]
283   }
285   expect_ValidationError("let_statement_without_parameters") {
286     validate_statement [:let]
287   }
289   expect_true("int_is_expression") {
290     validate_expression 12
291   }
293   expect_ValidationError("no_symbol_expression") {
294     validate_expression ["wrong"]
295   }
297   expect_ValidationError("no_symbol_statement") {
298     validate_statement ["wrong"]
299   }
301   expect_ValidationError("no_symbol_top_level") {
302     validate_top_level ["wrong"]
303   }
305   expect_ValidationError("no_array_statement") {
306     validate_statement :wrong
307   }
309   expect_ValidationError("no_array_top_level") {
310     validate_top_level :wrong
311   }
313   expect_true("not_expression") {
314     validate_expression [:not, :x]
315   }
317   expect_ValidationError("not_expression_no_parameters") {
318     validate_expression [:not]
319   }
321   expect_ValidationError("not_expression_too_many_parameters") {
322     validate_expression [:not, :x, :y]
323   }
325   expect_ValidationError("section_missing_name") {
326     validate_top_level [:section]
327   }
329   expect_ValidationError("section_number") {
330     validate_top_level [:section, 12]
331   }
333   expect_true("section_string") { validate_top_level [:section, :code] }
335   expect_true("section_symbol") { validate_top_level [:section, :code] }
337   expect_true("set_statement_int") {
338     validate_statement [:set, :x, 12]
339   }
341   expect_ValidationError("set_statement_no_symbol") {
342     validate_statement [:set, 12, 12]
343   }
345   expect_true("set_statement_expression") {
346     validate_statement [:set, :x, [:add, 12, 3]]
347   }
349   expect_ValidationError("set_statement_without_parameters") {
350     validate_statement [:set]
351   }
353   expect_true("string_top_level") {
354     validate_top_level [:string, "test"]
355   }
357   expect_ValidationError("string_top_level_no_string") {
358     validate_top_level [:string, 42]
359   }
361   expect_true("symbol_is_expression") {
362     validate_expression :foo
363   }
365   expect_true("word_top_level") {
366     validate_top_level [:word, 42]
367   }
369   expect_ValidationError("word_top_level_no_integer") {
370     validate_top_level [:word, "wrong"]
371   }
374 if $0 == __FILE__
375   test_validator
376   exit report_test_results