module Kernel

Private Instance Methods

assert(value, msg = "expression returned click to toggle source

Assert that value is not nil or false.

# File lib/cutest.rb, line 152
def assert(value, msg = "expression returned #{value.inspect}")
  flunk(msg) unless value
  success
end
assert_equal(actual, expected) click to toggle source

Assert that actual and expected values are equal.

# File lib/cutest.rb, line 158
def assert_equal(actual, expected)
  assert(actual == expected, "#{actual.inspect} != #{expected.inspect}")
end
assert_raise(expected = Exception) { || ... } click to toggle source

Assert that the block raises an expected exception.

# File lib/cutest.rb, line 163
def assert_raise(expected = Exception)
  begin
    yield
  rescue expected => exception
    exception
  ensure
    assert(exception.kind_of?(expected), "got #{exception.inspect} instead")
  end
end
cutest() click to toggle source

Shortcut to access Thread.current.

# File lib/cutest.rb, line 91
def cutest
  Thread.current[:cutest]
end
flunk(message = nil) click to toggle source

Stop the tests and raise an error where the message is the last line executed before flunking.

# File lib/cutest.rb, line 175
def flunk(message = nil)
  backtrace = caller.find { |line| line.include? 'top (required)' }
  exception = Cutest::AssertionFailed.new(message)
  exception.set_backtrace(backtrace)

  raise exception
end
prepare(&block) click to toggle source

Prepare the environment in order to run the tests. This method can be called many times, and each new block is appended to a list of preparation blocks. When a test is executed, all the preparation blocks are ran in the order they were declared. If called without a block, it returns the array of preparation blocks.

# File lib/cutest.rb, line 108
def prepare(&block)
  cutest[:prepare] << block if block_given?
  cutest[:prepare]
end
scope(name = nil, &block) click to toggle source

Create an instance where the block will be evaluated. Recommended to improve isolation between tests.

# File lib/cutest.rb, line 97
def scope(name = nil, &block)
  if !cutest[:scope] || cutest[:scope] == name
    Cutest::Scope.new(&block).call
  end
end
setup(&block) click to toggle source

Setup parameters for the tests. The block passed to setup is evaluated before running each test, and the result of the setup block is passed to the test as a parameter. If the setup and the tests are declared at the same level (in the global scope or in a sub scope), it is possible to use instance variables, but the parameter passing pattern is recommended to ensure there are no side effects.

If the setup blocks are declared in the global scope and the tests are declared in sub scopes, the parameter passing usage is required.

Setup blocks can be defined many times, but each new definition overrides the previous one. It is recommended to split the tests in many different files (the report is per file, not per assertion). Usually one setup block per file is enough, but nothing forbids having different scopes with different setup blocks.

# File lib/cutest.rb, line 128
def setup(&block)
  cutest[:setup] = block if block_given?
  cutest[:setup]
end
success() click to toggle source

Executed when an assertion succeeds.

# File lib/cutest.rb, line 184
def success
  print "."
end
test(name = nil, &block) click to toggle source

Call the prepare and setup blocks before executing the test. Even though the assertions can live anywhere (it’s not mandatory to put them inside test blocks), it is necessary to wrap them in test blocks in order to execute preparation and setup blocks.

# File lib/cutest.rb, line 140
def test(name = nil, &block)
  cutest[:test] = name

  if !cutest[:only] || cutest[:only] == name
    prepare.each { |blk| blk.call }
    block.call(setup && setup.call)
  end

  cutest[:test] = nil
end