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

マルチスケールシミュレーション特論:第8回(assert)

Last updated at Posted at 2020-12-30

assert

2つの引数をとり,それらが同じ値かどうかを出力するような関数assert_equalを作る.同様に,それらが異なる値かどうかを出力するような関数assert_not_equalを作る.

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

def assert_equal(expected, result)
  puts_vals(expected, result)
  if expected == result
    print "succeeded in #{__method__}.\n".green
  else
    print "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

他のソースコードからassertを利用する

require_assert.rb
require './assert'

assert_equal(4, 2 * 2)
assert_not_equal('Muku', 'Miku')

参考文献

チャート式ruby-IV(assert_equal)


  • source ~/grad_members_20f/members/ryuta-kikuchi/qiita_articles/lecture8.org
7
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
7
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?