Document Links
テスト駆動開発
assert_equal
とりあえず,equalかどうかを調べる関数を書いてみる.
def assert_equal(expected, result)
return expected == result
end
p assert_equal(1,1)
p assert_equal(1,2)
実行結果
$ ruby assert_equal.rb
true
false
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)
実行結果
$ ruby assert_equal.rb
true
false
trueが赤色に,falseが緑色に出力される.(example内で色付けるのどうするんだ?)
ちなみに,
$ irb
irb(main):001:0> require 'colorize'
=> true
irb(main):002:0> String.colors
=> [:black, :light_black, :red, :light_red, :green, :light_green, :yellow, :light_yellow, :blue, :light_blue, :magenta, :light_magenta, :cyan, :light_cyan, :white, :light_white, :default]
で,何色にcolorizeできるかがわかる.
出力を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)
if expected == result
puts "expected :: #{expected}"
puts "result :: #{result}"
print "succeeded in #{__method__}.\n".green
else
puts "expected :: #{expected}"
puts "result :: #{result}"
print "failed in #{__method__}.\n".red
end
end
assert_equal(1,1)
assert_equal(1,2)
実行結果
$ ruby assert_equal_ro.rb
expected :: 1
result :: 1
succeeded in assert_equal.
expected :: 1
result :: 2
failed in assert_equal.
assert_not_equal
必要とは思えないが,not_equalでtrueな関数を書く.
require 'colorize'
def assert_not_equal(expected, result)
if expected != result
puts "expected :: #{expected}"
puts "result :: #{result}"
print "succeeded in #{__method__}.\n".green
else
puts "expected :: #{expected}"
puts "result :: #{result}"
print "failed in #{__method__}.\n".red
end
end
assert_not_equal(1,2)
assert_not_equal(1,1)
実行結果
$ ruby assert_not_equal_ro.rb
expected :: 1
result :: 2
succeeded in assert_not_equal.
expected :: 1
result :: 1
failed in assert_not_equal.
関数の整理
今のままでは重複した処理がある.
- puts "expected: #{expected}"
- puts "result: #{result}"
これを別の関数にして,整理する.
require 'colorize'
def puts_value(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
end
def assert_equal(expected, result)
puts_value(expected, result)
if expected == result
print "succeeded in #{__method__}.\n".green
else
print "failed in #{__method__}.\n".red
end
end
def assert_not_equal(expected, result)
puts_value(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
実行結果
$ ruby aero.rb
expected :: 1
result :: 1
succeeded in assert_equal.
expected :: 1
result :: 2
failed in assert_equal.
expected :: 1
result :: 2
succeeded in assert_not_equal.
expected :: 1
result :: 1
failed in assert_not_equal.
case
caseの存在を忘れてた.
require 'colorize'
def puts_vals(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
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
def assert_not_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
これで完成.
と思って講義資料を見てみると,caseの部分,
print expected != result ?
"succeeded in #{__method__}.\n".green :
"failed in #{__method__}.\n".red
っていう書き方もあるみたい.
さらに,重複実行の予防のidiomとして,
if $PROGRAM_NAME == __FILE__
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
end
別のコードで本コードのmethodを引用したいときに,重複実行されないように予防.
/codeが書いてあるファイル名(FILE)が,動いているファイル名($PROGRAM_NAME)と一致したら中身を実行/とある.
どういう意味なんだ...(分かる方,コメントお願いします.)
Rubular
正規表現
文字情報を取り出す便利ツールな正規表現(regular expression)
Rubularとやらを使って勉強する.
Regex quick referenceが載ってるので,自分でいろいろ試してみよう.
気が向いたら,講義資料読んで解説するね.
締め
今回は関数と正規表現について学んだ.
授業外の内容も書いてるから書く量が多いわ...
- source ~/school/multi/my_ruby/grad_members_20f/members/evendemiaire/post/assert.org