egonSchiele / contracts.ruby

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

Pretty-print contracts in error messages 💅 #289

Closed frewsxcv closed 5 years ago

frewsxcv commented 6 years ago

Why?

I work on projects that uses contracts.ruby extensively and we have some gnarly contracts. When contract violations happen, we get errors that span many lines, without any indentation or formatting. Having some pretty-printing would greatly help readability when debugging contract violations.

Example!

In addition to the example below, see the new .feature test added in the pull request.

Before

Contract violation for return value: (ReturnContractError)
      Expected: {:a=>(String or Symbol), :b=>(String or Symbol), :c=>(String or Symbol), :d=>(String or Symbol), :e=>(String or Symbol), :f=>(String or Symbol), :g=>(String or Symbol)},
      Actual: {:a=>["foo", "foo"], :b=>["foo", "foo"], :c=>["foo", "foo"], :d=>["foo", "foo"], :e=>["foo", "foo"], :f=>["foo", "foo"], :g=>["foo", "foo"]}
      Value guarded in: Example::run
      With Contract: None => Hash
      At: example.rb:17

After

Contract violation for return value: (ReturnContractError)
      Expected: {:a=>(String or Symbol),
                 :b=>(String or Symbol),
                 :c=>(String or Symbol),
                 :d=>(String or Symbol),
                 :e=>(String or Symbol),
                 :f=>(String or Symbol),
                 :g=>(String or Symbol)},
      Actual: {:a=>["foo", "foo"],
               :b=>["foo", "foo"],
               :c=>["foo", "foo"],
               :d=>["foo", "foo"],
               :e=>["foo", "foo"],
               :f=>["foo", "foo"],
               :g=>["foo", "foo"]}
      Value guarded in: Example::run
      With Contract: None => Hash
      At: example.rb:17
frewsxcv commented 5 years ago

@egonSchiele I know you're busy, and I know the burnout of maintaining open source stuff, so no hard feelings if you don't get to this 🙏 Just wanted to say that if you need help maintaining this, I'm a little familiar with the internals of this library and would be happy to help!

egonSchiele commented 5 years ago

Hey there, sorry for my late response. Thanks for the PR! Yes, I haven't had much time for open source this year so would appreciate any help :) If you see an issue that you want to help with, please submit a PR! Or if you see a PR you think should get merged, just ping me on it.