4
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回(assert_equal)

Last updated at Posted at 2020-12-07

お題:assert_equal

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

解法

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

p assert_equal(1, 1)

結果

> ruby assert_equal.rb
true

解説: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)

結果

> ruby assert_equal.rb
true
false

ここだと分からないがターミナル上ではtrueは緑,falseは赤になった.

colorizeというgem libraryがないと怒られたなら

> gem install colorize

でinstall

改良:richer output

もう少し出力を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
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

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

拡張:assert_not_equal

require 'colorize'

def assert_not_equal(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

assert_not_equal(1,2)

結果

expected :: 1
result   :: 2
succeeded in assert_not_equal

整理:重複箇所の整理

重複している箇所は整理して,別立ての関数に

require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end

def assert_equal(expected, result)
  puts_vals(expected,result)
  if expected == result
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

def assert_not_equal(expected, result)
  puts_vals(expected,result)
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

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

補足:重複実行の予防

このcodeは今後繰り返し使うらしい.そのためには,requireで呼び出す

例えば,

require './assert_equal'

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

というように使う.

しかし,今のままだとassert_equal.rbに書いたテストがそのまま実行されるのでそういうのを回避してくれる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)と一致したら中身を実行.idiomなので覚えよう.

require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end

def assert_equal(expected, result)
  puts_vals(expected,result)
  if expected == result
    puts "succeeded in assert_equal".green
  else
    puts 'failed in assert_equal'.red
  end
end

def assert_not_equal(expected, result)
  puts_vals(expected,result)
  if expected != result
    puts "succeeded in assert_not_equal".green
  else
    puts 'failed in assert_not_equal'.red
  end
end

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

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