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
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]
22 expect_ValidationError("#{binop}_missing_arg") {
23 validate_expression [binop, 3]
26 expect_ValidationError("#{binop}_no_args") {
27 validate_expression [binop]
30 expect_ValidationError("#{binop}_too_many_args") {
31 validate_expression [binop, -14, 3, 28]
35 expect_true("align_no_parameters") {
36 validate_top_level [:align]
39 expect_ValidationError("align_no_integer") {
40 validate_top_level [:align, "wrong"]
43 expect_true("align_with_parameter") {
44 validate_top_level [:align, 16]
47 expect_ValidationError("align_too_many_parameters") {
48 validate_top_level [:align, 16, "wrong"]
51 expect_ValidationError("auto-bytes_missing_parameter") {
52 validate_expression [:'auto-bytes']
55 expect_true("auto-bytes_expression") {
56 validate_expression [:'auto-bytes', 23456]
59 expect_ValidationError("auto-bytes_too_many_parameters") {
60 validate_expression [:'auto-bytes', 23456, :oops]
63 expect_true("at_number_expression") {
64 validate_expression [:'@', 40]
67 expect_ValidationError("at_string_expression") {
68 validate_expression [:'@', "foo"]
71 expect_true("at_symbol_expression") {
72 validate_expression [:'@', :foo]
75 expect_ValidationError("block_expression") {
76 validate_expression [:block, [:call, :foo]]
79 expect_true("block_statement") {
80 validate_statement [:block, [:call, :foo]]
83 expect_true("block_level") {
84 validate_top_level [:block, [:call, :foo]]
87 expect_true("byte_top_level") {
88 validate_top_level [:byte, 42]
91 expect_ValidationError("byte_top_level_no_integer") {
92 validate_top_level [:byte, "wrong"]
95 [:call, :"tail-call"].each do |call|
96 expect_true("#{call}_expression") {
97 validate_expression [call, :foo]
100 expect_true("#{call}_expression_multiple_parameters") {
101 validate_expression [call, :foo, :bar, 42]
104 expect_true("#{call}_statement") {
105 validate_statement [call, :foo]
108 expect_true("#{call}_top_level") {
109 validate_top_level [call, :foo]
112 expect_ValidationError("#{call}_without_parameters") {
113 validate_expression [call]
117 expect_ValidationError("function_as_expression") {
118 validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
121 expect_ValidationError("function_as_statement") {
122 validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
125 expect_ValidationError("function_missing_formals") {
126 validate_top_level [:function]
129 expect_true("function_ok") {
130 validate_top_level [:function, [:x, :y], [:return, [:add, :x, :y]]]
133 [:byte, :word].each do |thing|
134 expect_true("get-#{thing}_expression") {
135 validate_expression [:"get-#{thing}", :foo, 12]
138 expect_ValidationError("get-#{thing}_expression_missing_parameter") {
139 validate_expression [:"get-#{thing}", :foo]
142 expect_ValidationError("get-#{thing}_expression_no_parameters") {
143 validate_expression [:"get-#{thing}"]
146 expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
147 validate_expression [:"get-#{thing}", :foo, 12, 13]
150 expect_ValidationError("get-#{thing}_statement") {
151 validate_statement [:"get-#{thing}", :foo, 12]
154 expect_ValidationError("set-#{thing}_expression") {
155 validate_expression [:"set-#{thing}", :foo, 12, 18]
158 expect_true("set-#{thing}_statement") {
159 validate_statement [:"set-#{thing}", :foo, 12, 18]
162 expect_ValidationError("set-#{thing}_statement_missing_parameters") {
163 validate_statement [:"set-#{thing}", :foo]
166 expect_ValidationError("set-#{thing}_statement_no_parameters") {
167 validate_statement [:"set-#{thing}"]
170 expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
171 validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
175 expect_ValidationError("goto_expression") {
176 validate_expression [:goto, 8888]
179 expect_true("goto_statement_int") {
180 validate_statement [:goto, 8888]
183 expect_true("goto_statement_label") {
184 validate_statement [:goto, :foo]
187 expect_ValidationError("goto_statement_no_parameters") {
188 validate_statement [:goto]
191 expect_ValidationError("goto_statement_too_many_parameters") {
192 validate_statement [:goto, :foo, 42]
195 expect_true("goto_top_level") {
196 validate_top_level [:goto, :foo]
199 [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
200 expect_ValidationError("#{cnd}_expression") {
201 validate_expression [cnd, [:x, :y], [[:call, :foo]]]
204 expect_true("#{cnd}_else_statement") {
205 validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
208 expect_true("#{cnd}_statement") {
209 validate_statement [cnd, [:x, :y], [[:call, :foo]]]
212 expect_true("#{cnd}_top_level") {
213 validate_top_level [cnd, [:x, :y], [[:call, :foo]]]
216 expect_ValidationError("let_inside_#{cnd}_statement") {
217 validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
220 expect_true("let_inside_block_inside_#{cnd}_statement") {
221 validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
225 [:export, :import].each do |directive|
226 expect_true("#{directive}_top_level") {
227 validate_top_level [directive, :foo]
230 expect_true("#{directive}_top_level_multiple_parameters") {
231 validate_top_level [directive, :foo, :bar, :baz]
234 expect_ValidationError("#{directive}_top_level_no_parameters") {
235 validate_top_level [directive]
238 expect_ValidationError("#{directive}_statement") {
239 validate_statement [directive, :foo]
243 expect_true("label_statement") {
244 validate_statement [:label, :foo]
247 expect_ValidationError("label_statement_no_parameters") {
248 validate_statement [:label]
251 expect_ValidationError("label_statement_too_many_parameters") {
252 validate_statement [:label, :foo, 18]
255 expect_ValidationError("label_statement_parameter_no_symbol") {
256 validate_statement [:label, 18]
259 expect_true("label_top_level") {
260 validate_top_level [:label, :foo]
263 expect_true("let_inside_block") {
264 validate_top_level [:block, [:let, :foo, 42], [:call, :foo]]
267 expect_true("let_inside_function") {
268 validate_top_level [:function, [:n],
269 [:let, :foo, [:mul, :n, :n]],
273 expect_true("let_statement_int") {
274 validate_statement [:let, :x, 12]
277 expect_true("let_statement_expression") {
278 validate_statement [:let, :x, [:add, 12, 3]]
281 expect_ValidationError("let_statement_no_symbol") {
282 validate_statement [:let, 12, 12]
285 expect_ValidationError("let_statement_without_parameters") {
286 validate_statement [:let]
289 expect_true("int_is_expression") {
290 validate_expression 12
293 expect_ValidationError("no_symbol_expression") {
294 validate_expression ["wrong"]
297 expect_ValidationError("no_symbol_statement") {
298 validate_statement ["wrong"]
301 expect_ValidationError("no_symbol_top_level") {
302 validate_top_level ["wrong"]
305 expect_ValidationError("no_array_statement") {
306 validate_statement :wrong
309 expect_ValidationError("no_array_top_level") {
310 validate_top_level :wrong
313 expect_true("not_expression") {
314 validate_expression [:not, :x]
317 expect_ValidationError("not_expression_no_parameters") {
318 validate_expression [:not]
321 expect_ValidationError("not_expression_too_many_parameters") {
322 validate_expression [:not, :x, :y]
325 expect_ValidationError("section_missing_name") {
326 validate_top_level [:section]
329 expect_ValidationError("section_number") {
330 validate_top_level [:section, 12]
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]
341 expect_ValidationError("set_statement_no_symbol") {
342 validate_statement [:set, 12, 12]
345 expect_true("set_statement_expression") {
346 validate_statement [:set, :x, [:add, 12, 3]]
349 expect_ValidationError("set_statement_without_parameters") {
350 validate_statement [:set]
353 expect_true("string_top_level") {
354 validate_top_level [:string, "test"]
357 expect_ValidationError("string_top_level_no_string") {
358 validate_top_level [:string, 42]
361 expect_true("symbol_is_expression") {
362 validate_expression :foo
365 expect_true("word_top_level") {
366 validate_top_level [:word, 42]
369 expect_ValidationError("word_top_level_no_integer") {
370 validate_top_level [:word, "wrong"]
376 exit report_test_results