Sha256: 736e6cdec712084edef99003d37e2e4bff04a0b454a76b435b8190338d04bbbc

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'test/unit'
$LOAD_PATH << "../lib"
require 'contract'

class TC_Exception < Test::Unit::TestCase
  def test_extract_method_name()
    { "test_provides_each(ListContract)" => "test_provides_each",
      "test_foo(Foo::Bar)" => "test_foo",
      "test_bar(#<Class:0x2ba5e08>)" => "test_bar"
    }.each do |from, to|
      assert_equal(to, Contract.extract_method_name(from))
    end
  end

  def test_fault_to_exception()
    name, backtrace, message, object, contract =
      "test_name(Contract)", caller, "message", Object.new,
      Class.new(Contract)
    exception = Exception.new(message)
    exception.set_backtrace(backtrace)

    [ Test::Unit::Failure.new(name, backtrace, message),
      Test::Unit::Error.new(name, exception)
    ].each do |fault|
      result = Contract.fault_to_exception(fault, object, contract)

      assert_kind_of(Contract::ContractException, result)
      assert_equal("test_name", result.test_method)
      assert_equal(backtrace, result.backtrace)
      assert_equal(message, result.original_message)
      assert_equal(object, result.test_object)
      assert_equal(contract, result.test_contract)

      case fault
        when Test::Unit::Failure then
          assert_instance_of(Contract::ContractMismatch, result)
        when Test::Unit::Error then
          assert_instance_of(Contract::ContractError, result)
          assert_equal(exception.class, result.type)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-contract-0.1.1 test/tc_exception.rb