Browse files

Update equalizer to only coerce when the class explicit defines a coe…

…rce method
  • Loading branch information...
1 parent cc54744 commit a85e6d1969e61bed6ce63b7e560236767cc718f6 @dkubb committed May 31, 2012
Showing with 31 additions and 18 deletions.
  1. +1 −1 config/flay.yml
  2. +1 −14 lib/veritas/support/equalizer.rb
  3. +29 −3 spec/unit/veritas/equalizer/methods/equal_value_spec.rb
View
2 config/flay.yml
@@ -1,3 +1,3 @@
---
threshold: 44
-total_score: 883
+total_score: 886
View
15 lib/veritas/support/equalizer.rb
@@ -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
View
32 spec/unit/veritas/equalizer/methods/equal_value_spec.rb
@@ -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
@@ -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) }
@@ -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.