3 require 'test_language_version'
4 require 'test_validator'
8 $errors_mutex = Mutex
.new
11 # Atomically increment $errors
13 $errors_mutex.synchronize
{ $errors = $errors + 1 }
16 # Runs block and verifies that it returns the given value.
17 # If it doesn't, prints an error message and increments $errors.
18 def expect name
, value
, &block
26 puts
"FAIL: expected #{value.inspect}, but got #{result.inspect}"
30 puts
"FAIL: Unexpected #{e.class}\n#{e.message}"
35 # Runs block and verifies that it returns true.
36 # If it doesn't, prints an error message and increments $errors.
37 def expect_true name
, &block
38 expect name
, true, &block
41 # Runs block and verifies that it throws an exception of type
42 # +exception_type+. If the block doesn't throw such an exception,
43 # prints an error message and increments $errors.
44 def expect_exception name
, exception_type
, &block
46 message
= "#{name}..."
48 message
<< "FAIL: Expected exception of type #{exception_type}," +
49 " but no exception was raised"
52 if e
.kind_of
? exception_type
56 message
<< "FAIL: Expected #{exception_type}, but got #{e.class}"
63 # Runs block and verifies that it throws a ValidationError.
64 # If the block doesn't throw such an exception,
65 # prints an error message and increments $errors.
66 def expect_ValidationError name
, &block
67 expect_exception name
, ValidationError
, &block
70 $RUBY = ENV['RUBY'] || 'ruby'
71 $VOODOOC = 'env RUBYLIB=../lib ../bin/voodooc'
73 # Runs a command with exec.
74 # With no block given, returns
75 # [status, stdin, stdout, stderr]
76 # With block given, passes
77 # status, stdin, stdout, stderr to block
79 pw
= IO
::pipe # pipe[0] for read, pipe[1] for write
104 dummy
, status
= Process
.wait2 pid
105 result
= [status
, pw
[1], pr
[0], pe
[0]]
109 return yield(*result
)
111 [pw
[1], pr
[0], pe
[0]].each
{ |p
| p
.close
unless p
.closed
? }
117 def add_test program
, command
, expected_output
= '', params
= {}
118 $tests << [program
, command
, expected_output
, params
]
121 def run_test program
, command
, expected_output
= '', params
= {}
122 input
= params
.has_key
?(:input) ? params
[:input] : nil
123 expected_status
= params
.has_key
?(:expected_status) ?
124 params
[:expected_status] : 0
125 expected_errors
= params
.has_key
?(:expected_errors) ?
126 params
[:expected_errors] : ''
128 message
= "#{program}..."
129 status
, stdin, stdout, stderr = popen4 command
133 exitstatus
= status
.exitstatus
135 err_output
= stderr.read
136 if exitstatus
!= expected_status
137 message
<< "FAIL: exit status is #{exitstatus}, expected #{expected_status}"
139 elsif output
!= expected_output
140 message
<< "FAIL: wrong output"
142 elsif err_output
!= expected_errors
143 message
<< "FAIL: wrong error output"
144 $stderr.puts
"--- ERRORS ---\n#{err_output}\n--- EXPECTED ---\n#{expected_errors}\n--- END ERRORS ---"
152 def add_test2 program
, expected_output
153 add_test program
, "./#{program}", expected_output
156 def add_test1 program
157 add_test2 program
, `cat #{program}.out`
160 # Runs all tests in the given queue.
161 # Each test is described by a list
162 # [name, command, expected_output, params].
163 # Those parameters are passed to run_test.
164 def run_tests queue
, nthreads
= 1
167 threads
<< Thread
.new
do
169 name
, command
, output
, params
= queue
.pop
170 run_test name
, command
, output
, params
174 threads
.each
{ |t
| t
.join
}
179 test_language_version
181 add_test
'architectures', '../bin/voodooc --architecture help',
182 `cat architectures.out`
184 add_test
'features', '../bin/voodooc -a i386 --features', `cat features.out`
186 add_test
'help', '../bin/voodooc --help', `cat help.out`
188 add_test('no-input-files',
191 :expected_status => 128,
192 :expected_errors => `cat no-input-files.err`)
194 add_test
'version', '../bin/voodooc --version',
195 "Voodoo Compiler version #{`cat ../VERSION`}"
197 add_test2
'hello', "Hello, world!\n"
199 add_test2
'tail-calls', ''
215 add_test2
'fact', "479001600\n"
219 add_test1
'plusminus'
221 add_test1
'many-vars'
243 printf
'test_output_name...'
244 increment_errors
unless system("$RUBY test_output_name.rb")
247 "#{$VOODOOC} -o errors.o errors.voo",
249 :expected_status => 1,
250 :expected_errors => `cat errors.err`
255 puts
"All tests passed"
257 puts
"#{$errors} tests failed"