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

assert_equal

Last updated at Posted at 2020-12-02

assert_equal

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

まずは簡単に1==1を試してみる.

assert_equal.rb に書き込んでいく

p 1==1
> ruby assert_equal.rb
true

と返ってくる

次はこれをmethodにする

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

p assert_equal(1, 1)
> ruby assert_equal.rb
true

と返ってくる.

colorizeで色をつける

まず

> gem install colorize

で install する

それから先程のコードの先頭に require 'colorize' を加え, 下記のように書きかえる

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)

このプログラムを実行すると true が green に false が red になっている.

色は green, red 以外にも black, light_black, light_red, light_green, light_yellow, light_blue, magenta, light_magenta, cyan, light_cyan, white, light_whiteがある

richer output

出力を少しわかりやすくする.

  • 引数の expected, resultの値をそのまま出力
  • そして以下のように出力するようにする
    • true の時は "succeeded in assert_equal"
    • false の時は "failed in assert_equal"
require 'colorize'

def assert_equal(expected, result)
  p ['expected', expected]
  p ['result', result]
  if expected == result
    puts 'succeeded in assert_equal'.green
  else
    puts 'failed in assert_equal'.red 
  end
end

if $PROGRAM_NAME  == __FILE__
  assert_equal(1, 1)
  assert_equal(1, 2)
end
> ruby assert_equal_richer_output.rb
[expected, 1]
[result, 1]
succeeded in assert_equal
[expected, 1]
[result, 2]
failed in assert_equal

と出力される.

拡張:assert_not_equal

関数 assert_not_equal を加える.

その後, 重複箇所の整理と重複実行の予防を考慮したコードが以下の通りである.

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".cyan :
	  "failed in #{__method__}.\n".magenta
end

def assert_equal(expected, result)
  puts_vals(expected, result)
  print case expected == result
		 when true ; "succeeded in #{__method__}.\n".cyan
		 when false ; "failed in #{__method__}.\n".magenta
		 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

上のコードの

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-IV(assert_equal)


  • source ~/grad_members_20f/members/djj31370/c4_assert_equal.org
6
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
6
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?