assert
2つの引数をとり,それらが同じ値かどうかを出力するような関数assert_equalを作る.同様に,それらが異なる値かどうかを出力するような関数assert_not_equalを作る.
assert.rb
require 'colorize'
def puts_vals(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
end
def assert_not_equal(expected, result)
puts_vals(expected, result)
if expected != result
print "succeeded in #{__method__}.\n".green
else
print "failed in #{__method__}.\n".red
end
end
def assert_equal(expected, result)
puts_vals(expected, result)
if expected == result
print "succeeded in #{__method__}.\n".green
else
print "failed in #{__method__}.\n".red
end
end
if $PROGRAM_NAME == __FILE__
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
end
他のソースコードからassertを利用する
require_assert.rb
require './assert'
assert_equal(4, 2 * 2)
assert_not_equal('Muku', 'Miku')
参考文献
- source ~/grad_members_20f/members/ryuta-kikuchi/qiita_articles/lecture8.org