assert_equalを作る
assert equalとはequalかどうかを確かめる(assert)関数.
例えば,
assert_equal(1, 1)
true
assert_equal(1, 2)
false
となるような関数を書きます.この関数では,expecetedとresultがイコールならtrueが返ってきます.
assert_equal(expected, result)
書いてみる
emacsでassert_equal.rbを開き,書き込み
def assert_equal(expected, result)
return expected == result
end
p assert_equal(1, 1)
実行.
$ ruby assert_equal.rb
true
trueが返ってきますね.
colorize
色をつけてみます.
trueは緑,falseは赤にしてみる.
require 'colorize'
def assert_equal(expected, result)
if expected == result
puts 'true'.green
else
puts 'false'.red
end
end
assert_equal(1, 1)
assert_equal(1, 2)
出力をrichに
- 引数でとってきた,expected, resultの値をそのまま出力
- どうなったかを記述
- true: print "succeeded in assert_equal.\n".green
- false: print "failed in assert_equal.\n".red
require 'colorize'
def assert_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected == result
print "succeeded in #{__method__}.\n".green
else
print "failed in #{__method__}.\n".red
end
end
assert_equal(1,1)
assert_equal(1,2)
assert_not_equal
notバージョンも作ってみます.!= にすればnot equal.
require 'colorize'
def assert_not_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected != result
print "succeeded in assert_not_equal.\n".green
else
print "failed in assert_not_equal.\n".red
end
end
assert_not_equal(1, 1)
assert_not_equal(1, 2)
今までのを合わせる
重複する部分をまとめてこうする
require 'colorize'
def puts_vals(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
end
def assert_equal(expected, result)
puts_vals(expected, result)
print expected == result ?
"succeeded in #{__method__}.\n".green :
"failed in #{__method__}.\n".red
end
def assert_not_equal(expected, result)
puts_vals(expected, result)
print expected != result ?
"succeeded in #{__method__}.\n".green :
"failed in #{__method__}.\n".red
end
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
しかし,今のままだとassert_equal.rbに書いたテストがそのまま実行される.例えば,
require './assert_equal'
assert_equal(2, 1*2)
assert_not_equal('bob', 'bob2')
とした場合,結果は6つ表示されることになる.そこで,そういうのを回避できるidiomを使う.
if $PROGRAM_NAME == __FILE__
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
end
意味は,codeが書いてあるファイル名(FILE)が,動いているファイル名($PROGRAM_NAME)と一致したら中身を実行.
つまり,
$ assert_equal.rb
とした時,$PROGRAM_NAME==__FILE__以下が実行される.
最終的には,こうなる
../codes/assert_equal_final.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)
print expected != result ?
"succeeded in #{__method__}.\n".green :
"failed in #{__method__}.\n".red
end
def assert_equal(expected, result)
puts_vals(expected, result)
print case expected == result
when true ; "succeeded in #{__method__}.\n".green
when false ; "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
参考
- source ~/Desktop/grad_members_20f/members/mm-cell/post_org/c4_assert_equal.org