module Test::Unit::AssertionCheckable

Private Instance Methods

add_assertion() click to toggle source
Calls superclass method
# File test-unit-3.3.4/test/test-assertions.rb, line 118
def add_assertion
  if @catch_assertions
    @actual_assertion_count += 1
  else
    super
  end
end
add_failure(message, location=caller, options=nil) click to toggle source
Calls superclass method
# File test-unit-3.3.4/test/test-assertions.rb, line 112
def add_failure(message, location=caller, options=nil)
  unless @catch_assertions
    super
  end
end
check(value, message="") click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 16
def check(value, message="")
  add_assertion
  raise AssertionFailedError.new(message) unless value
end
check_assertions(expect_fail, options={}) { || ... } click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 21
def check_assertions(expect_fail, options={})
  expected_message = options[:expected_message]
  actual_message_normalizer = options[:actual_message_normalizer]
  return_value_expected = options[:return_value_expected]
  @actual_assertion_count = 0
  failed = true
  actual_message = nil
  @catch_assertions = true
  return_value = nil
  begin
    return_value = yield
    failed = false
  rescue AssertionFailedError => error
    return_value = error
    actual_message = error.message
  end
  @catch_assertions = false

  if expect_fail
    message = "Should have failed, but didn't"
  else
    message = "Should not have failed, but did with message\n" +
      "<#{actual_message}>"
  end
  check(expect_fail == failed, message)

  message = "Should have made one assertion but made\n" +
    "<#{@actual_assertion_count}>"
  check(1 == @actual_assertion_count, message)

  if expect_fail
    if actual_message_normalizer
      actual_message = actual_message_normalizer.call(actual_message)
    end
    case expected_message
    when String
      check(expected_message == actual_message,
            "Should have the correct message.\n" +
            "<#{expected_message.inspect}> expected but was\n" +
            "<#{actual_message.inspect}>")
    when Regexp
      check(expected_message =~ actual_message,
            "The message should match correctly.\n" +
            "</#{expected_message.source}/> expected to match\n" +
            "<#{actual_message.inspect}>")
    else
      check(false,
            "Incorrect expected message type in assert_nothing_failed")
    end
  else
    case return_value_expected
    when :dont_care
      # do nothing
    when true
      check(!return_value.nil?, "Should return a value")
    else
      check(return_value.nil?,
            "Should not return a value but returned <#{return_value}>")
    end
  end

  return_value
end
check_fail(expected_message, options={}, &proc) click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 92
def check_fail(expected_message, options={}, &proc)
  check_assertions(true,
                   options.merge(:expected_message => expected_message),
                   &proc)
end
check_fail_exception(expected_message, options={}, &proc) click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 98
def check_fail_exception(expected_message, options={}, &proc)
  normalizer = lambda do |actual_message|
    actual_message.gsub(/(^[^:\n]+:\d+:.+\n?)+\z/, "")
  end
  check_assertions(true,
                   options.merge(:expected_message => expected_message,
                                 :actual_message_normalizer => normalizer),
                   &proc)
end
check_nothing_fails(return_value_expected=false, &proc) click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 85
def check_nothing_fails(return_value_expected=false, &proc)
  check_assertions(false,
                   {:expected_message => nil,
                    :return_value_expected => return_value_expected},
                   &proc)
end
inspect_tag(tag) click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 108
def inspect_tag(tag)
  tag.inspect
end