Last active
December 16, 2015 11:19
-
-
Save VorontsovIE/5426812 to your computer and use it in GitHub Desktop.
Run multiple test-cases at the same time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def wrong_result(meth, args, expected, real) | |
$stderr.puts "test #{meth}(#{args.map(&:inspect).join(',')}) expected to be #{expected} but was #{real}" | |
end | |
def failed_test(meth, args, expected, exception) | |
$stderr.puts "test #{meth}(#{args.map(&:inspect).join(',')}) expected to be #{expected} but raised an error #{exception}" | |
end | |
def test_method(meth, test_cases) | |
test_cases.each do |args, expected| | |
begin | |
arglist = args.is_a?(Array) ? args : [args] | |
result = send(meth, *arglist) | |
wrong_result(meth, arglist, expected, result) unless result == expected | |
rescue => e | |
failed_test(meth, arglist, expected, e) | |
end | |
end | |
end | |
def det(mat) | |
mat ? 1 : 0 | |
end | |
def atan2(y,x) | |
raise 'Fail-Fail-Fail!' unless x | |
y ? 1 : 0 | |
end | |
test_method :det, 'nonempty' => 1, nil => 0, 'test with wrong result' => 0 | |
test_method :atan2, ['nonempty', 'nobody cares'] => 1, [nil, 'nobody cares'] => 0, ['test with wrong result', 'nobody cares'] => 0, ['failing test', nil] => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment