7
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.

第9回

Last updated at Posted at 2020-12-24

assert_equal

assert_equalとは,equalか否かを確認する関数であり,今回はこの関数がどのような振る舞いをするのかを確認する.

assert_equal.rb
p 1==1

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

p assert_equal(1,1)

出力結果は以下のようになる.

true
true

colorize

 ターミナルの出力に色付けするStringの拡張機能です.通常の色付け方法だとコードが覚えにくい,読みにくいというデメリットを抱えているが,このgemがあればそれらの問題を解決することができる.

colorize.rb
require 'colorize'

def assert_equal(expected, result)
  if expected == result
    puts 'true'.green
  else
    puts 'false'.red
  end
end

p "assert_equal(1, 1)"
p "assert_equal(1, 2)"

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

講義ではassert_equalの応用であるassert_not_equalを扱っていたので,colorizeを用いて実装しようかと思います.完成したコードは以下になります.

assert_not_equal.rb

require 'colorize'

def assert_not_equal(expected, result)
  if expected == result
    puts 'false'.red
  else
    puts 'true'.green
  end
end

assert_not_equal(1, 1)
assert_not_equal(1, 2)

メソッドassert_equal_fin

それではassert_equalとcolorizeの内容を用いましてassert_equalの完成度を高めたいと思います.名前はassert_equal_finとしました.初めに,入力と出力の部分を改良したいと思います.以下のコードは期待値と実際に出力された数値を表示する関数になります.

def puts_vals(expected, result)
  puts "expected :: #{expected}"       #期待値
  puts "result   :: #{result}"     #実際に出力された値
end

次は関数assert_equal及びassert_not_equalを改良します.期待値と出力値が一致(assert_not_equalでは不一致)していたらsucceeded in assert_equal(assert_not_equal)を,不一致(assert_not_equalでは一致)ならば,faild in assert_equal(assert_not_equal)と出力します.

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

別スクリプトで使用する場合,

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

の部分は不要となるので,

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

と記入することで,記入されたファイル名と実行されたファイル名が完全一致したときの場合だけ実行させることができる.

以上より,assert_equal_finは以下のようになる.

assert_equal_fin.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 ~/grad_members_20f/members/NobuakiMori/L09_assert_equal.org
7
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
7
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?