Rubyの基礎[2]



# assert\_equalを作る

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

例えば,

```ruby
assert_equal(1, 1)
true

assert_equal(1, 2)
false
```

となるような関数を書きます．この関数では，expecetedとresultがイコールならtrueが返ってきます.

```ruby
assert_equal(expected, result)                                                
```


# 書いてみる

emacsでassert\_equal.rbを開き，書き込み

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

    p assert_equal(1, 1)                                                          
```

実行．

    $ ruby assert_equal.rb
    true

trueが返ってきますね.


# colorize

色をつけてみます.

trueは緑，falseは赤にしてみる.

```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)
```


# 出力を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
    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.

```ruby
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)
```


# 今までのを合わせる

重複する部分をまとめてこうする

```ruby
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に書いたテストがそのまま実行される．例えば，

```ruby
require './assert_equal'

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

とした場合，結果は6つ表示されることになる．そこで，そういうのを回避できる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)と一致したら中身を実行．

つまり，

    $ assert_equal.rb

とした時，$PROGRAM\_NAME==\_\_FILE\_\_以下が実行される．

最終的には，こうなる

```ruby:../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

```


# 参考

-   [Rubyでの \`if FILE == $0\`の意味](https://python5.com/q/wywdmpvy)



------
 - **source** ~/Desktop/grad_members_20f/members/mm-cell/post_org/c4_assert_equal.org