Skip to content

Instantly share code, notes, and snippets.

@VorontsovIE
Last active December 16, 2015 11:19
Show Gist options
  • Save VorontsovIE/5426812 to your computer and use it in GitHub Desktop.
Save VorontsovIE/5426812 to your computer and use it in GitHub Desktop.
Run multiple test-cases at the same time.
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