sfcgeorge / yard-contracts

YARD Plugin for Automatic Param Docs from Contracts.
MIT License
26 stars 3 forks source link

Add tests for contracts that are not built-in #5

Closed egonSchiele closed 9 years ago

egonSchiele commented 9 years ago

You should be able to use custom contracts with yard-contracts!

sfcgeorge commented 9 years ago

Tests added.

I'm ok with the above 2 requirements. Would you prefer it to be different?

The tests I added are now failing on JRuby, I'm guessing because of the inspect vs to_s behaviour. Not sure what to do about this.

waterlink commented 9 years ago

My suggestion: Extract exactly what is different on jruby, I assume it is string value of inspect, and make this abstraction Ruby-version-aware, then just use this abstraction in specs. On Mar 29, 2015 2:35 PM, "Simon George" notifications@github.com wrote:

Tests added.

  • Custom contracts should be in a single file supplied to YARD with the -e flag.
  • Custom contracts must be in the global or Contracts namespace.

I'm ok with the above 2 requirements. Would you prefer it to be different?

The tests I added are now failing on JRuby, I'm guessing because of the inspect vs to_s behaviour. Not sure what to do about this.

— Reply to this email directly or view it on GitHub https://github.com/sfcgeorge/yard-contracts/issues/5#issuecomment-87404676 .

sfcgeorge commented 9 years ago

For reference:

Ruby >= 2

(this is the expected new behaviour)

class Foo
  def to_s
    'baz'
  end
  def self.to_s
    'bar'
  end
end

irb(main):002:0> Foo.inspect
=> "Foo"
irb(main):003:0> Foo.to_s
=> "bar"
irb(main):004:0> Foo.new.inspect
=> "#<Foo:0x007fa7b8869e28>"
irb(main):005:0> Foo.new.to_s
=> "baz"

Ruby < 2

This is the old undesirable behaviour---to_s gets called by inspect for class and object.

irb(main):012:0> Foo.inspect
=> "bar"
irb(main):013:0> Foo.to_s
=> "bar"
irb(main):014:0> Foo.new.inspect
=> "baz"
irb(main):015:0> Foo.new.to_s
=> "baz"

I couldn't figure out why JRuby sometimes works, but mostly it seems to follow the < 2 behaviour. So I came up with a fix for all Rubies < 2 and pushed that. Which means now JRuby and 1.9.3 are supported. I've made the same change as a pull for Contracts and will eventually remove the duplicated InspectWrapper here.