Skip to content

Commit

Permalink
Merge pull request #738 from nanoc/fix-comparisons
Browse files Browse the repository at this point in the history
Fix comparisons
  • Loading branch information
denisdefreyne committed Nov 28, 2015
2 parents 58bba43 + d4590de commit fa59c47
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
7 changes: 2 additions & 5 deletions lib/nanoc/base/entities/document.rb
Expand Up @@ -44,13 +44,10 @@ def hash
self.class.hash ^ identifier.hash
end

def eql?(other)
self.class == other.class && identifier == other.identifier
end

def ==(other)
self.eql?(other)
other.respond_to?(:identifier) && identifier == other.identifier
end
alias_method :eql?, :==
end
end
end
7 changes: 6 additions & 1 deletion lib/nanoc/base/entities/identifier.rb
Expand Up @@ -65,7 +65,12 @@ def initialize(string, params = {})
end

def ==(other)
to_s == other.to_s
case other
when Nanoc::Identifier, String
to_s == other.to_s
else
false
end
end
alias_method :eql?, :==

Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/item_rep_view.rb
Expand Up @@ -12,7 +12,7 @@ def unwrap

# @see Object#==
def ==(other)
item.identifier == other.item.identifier && name == other.name
other.respond_to?(:item) && other.respond_to?(:name) && item == other.item && name == other.name
end
alias_method :eql?, :==

Expand Down
36 changes: 28 additions & 8 deletions spec/nanoc/base/entities/identifier_spec.rb
Expand Up @@ -111,33 +111,53 @@
end

describe '#== and #eql?' do
context 'equal identifiers' do
context 'comparing with equal identifier' do
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { described_class.new('/foo/bar//', type: :legacy) }

it 'is equal to identifier' do
it 'is equal' do
expect(identifier_a).to eq(identifier_b)
expect(identifier_a).to eql(identifier_b)
end
end

it 'is equal to string' do
context 'comparing with equal string' do
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { '/foo/bar/' }

it 'is equal' do
expect(identifier_a).to eq(identifier_b.to_s)
expect(identifier_a).to eql(identifier_b.to_s)
end
end

context 'different identifiers' do
context 'comparing with different identifier' do
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { described_class.new('/baz/qux//', type: :legacy) }

it 'differs from identifier' do
it 'is not equal' do
expect(identifier_a).not_to eq(identifier_b)
expect(identifier_a).not_to eql(identifier_b)
end
end

context 'comparing with different string' do
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { '/baz/qux/' }

it 'differs from string' do
expect(identifier_a).not_to eq(identifier_b.to_s)
expect(identifier_a).not_to eql(identifier_b.to_s)
it 'is not equal' do
expect(identifier_a).not_to eq(identifier_b)
expect(identifier_a).not_to eql(identifier_b)
end
end

context 'comparing with something that is not an identifier' do
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { :donkey }

it 'is not equal' do
expect(identifier_a).not_to eq(identifier_b)
expect(identifier_a).not_to eql(identifier_b)
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/nanoc/base/views/item_rep_view_spec.rb
Expand Up @@ -63,6 +63,16 @@
expect(view).not_to eql(other)
end
end

context 'comparing with something that is not an item rep' do
let(:other_item) { double(:other_item, identifier: '/foo/') }
let(:other) { :donkey }

it 'is not equal' do
expect(view).not_to eq(other)
expect(view).not_to eql(other)
end
end
end

describe '#hash' do
Expand Down

0 comments on commit fa59c47

Please sign in to comment.