第9回(assert_equal)

# お題:assert\_equal

assert equalとはequalかどうかを確かめる(assert)関数

# 解法

```ruby
def assert_equal(expected, result)
  return expected == result
end

p assert_equal(1, 1)
```

結果

    > ruby assert_equal.rb
    true

# 解説:colorize-出色の継承-

出力に色を付ける.

```ruby
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は赤になった.

colorizeというgem libraryがないと怒られたなら

    > gem install colorize

でinstall

# 改良:richer output

もう少し出力をrichに
-   引数でとってきた，expected, resultの値をそのまま出力
-   どうなったかを記述
    -   **true:** print "succeeded in assert\_equal.\n".green
    -   **false:** print "failed in assert\_equal.\n".red

```ruby
require 'colorize'

def assert_equal(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
  if expected == result
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

assert_equal(1,1)
assert_equal(1,2)
```

# 拡張:assert\_not\_equal

```ruby
require 'colorize'

def assert_not_equal(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

assert_not_equal(1,2)
```

結果

    expected :: 1
    result   :: 2
    succeeded in assert_not_equal

# 整理:重複箇所の整理

重複している箇所は整理して，別立ての関数に

```ruby
require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end

def assert_equal(expected, result)
  puts_vals(expected,result)
  if expected == result
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

def assert_not_equal(expected, result)
  puts_vals(expected,result)
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

assert_equal(1,1)
assert_equal(1,2)
assert_not_equal(1,1)
assert_not_equal(1,2)
```

# 補足:重複実行の予防

このcodeは今後繰り返し使うらしい.そのためには，requireで呼び出す

例えば，

```ruby
require './assert_equal'

assert_equal(2, 1*2)
assert_not_equal('bob', 'bob2')
```

というように使う.

しかし，今のままだとassert\_equal.rbに書いたテストがそのまま実行されるのでそういうのを回避してくれるidiom(熟語)を使う.

```ruby
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)と一致したら中身を実行.idiomなので覚えよう.

```ruby
require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end

def assert_equal(expected, result)
  puts_vals(expected,result)
  if expected == result
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

def assert_not_equal(expected, result)
  puts_vals(expected,result)
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

if $PROGRAM_NAME == __FILE__
  assert_equal(1,1)
  assert_equal(1,2)
  assert_not_equal(1,1)
  assert_not_equal(1,2)
end
```

------
 - **source** ~/grad_members_20f/members/yoshida/c4_assert_equal.org