This is a lecture memo of the special lecture on multi-scale simulation. The index of lecture memos is here
The reference materials this time are Chart type ruby-appendix-IV (assert \ _equal) and Chart type ruby-appendix-V (rubular).
Reference article is here
assert_equal
Create a function called assert \ _equal. This is a function that compares the expected output with the actual output and makes sure they are equal. In test-driven development, try to output for the time being, and if it is different from the expected output, correct it. Assert \ _equal is useful in such a development method. (I can't say it unconditionally)
I will create it immediately.
assert_equal
> emacs assert_equal.rb
First of all, from the outline of the function.
def assert_equal(expected, result)
return expected == result
end
p assert_equal(1,1)
This returns true if expected and result are equal, false if they are different.
colorize
If colorize is true, it will be output in green, and if it is false, it will be output in red. It is important to make it easy to understand qualifications.
Call colorize in the previous program
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)
Now you can output green if true and red if false.
Although it derails, the following colors can be specified in 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]
This alone is not enough, so add the following two points.
--Outputs the values of expected and result that are the arguments of the function --Output the result in sentences
require 'colorize'
def assert_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected==result
puts "succeeded in assert_equal.\n".green
else
puts "failed in assert_equal.\n".red
end
end
assert_equal(1, 1)
assert_equal(1, 2)
When I run it
> ruby assert_equal.rb
expected :: 1
result :: 1
succeeded in assert_equal.
expected :: 1
result :: 2
failed in assert_equal.
The color of the characters was also output correctly.
assert_not_equal
Create a function assert \ _not \ _equal that returns the reverse version of assert \ _equal, true if not equal, false if equal.
require 'colorize'
def assert_not_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected != result
puts "succeeded in assert_not_equal.\n".green
else
puts "failed in assert_not_equal.\n".red
end
end
assert_not_equal(1,2)
assert_not_equal(1,1)
> ruby assert_not_equal_ro.rb
expected :: 1
result :: 2
succeeded in assert_not_equal.
expected :: 1
result :: 1
failed in assert_not_equal.
It was output without any problem. A feeling of strangeness.
Speaking of duplicates in the function, is it a print statement? Since the method names of assert \ _equal and asset \ _not \ _equal are solid, I wonder if that is annoying.
def assert_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected == result
puts "succeeded in #{__method__}.\n".green
else
puts "failed in #{__method__}.\n".red
end
end
def assert_not_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected != result
puts "succeeded in #{__method__}.\n".green
else
puts "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)
Such a thing.
I will continue to use assert \ _equal, so I want to omit the test program.
Therefore
if $PROGRAM_NAME == __FILE__
assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)
end
By doing this, it is possible to avoid executing the test program.
The assert \ _equal program I created is as follows.
require 'colorize'
def assert_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected == result
puts "succeeded in #{__method__}.\n".green
else
puts "failed in #{__method__}.\n".red
end
end
def assert_not_equal(expected, result)
puts "expected :: #{expected}"
puts "result :: #{result}"
if expected != result
puts "succeeded in #{__method__}.\n".green
else
puts "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
The model answer program is as follows.
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
I couldn't get the perfect answer so far. Insufficient power.
Reference article is here
Rubular
Rubular: http://rubular.com
A tool that you can use to practice using regular expressions. The usage is summarized in the quick reference, so you can easily refer to it.
I personally feel the convenience of regular expressions, and I have actually used them, but I haven't learned them. Let's take this opportunity to study.
Next time is Chart type ruby-IV (Recursive Fibonacci).
Recommended Posts