1 #### Common functionality for running tests.
3 # Make sure that the lib directory is in $LOAD_PATH
4 $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
9 def initialize initial_value = 0
15 @mutex.synchronize { @value = @value + x }
19 @mutex.synchronize { @value }
23 @mutex.synchronize { @value = x }
35 $failed = AtomicInteger.new
36 $passed = AtomicInteger.new
40 # +reason+ Should be a short message describing the reason for failure.
41 # +stderr+ If not empty, this text is written to standard error.
42 def fail_test reason = $!, stderr = ''
44 puts "FAIL: #{reason}"
61 # Runs block and verifies that it returns the given value.
62 # If it doesn't, prints an error message and increments $failed.
63 def expect name, value, &block
70 fail_test "expected #{value.inspect}, but got #{result.inspect}"
73 fail_test "Unexpected #{e.class}\n#{e.message}"
77 # Runs block and verifies that it returns true.
78 # If it doesn't, prints an error message and increments $failed.
79 def expect_true name, &block
80 expect name, true, &block
83 # Runs block and verifies that it throws an exception of type
84 # +exception_type+. If the block doesn't throw such an exception,
85 # prints an error message and increments $failed.
86 def expect_exception name, exception_type, &block
90 fail_test "Expected exception of type #{exception_type}," +
91 " but no exception was raised"
93 if e.kind_of? exception_type
96 fail_test "Expected #{exception_type}, but got #{e.class}"
101 $RUBY = ENV['RUBY'] || 'ruby'
102 $VOODOOC = 'env RUBYLIB=../lib ../bin/voodooc'
104 # Runs a command with exec.
105 # With no block given, returns
106 # [status, stdin, stdout, stderr]
107 # With block given, passes
108 # status, stdin, stdout, stderr to block
110 pw = IO::pipe # pipe[0] for read, pipe[1] for write
135 dummy, status = Process.wait2 pid
136 result = [status, pw[1], pr[0], pe[0]]
140 return yield(*result)
142 [pw[1], pr[0], pe[0]].each { |p| p.close unless p.closed? }
148 def add_test program, command, expected_output = '', params = {}
149 $tests << [program, command, expected_output, params]
152 def run_test program, command, expected_output = '', params = {}
153 input = params.has_key?(:input) ? params[:input] : nil
154 expected_status = params.has_key?(:expected_status) ?
155 params[:expected_status] : 0
156 expected_errors = params.has_key?(:expected_errors) ?
157 params[:expected_errors] : ''
158 expected_signal = params.has_key?(:expected_signal) ?
159 params[:expected_signal] : nil
161 message = "#{program}..."
162 status, stdin, stdout, stderr = popen4 command
166 exitstatus = status.exited? ? status.exitstatus : (status.to_i >> 8)
167 signal = status.signaled? ? status.termsig : nil
169 err_output = stderr.read
170 if expected_signal && signal != expected_signal
171 message << "FAIL: expected signal #{expected_signal} but got "
173 message << "signal #{signal}"
175 message << "no signal"
178 elsif !expected_signal && signal
179 message << "FAIL: got signal #{signal} while not expecting a signal"
181 elsif exitstatus != expected_status
182 message << "FAIL: exit status is #{exitstatus}, expected #{expected_status}"
184 elsif output != expected_output
185 message << "FAIL: wrong output"
186 $stderr.puts "--- OUTPUT ---\n#{output}\n--- EXPECTED ---\n#{expected_output}\n--- END OUTPUT ---"
188 elsif err_output != expected_errors
189 message << "FAIL: wrong error output"
190 $stderr.puts "--- ERRORS ---\n#{err_output}\n--- EXPECTED ---\n#{expected_errors}\n--- END ERRORS ---"
199 def add_test2 program, expected_output
200 add_test program, "./#{program}", expected_output
203 def add_test1 program
204 add_test2 program, `cat #{program}.out`
207 # Reports tests results.
208 # Returns 0 if all tests passed, 1 if some failed.
209 def report_test_results
210 puts "#{$passed.value} tests passed, #{$failed.value} tests failed"
211 if $failed.value == 0
218 # Runs all tests in the given queue.
219 # Each test is described by a list
220 # [name, command, expected_output, params].
221 # Those parameters are passed to run_test.
222 def run_tests queue, nthreads = 1
225 threads << Thread.new do
227 name, command, output, params = queue.pop
228 run_test name, command, output, params
232 threads.each { |t| t.join }