From e4872fb83bb81195f2eb26408e6cf9bbca22247a Mon Sep 17 00:00:00 2001 From: inglorion Date: Sat, 28 Jan 2012 20:30:25 -0800 Subject: [PATCH] Refactored test.rb so that pass_test and fail_test can be called, instead of manually implementing their logic repeatedly --- test/test.rb | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/test/test.rb b/test/test.rb index ef76605..9af1c4e 100644 --- a/test/test.rb +++ b/test/test.rb @@ -9,11 +9,32 @@ $errors = 0 $errors_mutex = Mutex.new $tests = Queue.new +# Fails a test. +# +reason+ Should be a short message describing the reason for failure. +# +stderr+ If not empty, this text is written to standard error. +def fail_test reason, stderr = '' + increment_errors + puts "FAIL: #{reason}" + $stdout.flush + unless stderr.empty? + $stderr.puts stderr + $stderr.flush + end + false +end + # Atomically increment $errors def increment_errors $errors_mutex.synchronize { $errors = $errors + 1 } end +# Passes a test. +def pass_test + puts "pass" + $stdout.flush + true +end + # Runs block and verifies that it returns the given value. # If it doesn't, prints an error message and increments $errors. def expect name, value, &block @@ -21,15 +42,12 @@ def expect name, value, &block print "#{name}..." result = yield if result == value - puts "pass" - true + pass_test else - puts "FAIL: expected #{value.inspect}, but got #{result.inspect}" - increment_errors + fail_test "expected #{value.inspect}, but got #{result.inspect}" end rescue Exception => e - puts "FAIL: Unexpected #{e.class}\n#{e.message}" - increment_errors + fail_test "Unexpected #{e.class}\n#{e.message}" end end @@ -44,21 +62,17 @@ end # prints an error message and increments $errors. def expect_exception name, exception_type, &block begin - message = "#{name}..." + print "#{name}..." yield - message << "FAIL: Expected exception of type #{exception_type}," + + fail_test "Expected exception of type #{exception_type}," + " but no exception was raised" - increment_errors rescue Exception => e if e.kind_of? exception_type - message << "pass" - true + pass_test else - message << "FAIL: Expected #{exception_type}, but got #{e.class}" - increment_errors + fail_test "Expected #{exception_type}, but got #{e.class}" end end - puts message end $RUBY = ENV['RUBY'] || 'ruby' -- 2.11.4.GIT