Restructured tests so that tests for validator and language version
[voodoo-lang.git] / test / test_validator.rb
blobbf9cd7dceb19d0a67405886b3b8a6dc378431d1b
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_true("at_number_expression") {
52     validate_expression [:'@', 40]
53   }
55   expect_ValidationError("at_string_expression") {
56     validate_expression [:'@', "foo"]
57   }
59   expect_true("at_symbol_expression") {
60     validate_expression [:'@', :foo]
61   }
63   expect_ValidationError("block_expression") {
64     validate_expression [:block, [:call, :foo]]
65   }
67   expect_true("block_statement") {
68     validate_statement [:block, [:call, :foo]]
69   }
71   expect_true("block_level") {
72     validate_top_level [:block, [:call, :foo]]
73   }
75   expect_true("byte_top_level") {
76     validate_top_level [:byte, 42]
77   }
79   expect_ValidationError("byte_top_level_no_integer") {
80     validate_top_level [:byte, "wrong"]
81   }
83   [:call, :"tail-call"].each do |call|
84     expect_true("#{call}_expression") {
85       validate_expression [call, :foo]
86     }
88     expect_true("#{call}_expression_multiple_parameters") {
89       validate_expression [call, :foo, :bar, 42]
90     }
92     expect_true("#{call}_statement") {
93       validate_statement [call, :foo]
94     }
96     expect_true("#{call}_top_level") {
97       validate_top_level [call, :foo]
98     }
100     expect_ValidationError("#{call}_without_parameters") {
101       validate_expression [call]
102     }
103   end
105   expect_ValidationError("function_as_expression") {
106     validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
107   }
109   expect_ValidationError("function_as_statement") {
110     validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
111   }
113   expect_ValidationError("function_missing_formals") {
114     validate_top_level [:function]
115   }
117   expect_true("function_ok") {
118     validate_top_level [:function, [:x, :y], [:return, [:add, :x, :y]]]
119   }
121   [:byte, :word].each do |thing|
122     expect_true("get-#{thing}_expression") {
123       validate_expression [:"get-#{thing}", :foo, 12]
124     }
126     expect_ValidationError("get-#{thing}_expression_missing_parameter") {
127       validate_expression [:"get-#{thing}", :foo]
128     }
130     expect_ValidationError("get-#{thing}_expression_no_parameters") {
131       validate_expression [:"get-#{thing}"]
132     }
134     expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
135       validate_expression [:"get-#{thing}", :foo, 12, 13]
136     }
138     expect_ValidationError("get-#{thing}_statement") {
139       validate_statement [:"get-#{thing}", :foo, 12]
140     }
142     expect_ValidationError("set-#{thing}_expression") {
143       validate_expression [:"set-#{thing}", :foo, 12, 18]
144     }
146     expect_true("set-#{thing}_statement") {
147       validate_statement [:"set-#{thing}", :foo, 12, 18]
148     }
150     expect_ValidationError("set-#{thing}_statement_missing_parameters") {
151       validate_statement [:"set-#{thing}", :foo]
152     }
154     expect_ValidationError("set-#{thing}_statement_no_parameters") {
155       validate_statement [:"set-#{thing}"]
156     }
158     expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
159       validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
160     }
161   end
163   expect_ValidationError("goto_expression") {
164     validate_expression [:goto, 8888]
165   }
167   expect_true("goto_statement_int") {
168     validate_statement [:goto, 8888]
169   }
171   expect_true("goto_statement_label") {
172     validate_statement [:goto, :foo]
173   }
175   expect_ValidationError("goto_statement_no_parameters") {
176     validate_statement [:goto]
177   }
179   expect_ValidationError("goto_statement_too_many_parameters") {
180     validate_statement [:goto, :foo, 42]
181   }
183   expect_true("goto_top_level") {
184     validate_top_level [:goto, :foo]
185   }
187   [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
188     expect_ValidationError("#{cnd}_expression") {
189       validate_expression [cnd, [:x, :y], [[:call, :foo]]]
190     }
192     expect_true("#{cnd}_else_statement") {
193       validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
194     }
196     expect_true("#{cnd}_statement") {
197       validate_statement [cnd, [:x, :y], [[:call, :foo]]]
198     }
200     expect_true("#{cnd}_top_level") {
201       validate_top_level [cnd, [:x, :y], [[:call, :foo]]]
202     }
204     expect_ValidationError("let_inside_#{cnd}_statement") {
205       validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
206     }
208     expect_true("let_inside_block_inside_#{cnd}_statement") {
209       validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
210     }
211   end
213   [:export, :import].each do |directive|
214     expect_true("#{directive}_top_level") {
215       validate_top_level [directive, :foo]
216     }
218     expect_true("#{directive}_top_level_multiple_parameters") {
219       validate_top_level [directive, :foo, :bar, :baz]
220     }
222     expect_ValidationError("#{directive}_top_level_no_parameters") {
223       validate_top_level [directive]
224     }
226     expect_ValidationError("#{directive}_statement") {
227       validate_statement [directive, :foo]
228     }
229   end
231   expect_true("label_statement") {
232     validate_statement [:label, :foo]
233   }
235   expect_ValidationError("label_statement_no_parameters") {
236     validate_statement [:label]
237   }
239   expect_ValidationError("label_statement_too_many_parameters") {
240     validate_statement [:label, :foo, 18]
241   }
243   expect_ValidationError("label_statement_parameter_no_symbol") {
244     validate_statement [:label, 18]
245   }
247   expect_true("label_top_level") {
248     validate_top_level [:label, :foo]
249   }
251   expect_true("let_inside_block") {
252     validate_top_level [:block, [:let, :foo, 42], [:call, :foo]]
253   }
255   expect_true("let_inside_function") {
256     validate_top_level [:function, [:n],
257                        [:let, :foo, [:mul, :n, :n]],
258                        [:call, :foo]]
259   }
261   expect_true("let_statement_int") {
262     validate_statement [:let, :x, 12]
263   }
265   expect_true("let_statement_expression") {
266     validate_statement [:let, :x, [:add, 12, 3]]
267   }
269   expect_ValidationError("let_statement_no_symbol") {
270     validate_statement [:let, 12, 12]
271   }
273   expect_ValidationError("let_statement_without_parameters") {
274     validate_statement [:let]
275   }
277   expect_true("int_is_expression") {
278     validate_expression 12
279   }
281   expect_ValidationError("no_symbol_expression") {
282     validate_expression ["wrong"]
283   }
285   expect_ValidationError("no_symbol_statement") {
286     validate_statement ["wrong"]
287   }
289   expect_ValidationError("no_symbol_top_level") {
290     validate_top_level ["wrong"]
291   }
293   expect_ValidationError("no_array_statement") {
294     validate_statement :wrong
295   }
297   expect_ValidationError("no_array_top_level") {
298     validate_top_level :wrong
299   }
301   expect_true("not_expression") {
302     validate_expression [:not, :x]
303   }
305   expect_ValidationError("not_expression_no_parameters") {
306     validate_expression [:not]
307   }
309   expect_ValidationError("not_expression_too_many_parameters") {
310     validate_expression [:not, :x, :y]
311   }
313   expect_ValidationError("section_missing_name") {
314     validate_top_level [:section]
315   }
317   expect_ValidationError("section_number") {
318     validate_top_level [:section, 12]
319   }
321   expect_true("section_string") { validate_top_level [:section, :code] }
323   expect_true("section_symbol") { validate_top_level [:section, :code] }
325   expect_true("set_statement_int") {
326     validate_statement [:set, :x, 12]
327   }
329   expect_ValidationError("set_statement_no_symbol") {
330     validate_statement [:set, 12, 12]
331   }
333   expect_true("set_statement_expression") {
334     validate_statement [:set, :x, [:add, 12, 3]]
335   }
337   expect_ValidationError("set_statement_without_parameters") {
338     validate_statement [:set]
339   }
341   expect_true("string_top_level") {
342     validate_top_level [:string, "test"]
343   }
345   expect_ValidationError("string_top_level_no_string") {
346     validate_top_level [:string, 42]
347   }
349   expect_true("symbol_is_expression") {
350     validate_expression :foo
351   }
353   expect_true("word_top_level") {
354     validate_top_level [:word, 42]
355   }
357   expect_ValidationError("word_top_level_no_integer") {
358     validate_top_level [:word, "wrong"]
359   }
362 if $0 == __FILE__
363   test_validator
364   exit report_test_results