Changed "toplevel" to "top_level" to align with existing code
[voodoo-lang.git] / test / test_validator.rb
blobf94723aeaa5e73d82dc44de25fa65ad17bce5409
1 #! /usr/bin/env ruby
3 require 'voodoo/validator'
5 include Voodoo::Validator
7 def test_validator
8   [:add, :and, :asr, :bsr, :div, :mod, :mul,
9    :or, :rol, :ror, :shl, :shr, :sub, :xor].each do |binop|
10     expect_true("#{binop}_ints") {
11       validate_expression [binop, -14, 3]
12     }
14     expect_ValidationError("#{binop}_missing_arg") {
15       validate_expression [binop, 3]
16     }
18     expect_ValidationError("#{binop}_no_args") {
19       validate_expression [binop]
20     }
22     expect_ValidationError("#{binop}_too_many_args") {
23       validate_expression [binop, -14, 3, 28]
24     }
25   end
27   expect_true("align_no_parameters") {
28     validate_top_level [:align]
29   }
31   expect_ValidationError("align_no_integer") {
32     validate_top_level [:align, "wrong"]
33   }
35   expect_true("align_with_parameter") {
36     validate_top_level [:align, 16]
37   }
39   expect_ValidationError("align_too_many_parameters") {
40     validate_top_level [:align, 16, "wrong"]
41   }
43   expect_true("at_number_expression") {
44     validate_expression [:'@', 40]
45   }
47   expect_ValidationError("at_string_expression") {
48     validate_expression [:'@', "foo"]
49   }
51   expect_true("at_symbol_expression") {
52     validate_expression [:'@', :foo]
53   }
55   expect_ValidationError("block_expression") {
56     validate_expression [:block, [:call, :foo]]
57   }
59   expect_true("block_statement") {
60     validate_statement [:block, [:call, :foo]]
61   }
63   expect_true("block_level") {
64     validate_top_level [:block, [:call, :foo]]
65   }
67   expect_true("byte_top_level") {
68     validate_top_level [:byte, 42]
69   }
71   expect_ValidationError("byte_top_level_no_integer") {
72     validate_top_level [:byte, "wrong"]
73   }
75   [:call, :"tail-call"].each do |call|
76     expect_true("#{call}_expression") {
77       validate_expression [call, :foo]
78     }
80     expect_true("#{call}_expression_multiple_parameters") {
81       validate_expression [call, :foo, :bar, 42]
82     }
84     expect_true("#{call}_statement") {
85       validate_statement [call, :foo]
86     }
88     expect_true("#{call}_top_level") {
89       validate_top_level [call, :foo]
90     }
92     expect_ValidationError("#{call}_without_parameters") {
93       validate_expression [call]
94     }
95   end
97   expect_ValidationError("function_as_expression") {
98     validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
99   }
101   expect_ValidationError("function_as_statement") {
102     validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
103   }
105   expect_ValidationError("function_missing_formals") {
106     validate_top_level [:function]
107   }
109   expect_true("function_ok") {
110     validate_top_level [:function, [:x, :y], [:return, [:add, :x, :y]]]
111   }
113   [:byte, :word].each do |thing|
114     expect_true("get-#{thing}_expression") {
115       validate_expression [:"get-#{thing}", :foo, 12]
116     }
118     expect_ValidationError("get-#{thing}_expression_missing_parameter") {
119       validate_expression [:"get-#{thing}", :foo]
120     }
122     expect_ValidationError("get-#{thing}_expression_no_parameters") {
123       validate_expression [:"get-#{thing}"]
124     }
126     expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
127       validate_expression [:"get-#{thing}", :foo, 12, 13]
128     }
130     expect_ValidationError("get-#{thing}_statement") {
131       validate_statement [:"get-#{thing}", :foo, 12]
132     }
134     expect_ValidationError("set-#{thing}_expression") {
135       validate_expression [:"set-#{thing}", :foo, 12, 18]
136     }
138     expect_true("set-#{thing}_statement") {
139       validate_statement [:"set-#{thing}", :foo, 12, 18]
140     }
142     expect_ValidationError("set-#{thing}_statement_missing_parameters") {
143       validate_statement [:"set-#{thing}", :foo]
144     }
146     expect_ValidationError("set-#{thing}_statement_no_parameters") {
147       validate_statement [:"set-#{thing}"]
148     }
150     expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
151       validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
152     }
153   end
155   expect_ValidationError("goto_expression") {
156     validate_expression [:goto, 8888]
157   }
159   expect_true("goto_statement_int") {
160     validate_statement [:goto, 8888]
161   }
163   expect_true("goto_statement_label") {
164     validate_statement [:goto, :foo]
165   }
167   expect_ValidationError("goto_statement_no_parameters") {
168     validate_statement [:goto]
169   }
171   expect_ValidationError("goto_statement_too_many_parameters") {
172     validate_statement [:goto, :foo, 42]
173   }
175   expect_true("goto_top_level") {
176     validate_top_level [:goto, :foo]
177   }
179   [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
180     expect_ValidationError("#{cnd}_expression") {
181       validate_expression [cnd, [:x, :y], [[:call, :foo]]]
182     }
184     expect_true("#{cnd}_else_statement") {
185       validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
186     }
188     expect_true("#{cnd}_statement") {
189       validate_statement [cnd, [:x, :y], [[:call, :foo]]]
190     }
192     expect_true("#{cnd}_top_level") {
193       validate_top_level [cnd, [:x, :y], [[:call, :foo]]]
194     }
196     expect_ValidationError("let_inside_#{cnd}_statement") {
197       validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
198     }
200     expect_true("let_inside_block_inside_#{cnd}_statement") {
201       validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
202     }
203   end
205   [:export, :import].each do |directive|
206     expect_true("#{directive}_top_level") {
207       validate_top_level [directive, :foo]
208     }
210     expect_true("#{directive}_top_level_multiple_parameters") {
211       validate_top_level [directive, :foo, :bar, :baz]
212     }
214     expect_ValidationError("#{directive}_top_level_no_parameters") {
215       validate_top_level [directive]
216     }
218     expect_ValidationError("#{directive}_statement") {
219       validate_statement [directive, :foo]
220     }
221   end
223   expect_true("label_statement") {
224     validate_statement [:label, :foo]
225   }
227   expect_ValidationError("label_statement_no_parameters") {
228     validate_statement [:label]
229   }
231   expect_ValidationError("label_statement_too_many_parameters") {
232     validate_statement [:label, :foo, 18]
233   }
235   expect_ValidationError("label_statement_parameter_no_symbol") {
236     validate_statement [:label, 18]
237   }
239   expect_true("label_top_level") {
240     validate_top_level [:label, :foo]
241   }
243   expect_true("let_inside_block") {
244     validate_top_level [:block, [:let, :foo, 42], [:call, :foo]]
245   }
247   expect_true("let_inside_function") {
248     validate_top_level [:function, [:n],
249                        [:let, :foo, [:mul, :n, :n]],
250                        [:call, :foo]]
251   }
253   expect_true("let_statement_int") {
254     validate_statement [:let, :x, 12]
255   }
257   expect_true("let_statement_expression") {
258     validate_statement [:let, :x, [:add, 12, 3]]
259   }
261   expect_ValidationError("let_statement_no_symbol") {
262     validate_statement [:let, 12, 12]
263   }
265   expect_ValidationError("let_statement_without_parameters") {
266     validate_statement [:let]
267   }
269   expect_true("int_is_expression") {
270     validate_expression 12
271   }
273   expect_ValidationError("no_symbol_expression") {
274     validate_expression ["wrong"]
275   }
277   expect_ValidationError("no_symbol_statement") {
278     validate_statement ["wrong"]
279   }
281   expect_ValidationError("no_symbol_top_level") {
282     validate_top_level ["wrong"]
283   }
285   expect_ValidationError("no_array_statement") {
286     validate_statement :wrong
287   }
289   expect_ValidationError("no_array_top_level") {
290     validate_top_level :wrong
291   }
293   expect_true("not_expression") {
294     validate_expression [:not, :x]
295   }
297   expect_ValidationError("not_expression_no_parameters") {
298     validate_expression [:not]
299   }
301   expect_ValidationError("not_expression_too_many_parameters") {
302     validate_expression [:not, :x, :y]
303   }
305   expect_ValidationError("section_missing_name") {
306     validate_top_level [:section]
307   }
309   expect_ValidationError("section_number") {
310     validate_top_level [:section, 12]
311   }
313   expect_true("section_string") { validate_top_level [:section, :code] }
315   expect_true("section_symbol") { validate_top_level [:section, :code] }
317   expect_true("set_statement_int") {
318     validate_statement [:set, :x, 12]
319   }
321   expect_ValidationError("set_statement_no_symbol") {
322     validate_statement [:set, 12, 12]
323   }
325   expect_true("set_statement_expression") {
326     validate_statement [:set, :x, [:add, 12, 3]]
327   }
329   expect_ValidationError("set_statement_without_parameters") {
330     validate_statement [:set]
331   }
333   expect_true("string_top_level") {
334     validate_top_level [:string, "test"]
335   }
337   expect_ValidationError("string_top_level_no_string") {
338     validate_top_level [:string, 42]
339   }
341   expect_true("symbol_is_expression") {
342     validate_expression :foo
343   }
345   expect_true("word_top_level") {
346     validate_top_level [:word, 42]
347   }
349   expect_ValidationError("word_top_level_no_integer") {
350     validate_top_level [:word, "wrong"]
351   }