Skip to content

Commit

Permalink
Update equalizer to only coerce when the class explicit defines a coe…
Browse files Browse the repository at this point in the history
…rce method
  • Loading branch information
dkubb committed May 31, 2012
1 parent cc54744 commit a85e6d1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config/flay.yml
@@ -1,3 +1,3 @@
---
threshold: 44
total_score: 883
total_score: 886
15 changes: 1 addition & 14 deletions lib/veritas/support/equalizer.rb
Expand Up @@ -115,24 +115,11 @@ def eql?(other)
#
# @api public
def ==(other)
other = coerce(other)
other = coerce(other) if respond_to?(:coerce, true)
return false unless self.class <=> other.class
cmp?(__method__, other)
end

private

# Coerce the object into something that can be compared
#
# @param [Object] other
#
# @return [Object]
#
# @api private
def coerce(other)
other
end

end # module Methods
end # class Equalizer
end # module Veritas
32 changes: 29 additions & 3 deletions spec/unit/veritas/equalizer/methods/equal_value_spec.rb
Expand Up @@ -5,14 +5,20 @@
describe Veritas::Equalizer::Methods, '#==' do
subject { object == other }

let(:object) { described_class.new }
let(:object) { described_class.new(true) }

let(:described_class) do
Class.new do
include Veritas::Equalizer::Methods

attr_reader :boolean

def initialize(boolean)
@boolean = boolean
end

def cmp?(comparator, other)
!!(comparator and other)
boolean.send(comparator, other.boolean)
end
end
end
Expand All @@ -38,7 +44,7 @@ def cmp?(comparator, other)
end

context 'with an equivalent object of a subclass' do
let(:other) { Class.new(described_class).new }
let(:other) { Class.new(described_class).new(true) }

it { should be(true) }

Expand All @@ -56,4 +62,24 @@ def cmp?(comparator, other)
should eql(other == object)
end
end

context 'with an equivalent object after coercion' do
let(:other) { Object.new }

before do
# declare a private #coerce method
described_class.class_eval do
def coerce(other)
self.class.new(!!other)
end
private :coerce
end
end

it { should be(true) }

it 'is not symmetric' do
should_not eql(other == object)
end
end
end

0 comments on commit a85e6d1

Please sign in to comment.