reject programs that import symbols after using them
[voodoo-lang.git] / test / test_validator.rb
blobb6d2ce3b7ddf4722e26e0a1eb9bdf88a6347d817
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_in_function") {
126     validate_statement [:function, [],
127                         [:function, [], [:return, 0]], [:return, 0]]
128   }
130   expect_ValidationError("function_missing_formals") {
131     validate_top_level [:function]
132   }
134   expect_true("function_ok") {
135     validate_top_level [:function, [:x, :y], [:return, [:add, :x, :y]]]
136   }
138   [:byte, :word].each do |thing|
139     expect_true("get-#{thing}_expression") {
140       validate_expression [:"get-#{thing}", :foo, 12]
141     }
143     expect_ValidationError("get-#{thing}_expression_missing_parameter") {
144       validate_expression [:"get-#{thing}", :foo]
145     }
147     expect_ValidationError("get-#{thing}_expression_no_parameters") {
148       validate_expression [:"get-#{thing}"]
149     }
151     expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
152       validate_expression [:"get-#{thing}", :foo, 12, 13]
153     }
155     expect_ValidationError("get-#{thing}_statement") {
156       validate_statement [:"get-#{thing}", :foo, 12]
157     }
159     expect_ValidationError("set-#{thing}_expression") {
160       validate_expression [:"set-#{thing}", :foo, 12, 18]
161     }
163     expect_true("set-#{thing}_statement") {
164       validate_statement [:"set-#{thing}", :foo, 12, 18]
165     }
167     expect_ValidationError("set-#{thing}_statement_missing_parameters") {
168       validate_statement [:"set-#{thing}", :foo]
169     }
171     expect_ValidationError("set-#{thing}_statement_no_parameters") {
172       validate_statement [:"set-#{thing}"]
173     }
175     expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
176       validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
177     }
178   end
180   expect_ValidationError("goto_expression") {
181     validate_expression [:goto, 8888]
182   }
184   expect_true("goto_statement_int") {
185     validate_statement [:goto, 8888]
186   }
188   expect_true("goto_statement_label") {
189     validate_statement [:goto, :foo]
190   }
192   expect_ValidationError("goto_statement_no_parameters") {
193     validate_statement [:goto]
194   }
196   expect_ValidationError("goto_statement_too_many_parameters") {
197     validate_statement [:goto, :foo, 42]
198   }
200   expect_true("goto_top_level") {
201     validate_top_level [:goto, :foo]
202   }
204   expect_true("group") {
205     validate_top_level [:group, [:word, 42], [:string, "xyzzy\x00"]]
206   }
208   expect_true("group_empty") {
209     validate_top_level [:group]
210   }
212   expect_ValidationError("group_invalid_statement") {
213     validate_top_level [:group, [:invalid]]
214   }
216   [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
217     expect_ValidationError("#{cnd}_expression") {
218       validate_expression [cnd, [:x, :y], [[:call, :foo]]]
219     }
221     expect_true("#{cnd}_else_statement") {
222       validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
223     }
225     expect_true("#{cnd}_statement") {
226       validate_statement [cnd, [:x, :y], [[:call, :foo]]]
227     }
229     expect_true("#{cnd}_top_level") {
230       validate_top_level [cnd, [:x, :y], [[:call, :foo]]]
231     }
233     expect_ValidationError("let_inside_#{cnd}_statement") {
234       validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
235     }
237     expect_true("let_inside_block_inside_#{cnd}_statement") {
238       validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
239     }
240   end
242   [:export, :import].each do |directive|
243     expect_true("#{directive}_top_level") {
244       validate_top_level [directive, :foo]
245     }
247     expect_true("#{directive}_top_level_multiple_parameters") {
248       validate_top_level [directive, :foo, :bar, :baz]
249     }
251     expect_ValidationError("#{directive}_top_level_no_parameters") {
252       validate_top_level [directive]
253     }
255     expect_ValidationError("#{directive}_statement") {
256       validate_statement [directive, :foo]
257     }
258   end
260   expect_true("label_statement") {
261     validate_statement [:label, :foo]
262   }
264   expect_ValidationError("label_statement_no_parameters") {
265     validate_statement [:label]
266   }
268   expect_ValidationError("label_statement_too_many_parameters") {
269     validate_statement [:label, :foo, 18]
270   }
272   expect_ValidationError("label_statement_parameter_no_symbol") {
273     validate_statement [:label, 18]
274   }
276   expect_true("label_top_level") {
277     validate_top_level [:label, :foo]
278   }
280   expect_true("let_inside_block") {
281     validate_top_level [:block, [:let, :foo, 42], [:call, :foo]]
282   }
284   expect_true("let_inside_function") {
285     validate_top_level [:function, [:n],
286                        [:let, :foo, [:mul, :n, :n]],
287                        [:call, :foo]]
288   }
290   expect_true("let_statement_int") {
291     validate_statement [:let, :x, 12]
292   }
294   expect_true("let_statement_expression") {
295     validate_statement [:let, :x, [:add, 12, 3]]
296   }
298   expect_ValidationError("let_statement_no_symbol") {
299     validate_statement [:let, 12, 12]
300   }
302   expect_ValidationError("let_statement_without_parameters") {
303     validate_statement [:let]
304   }
306   expect_true("int_is_expression") {
307     validate_expression 12
308   }
310   expect_ValidationError("no_symbol_expression") {
311     validate_expression ["wrong"]
312   }
314   expect_ValidationError("no_symbol_statement") {
315     validate_statement ["wrong"]
316   }
318   expect_ValidationError("no_symbol_top_level") {
319     validate_top_level ["wrong"]
320   }
322   expect_ValidationError("no_array_statement") {
323     validate_statement :wrong
324   }
326   expect_ValidationError("no_array_top_level") {
327     validate_top_level :wrong
328   }
330   expect_true("not_expression") {
331     validate_expression [:not, :x]
332   }
334   expect_ValidationError("not_expression_no_parameters") {
335     validate_expression [:not]
336   }
338   expect_ValidationError("not_expression_too_many_parameters") {
339     validate_expression [:not, :x, :y]
340   }
342   expect_ValidationError("section_missing_name") {
343     validate_top_level [:section]
344   }
346   expect_ValidationError("section_number") {
347     validate_top_level [:section, 12]
348   }
350   expect_true("section_string") { validate_top_level [:section, :code] }
352   expect_true("section_symbol") { validate_top_level [:section, :code] }
354   expect_true("set_statement_int") {
355     validate_statement [:set, :x, 12]
356   }
358   expect_ValidationError("set_statement_no_symbol") {
359     validate_statement [:set, 12, 12]
360   }
362   expect_true("set_statement_expression") {
363     validate_statement [:set, :x, [:add, 12, 3]]
364   }
366   expect_ValidationError("set_statement_without_parameters") {
367     validate_statement [:set]
368   }
370   expect_true("string_top_level") {
371     validate_top_level [:string, "test"]
372   }
374   expect_ValidationError("string_top_level_no_string") {
375     validate_top_level [:string, 42]
376   }
378   expect_true("symbol_is_expression") {
379     validate_expression :foo
380   }
382   expect_true("word_top_level") {
383     validate_top_level [:word, 42]
384   }
386   expect_ValidationError("word_top_level_no_integer") {
387     validate_top_level [:word, "wrong"]
388   }
391 if $0 == __FILE__
392   test_validator
393   exit report_test_results