3 require 'voodoo/validator'
5 include Voodoo::Validator
9 # Runs block and verifies that it returns true.
10 # If it doesn't, prints an error message and increments $errors.
11 def expect_true name, &block
19 puts "FAIL: expected true, but got #{result.inspect}"
23 puts "FAIL: Unexpected #{e.class}\n#{e.message}"
28 def expect_exception name, exception_type, &block
32 puts "FAIL: Expected exception of type #{exception_type}," +
33 " but no exception was raised"
36 if e.kind_of? exception_type
40 puts "FAIL: Expected #{exception_type}, but got #{e.class}"
46 def expect_ValidationError name, &block
47 expect_exception name, ValidationError, &block
50 [:add, :and, :asr, :bsr, :div, :mod, :mul,
51 :or, :rol, :ror, :shl, :shr, :sub, :xor].each do |binop|
52 expect_true("#{binop}_ints") {
53 validate_expression [binop, -14, 3]
56 expect_ValidationError("#{binop}_missing_arg") {
57 validate_expression [binop, 3]
60 expect_ValidationError("#{binop}_no_args") {
61 validate_expression [binop]
64 expect_ValidationError("#{binop}_too_many_args") {
65 validate_expression [binop, -14, 3, 28]
69 expect_true("align_no_parameters") {
70 validate_toplevel [:align]
73 expect_ValidationError("align_no_integer") {
74 validate_toplevel [:align, "wrong"]
77 expect_true("align_with_parameter") {
78 validate_toplevel [:align, 16]
81 expect_ValidationError("align_too_many_parameters") {
82 validate_toplevel [:align, 16, "wrong"]
85 expect_true("at_number_expression") {
86 validate_expression [:'@', 40]
89 expect_ValidationError("at_string_expression") {
90 validate_expression [:'@', "foo"]
93 expect_true("at_symbol_expression") {
94 validate_expression [:'@', :foo]
97 expect_ValidationError("block_expression") {
98 validate_expression [:block, [:call, :foo]]
101 expect_true("block_statement") {
102 validate_statement [:block, [:call, :foo]]
105 expect_true("block_level") {
106 validate_toplevel [:block, [:call, :foo]]
109 expect_true("byte_toplevel") {
110 validate_toplevel [:byte, 42]
113 expect_ValidationError("byte_toplevel_no_integer") {
114 validate_toplevel [:byte, "wrong"]
117 [:call, :"tail-call"].each do |call|
118 expect_true("#{call}_expression") {
119 validate_expression [call, :foo]
122 expect_true("#{call}_expression_multiple_parameters") {
123 validate_expression [call, :foo, :bar, 42]
126 expect_true("#{call}_statement") {
127 validate_statement [call, :foo]
130 expect_true("#{call}_toplevel") {
131 validate_toplevel [call, :foo]
134 expect_ValidationError("#{call}_without_parameters") {
135 validate_expression [call]
139 expect_ValidationError("function_as_expression") {
140 validate_expression [:function, [:x, :y], [:return, [:add, :x, :y]]]
143 expect_ValidationError("function_as_statement") {
144 validate_statement [:function, [:x, :y], [:return, [:add, :x, :y]]]
147 expect_ValidationError("function_missing_formals") {
148 validate_toplevel [:function]
151 expect_true("function_ok") {
152 validate_toplevel [:function, [:x, :y], [:return, [:add, :x, :y]]]
155 [:byte, :word].each do |thing|
156 expect_true("get-#{thing}_expression") {
157 validate_expression [:"get-#{thing}", :foo, 12]
160 expect_ValidationError("get-#{thing}_expression_missing_parameter") {
161 validate_expression [:"get-#{thing}", :foo]
164 expect_ValidationError("get-#{thing}_expression_no_parameters") {
165 validate_expression [:"get-#{thing}"]
168 expect_ValidationError("get-#{thing}_expression_too_many_parameters") {
169 validate_expression [:"get-#{thing}", :foo, 12, 13]
172 expect_ValidationError("get-#{thing}_statement") {
173 validate_statement [:"get-#{thing}", :foo, 12]
176 expect_ValidationError("set-#{thing}_expression") {
177 validate_expression [:"set-#{thing}", :foo, 12, 18]
180 expect_true("set-#{thing}_statement") {
181 validate_statement [:"set-#{thing}", :foo, 12, 18]
184 expect_ValidationError("set-#{thing}_statement_missing_parameters") {
185 validate_statement [:"set-#{thing}", :foo]
188 expect_ValidationError("set-#{thing}_statement_no_parameters") {
189 validate_statement [:"set-#{thing}"]
192 expect_ValidationError("set-#{thing}_statement_too_many_parameters") {
193 validate_statement [:"set-#{thing}", :foo, 12, 18, 19]
197 expect_ValidationError("goto_expression") {
198 validate_expression [:goto, 8888]
201 expect_true("goto_statement_int") {
202 validate_statement [:goto, 8888]
205 expect_true("goto_statement_label") {
206 validate_statement [:goto, :foo]
209 expect_ValidationError("goto_statement_no_parameters") {
210 validate_statement [:goto]
213 expect_ValidationError("goto_statement_too_many_parameters") {
214 validate_statement [:goto, :foo, 42]
217 expect_true("goto_toplevel") {
218 validate_toplevel [:goto, :foo]
221 [:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne].each do |cnd|
222 expect_ValidationError("#{cnd}_expression") {
223 validate_expression [cnd, [:x, :y], [[:call, :foo]]]
226 expect_true("#{cnd}_else_statement") {
227 validate_statement [cnd, [:x, :y], [[:call, :foo]], [[:call, :bar]]]
230 expect_true("#{cnd}_statement") {
231 validate_statement [cnd, [:x, :y], [[:call, :foo]]]
234 expect_true("#{cnd}_toplevel") {
235 validate_toplevel [cnd, [:x, :y], [[:call, :foo]]]
238 expect_ValidationError("let_inside_#{cnd}_statement") {
239 validate_statement [cnd, [:x, :y], [[:let, :foo, 42]]]
242 expect_true("let_inside_block_inside_#{cnd}_statement") {
243 validate_statement [cnd, [:x, :y], [[:block, [:let, :foo, 42]]]]
247 [:export, :import].each do |directive|
248 expect_true("#{directive}_toplevel") {
249 validate_toplevel [directive, :foo]
252 expect_true("#{directive}_toplevel_multiple_parameters") {
253 validate_toplevel [directive, :foo, :bar, :baz]
256 expect_ValidationError("#{directive}_toplevel_no_parameters") {
257 validate_toplevel [directive]
260 expect_ValidationError("#{directive}_statement") {
261 validate_statement [directive, :foo]
265 expect_true("label_statement") {
266 validate_statement [:label, :foo]
269 expect_ValidationError("label_statement_no_parameters") {
270 validate_statement [:label]
273 expect_ValidationError("label_statement_too_many_parameters") {
274 validate_statement [:label, :foo, 18]
277 expect_ValidationError("label_statement_parameter_no_symbol") {
278 validate_statement [:label, 18]
281 expect_true("label_toplevel") {
282 validate_toplevel [:label, :foo]
285 expect_true("let_inside_block") {
286 validate_toplevel [:block, [:let, :foo, 42], [:call, :foo]]
289 expect_true("let_inside_function") {
290 validate_toplevel [:function, [:n],
291 [:let, :foo, [:mul, :n, :n]],
295 expect_true("let_statement_int") {
296 validate_statement [:let, :x, 12]
299 expect_true("let_statement_expression") {
300 validate_statement [:let, :x, [:add, 12, 3]]
303 expect_ValidationError("let_statement_no_symbol") {
304 validate_statement [:let, 12, 12]
307 expect_ValidationError("let_statement_without_parameters") {
308 validate_statement [:let]
311 expect_true("int_is_expression") {
312 validate_expression 12
315 expect_ValidationError("no_symbol_expression") {
316 validate_expression ["wrong"]
319 expect_ValidationError("no_symbol_statement") {
320 validate_statement ["wrong"]
323 expect_ValidationError("no_symbol_toplevel") {
324 validate_toplevel ["wrong"]
327 expect_ValidationError("no_array_statement") {
328 validate_statement :wrong
331 expect_ValidationError("no_array_toplevel") {
332 validate_toplevel :wrong
335 expect_true("not_expression") {
336 validate_expression [:not, :x]
339 expect_ValidationError("not_expression_no_parameters") {
340 validate_expression [:not]
343 expect_ValidationError("not_expression_too_many_parameters") {
344 validate_expression [:not, :x, :y]
347 expect_ValidationError("section_missing_name") {
348 validate_toplevel [:section]
351 expect_ValidationError("section_number") {
352 validate_toplevel [:section, 12]
355 expect_true("section_string") { validate_toplevel [:section, :code] }
357 expect_true("section_symbol") { validate_toplevel [:section, :code] }
359 expect_true("set_statement_int") {
360 validate_statement [:set, :x, 12]
363 expect_ValidationError("set_statement_no_symbol") {
364 validate_statement [:set, 12, 12]
367 expect_true("set_statement_expression") {
368 validate_statement [:set, :x, [:add, 12, 3]]
371 expect_ValidationError("set_statement_without_parameters") {
372 validate_statement [:set]
375 expect_true("string_toplevel") {
376 validate_toplevel [:string, "test"]
379 expect_ValidationError("string_toplevel_no_string") {
380 validate_toplevel [:string, 42]
383 expect_true("symbol_is_expression") {
384 validate_expression :foo
387 expect_true("word_toplevel") {
388 validate_toplevel [:word, 42]
391 expect_ValidationError("word_toplevel_no_integer") {
392 validate_toplevel [:word, "wrong"]
396 puts "All tests passed"
399 puts "#{$errors} tests failed"