Assert&Rubular



# Document Links

-   [マルチスケールシミュレーション特論まとめ](https://qiita.com/evendemiaire/items/25f459daa57ab8875ba6)
-   [チャート式ruby-IV(assert\_equal)](https://qiita.com/daddygongon/items/f6cea87314ee26e130ea)
-   [チャート式ruby-appendix-V(rubular)](https://qiita.com/daddygongon/items/5e2de543400fe56be768)


# テスト駆動開発


## assert\_equal

とりあえず，equalかどうかを調べる関数を書いてみる．

```ruby
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は返すようになったが，パッと見で分かるようにしたい．

というわけで，

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

```ruby:assert_equal_ro.rb
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な関数を書く．

```ruby:assert_not_equal_ro.rb
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}"

これを別の関数にして，整理する．

```ruby:aero.rb
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の存在を忘れてた．

```ruby:aeroc.rb
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の部分，

```ruby
print expected != result ?
  "succeeded in #{__method__}.\n".green :
    "failed in #{__method__}.\n".red
```

っていう書き方もあるみたい．

さらに，重複実行の予防の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
```

別のコードで本コードのmethodを引用したいときに，重複実行されないように予防．

**/codeが書いてあるファイル名(FILE)が，動いているファイル名($PROGRAM\_NAME)と一致したら中身を実行/とある．**

**どういう意味なんだ．．．（分かる方，コメントお願いします．）**


# Rubular


## 正規表現

文字情報を取り出す便利ツールな正規表現(regular expression)

[Rubular](https://rubular.com/)とやらを使って勉強する．

Regex quick referenceが載ってるので，自分でいろいろ試してみよう．

気が向いたら，講義資料読んで解説するね．


# 締め

今回は関数と正規表現について学んだ．

授業外の内容も書いてるから書く量が多いわ．．．

次回，[Recursive(再帰)とClass化の冒頭](https://qiita.com/evendemiaire/items/120d9ea4a1a6f64cda53)



------
 - **source** ~/school/multi/my_ruby/grad_members_20f/members/evendemiaire/post/assert.org