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(#)" => "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