egonSchiele / contracts.ruby

Contracts for Ruby.
http://egonschiele.github.com/contracts.ruby
BSD 2-Clause "Simplified" License
1.44k stars 82 forks source link

ContractError should also show the arguments that caused the error #231

Open egonSchiele opened 8 years ago

egonSchiele commented 8 years ago

Output:

contracts.ruby (adit/215) $ cat test.rb
require "contracts"
include Contracts

Contract Num => Num
def incr x
  "Sdf"
end

incr(23)
contracts.ruby (adit/215) $ ruby test.rb
/Users/abhargava/contracts.ruby/lib/contracts.rb:45:in `block in <class:Contract>': Contract violation for return value: (ReturnContractError)
        Expected: Num,
        Actual: "Sdf"
        Value guarded in: Object::incr
        With Contract: Num => Num
        All arguments: [23]
        At: test.rb:5
    from /Users/abhargava/contracts.ruby/lib/contracts.rb:155:in `call'
    from /Users/abhargava/contracts.ruby/lib/contracts.rb:155:in `failure_callback'
    from /Users/abhargava/contracts.ruby/lib/contracts/call_with.rb:82:in `call_with'
    from /Users/abhargava/contracts.ruby/lib/contracts/method_handler.rb:138:in `block in redefine_method'
    from test.rb:9:in `<main>'

cc @waterlink @beezee (for issue #215)

egonSchiele commented 8 years ago

Oh man, I forgot I need to update the output in the Cucumber tests :/

waterlink commented 8 years ago

This is a very useful feature