assert_equal
assert \ _equal is a function that confirms whether it is equal or not, and this time we confirm how this function behaves.
assert_equal.rb
p 1==1
def assert_equal(expected,result)
return expected==result
end
p assert_equal(1,1)
The output result is as follows.
true
true
colorize
This is an extension of String that colors the output of the terminal. The usual coloring method has the disadvantages that the code is difficult to remember and read, but this gem can solve those problems.
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)
Since the lecture dealt with assert \ _not \ _equal, which is an application of assert \ _equal, I would like to implement it using colorize. The completed code is below.
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)
Then, I would like to improve the completeness of assert \ _equal by using the contents of assert \ _equal and colorize. The name is assert \ _equal \ _fin. First, I would like to improve the input and output parts. The code below is a function that displays the expected value and the actual output value.
def puts_vals(expected, result)
puts "expected :: #{expected}" #Expected value
puts "result :: #{result}" #Actually output value
end
Next, we will improve the functions assert \ _equal and assert \ _not \ _equal. If the expected value and the output value match (assert \ _not \ _equal does not match), succeeded in assert \ _equal (assert \ _not \ _equal), and if they do not match (assert \ _not \ _equal match), failed in assert \ Outputs _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
When using in another script
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
Because the part of is unnecessary
if $PROGRAM_NAME == __FILE__
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
end
By entering, it is possible to execute only when the entered file name and the executed file name exactly match.
From the above, assert \ _equal \ _fin is as follows.
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
Recommended Posts