8
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-09

目標

assert equalとはequalかどうかを確かめる(assert)関数を使い,それらをテスト駆動開発で実装していく.

解説

TDDに基づいて

まずは1==1から始めていきます.

p 1==1

として見ましょう.

> ruby assert_equal.rb
true

次はこれをメソッドに直していきます.

p assert_equal(1, 1)

これを動かしてみると

> ruby assert_equal.rb
assert_equal.rb:1:in `<main>': undefined method `assert_equal' for main:Object (NoMethodError)

assert_equalの関数が定義されていないのでエラーが出ます.

def assert_equal(expected, result)
  return expected == result
end
p assert_equal(1, 1)
> ruby assert_equal.rb
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)

colorizeには他にも使える色があります.実際に見てみましょう.

> 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)
  if expected == result
    puts "expected #{expected}"
    puts "result #{result}"
    puts "succeeded in #{__method__}".green
  else
    puts "expected #{expected}"
    puts "result #{result}"
    puts "failed in #{__method__}".red
  end
end

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

expected 1
result 1
succeeded in assert_equal
expected 1
result 2
failed in assert_equal

#{method}と表記することでコードの中で定義した名前が出力されました.

assert_not_equal

今度は逆にexpectedとresultがnot_equalかどうか判定するassert_not_equal.rbを考えます.


require 'colorize'

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

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

expected 1
result 1
failed in assert_not_equal
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_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

参考にしたサイト

 この記事を書くために以下のサイトを参考にしました.チャート式ruby-IV(assert_equal)


  • source ~/grad_members_20f/members/Takahiro-Nishikawa/ruby_assert_equal.org
8
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
8
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?