5
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-10

!Mac OS X-10.15.7 !ruby-2.7.1p83$

お題: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

ちゃんとtrueが返ってくる

解説:colorize

出力の文字に色を付ける

初めにcolorizeっていうのをインストールする必要があるらしい以下のコマンドでインストール

> gem install 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は赤色で出力された.

ちなみに

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

いろんな色が使えるらしい.聞いたことのない色も多い

改良:richer output

もう少し出力をきれいにする.

  • 引数でとってきた,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)

これを実行すると

> 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

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, 1)
assert_not_equal(1, 2)

上記のコードを書いて実行すると

> ruby assert_not_equal_richer_output.rb
expected :: 1
result   :: 1
failed in assert_not_equal.
expected :: 1
result   :: 2
succeeded in assert_not_equal.

ちゃんと出力されてる

整理:重複箇所の整理

重複している箇所を整理する.

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)

補足:重複実行の予防

このcodeは今後繰り返し使う.そのためには,requireで呼び出す必要がある.

例えば,

require './assert_equal'

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

というように使う.

でも,このままだとassert_equal.rbに書いたテストがそのまま実行されてしまう.

解決策として

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)と一致したら中身を実行する.最終的には

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

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/okamoto0910/article/c9.org
5
0
1

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
5
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?