12
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rubyの基礎[2]

Last updated at Posted at 2020-12-02

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
12
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?