In Test Driven Development (TDD), it is basic to repeat simple tests over and over again.
In this article, I'll create a function assert \ _equal that checks if an expression or value is the same as expected, and use it to create simple test code.
Assert
Equal
First, make sure that the values of expected
and result
are equal.
assert_equal.rb
def assert_equal(expected, result)
return expected == result
end
p assert_equal(1, 1)
> ruby assert_equal.rb
-> true
Then use the colorize
library to color the output for visual clarity.
assert_equal.rb
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)
The result should be green true for the first and red false for the second.
Make a few improvements.
--Output the expected and result values of the arguments --Changed the processing of assert \ _equal to the following - true: print "succeeded in assert_equal.\n".green - false: print "failed in assert_equal.\n".red
assert_equal.rb
require 'colorize'
def puts_vals(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
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
assert_equal(1, 1)
assert_equal(1, 2)
The output is
> ruby assert_equal.rb
expected :: 1
result :: 1
succeeded in assert_equal.
expected :: 1
result :: 2
failed in assert_equal.
(The color has not changed on Qiita.)
Not Equal
Now create an assert \ _not \ _equal function that returns false if expected
and result
are equal, and true if they are different.
assert_equal.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
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
The output is
> ruby assert_equal.rb
expected :: 1
result :: 1
succeeded in assert_equal.
expected :: 1
result :: 2
failed in assert_equal.
expected :: 1
result :: 2
succeeded in assert_not_equal.
expected :: 1
result :: 1
failed in assert_not_equal.
Since this assert \ _equal and assert \ _not \ _equal are convenient, I would like to use them for other code as well. However, as it is, the tests in the code will also be executed, so
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
Change the test part like this. By writing like this, if the file name (FILE) where the code is written matches the file name ($ PROGRAM \ _FILE) where PROGRAM is running, the contents can be executed.
Chart type ruby-IV (assert \ _equal)
Recommended Posts