Skip to content

Commit

Permalink
Remove type coercion in #eql?
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 2, 2016
1 parent c5e8c9b commit 5ed5f20
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 24 deletions.
6 changes: 5 additions & 1 deletion lib/nanoc/base/entities/document.rb
Expand Up @@ -69,7 +69,11 @@ def hash
def ==(other)
other.respond_to?(:identifier) && identifier == other.identifier
end
alias eql? ==

contract C::Any => C::Bool
def eql?(other)
other.is_a?(self.class) && identifier == other.identifier
end
end
end
end
6 changes: 5 additions & 1 deletion lib/nanoc/base/entities/identifier.rb
Expand Up @@ -76,7 +76,11 @@ def ==(other)
false
end
end
alias eql? ==

contract C::Any => C::Bool
def eql?(other)
other.is_a?(self.class) && to_s == other.to_s
end

contract C::None => C::Num
def hash
Expand Down
8 changes: 7 additions & 1 deletion lib/nanoc/base/views/item_rep_view.rb
Expand Up @@ -15,7 +15,13 @@ def unwrap
def ==(other)
other.respond_to?(:item) && other.respond_to?(:name) && item == other.item && name == other.name
end
alias eql? ==

# @see Object#eql?
def eql?(other)
other.is_a?(self.class) &&
item.eql?(other.item) &&
name.eql?(other.name)
end

# @see Object#hash
def hash
Expand Down
6 changes: 5 additions & 1 deletion lib/nanoc/base/views/mixins/document_view_mixin.rb
Expand Up @@ -18,7 +18,11 @@ def unwrap
def ==(other)
other.respond_to?(:identifier) && identifier == other.identifier
end
alias eql? ==

# @see Object#eql?
def eql?(other)
other.is_a?(self.class) && identifier.eql?(other.identifier)
end

# @see Object#hash
def hash
Expand Down
20 changes: 16 additions & 4 deletions spec/nanoc/base/entities/identifier_spec.rb
Expand Up @@ -128,8 +128,11 @@
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { described_class.new('/foo/bar//', type: :legacy) }

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

it 'is eql?' do
expect(identifier_a).to eql(identifier_b)
end
end
Expand All @@ -138,18 +141,24 @@
let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) }
let(:identifier_b) { '/foo/bar/' }

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

it 'is not eql?' do
expect(identifier_a).not_to eql(identifier_b.to_s)
end
end

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 'is not equal' do
it 'is not ==' do
expect(identifier_a).not_to eq(identifier_b)
end

it 'is not eql?' do
expect(identifier_a).not_to eql(identifier_b)
end
end
Expand All @@ -160,6 +169,9 @@

it 'is not equal' do
expect(identifier_a).not_to eq(identifier_b)
end

it 'is not eql?' do
expect(identifier_a).not_to eql(identifier_b)
end
end
Expand Down
33 changes: 24 additions & 9 deletions spec/nanoc/base/views/document_view_spec.rb
Expand Up @@ -33,44 +33,59 @@
context 'comparing with document with same identifier' do
let(:other) { entity_class.new('content', {}, '/asdf/') }

it 'is equal' do
it 'is ==' do
expect(view).to eq(other)
expect(view).to eql(other)
end

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

context 'comparing with document with different identifier' do
let(:other) { entity_class.new('content', {}, '/fdsa/') }

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

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

context 'comparing with document view with same identifier' do
let(:other) { Nanoc::LayoutView.new(entity_class.new('content', {}, '/asdf/'), nil) }
let(:other) { other_view_class.new(entity_class.new('content', {}, '/asdf/'), nil) }

it 'is equal' do
it 'is ==' do
expect(view).to eq(other)
expect(view).to eql(other)
end

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

context 'comparing with document view with different identifier' do
let(:other) { Nanoc::LayoutView.new(entity_class.new('content', {}, '/fdsa/'), nil) }
let(:other) { other_view_class.new(entity_class.new('content', {}, '/fdsa/'), nil) }

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

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

context 'comparing with other object' do
let(:other) { nil }

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

it 'is not eql?' do
expect(view).not_to eql(other)
end
end
Expand Down
24 changes: 18 additions & 6 deletions spec/nanoc/base/views/item_rep_view_spec.rb
Expand Up @@ -32,18 +32,24 @@
let(:other_item) { double(:other_item, identifier: '/foo/') }
let(:other) { double(:other_item_rep, item: other_item, name: :jacques) }

it 'is equal' do
it 'is ==' do
expect(view).to eq(other)
expect(view).to eql(other)
end

it 'is eql?' do
expect(view).not_to eql(other)
end
end

context 'comparing with item rep with different identifier' do
let(:other_item) { double(:other_item, identifier: '/bar/') }
let(:other) { double(:other_item_rep, item: other_item, name: :jacques) }

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

it 'is not eql?' do
expect(view).not_to eql(other)
end
end
Expand All @@ -52,8 +58,11 @@
let(:other_item) { double(:other_item, identifier: '/foo/') }
let(:other) { double(:other_item_rep, item: other_item, name: :marvin) }

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

it 'is not eql?' do
expect(view).not_to eql(other)
end
end
Expand All @@ -62,9 +71,12 @@
let(:other_item) { double(:other_item, identifier: '/foo/') }
let(:other) { described_class.new(double(:other_item_rep, item: other_item, name: :jacques), view_context) }

it 'is equal' do
it 'is ==' do
expect(view).to eq(other)
expect(view).to eql(other)
end

it 'is eql?' do
expect(view).not_to eql(other)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/nanoc/base/views/item_view_spec.rb
@@ -1,5 +1,6 @@
describe Nanoc::ItemWithRepsView do
let(:entity_class) { Nanoc::Int::Item }
let(:other_view_class) { Nanoc::LayoutView }
it_behaves_like 'a document view'

let(:view_context) { Nanoc::ViewContext.new(reps: reps, items: items, dependency_tracker: dependency_tracker, compiler: compiler) }
Expand Down
1 change: 1 addition & 0 deletions spec/nanoc/base/views/layout_view_spec.rb
@@ -1,4 +1,5 @@
describe Nanoc::LayoutView do
let(:entity_class) { Nanoc::Int::Layout }
let(:other_view_class) { Nanoc::ItemWithRepsView }
it_behaves_like 'a document view'
end
2 changes: 1 addition & 1 deletion spec/nanoc/helpers/blogging_spec.rb
Expand Up @@ -33,7 +33,7 @@
end

it 'returns the two articles in descending order' do
expect(subject.map(&:identifier)).to eql(['/2/', '/1/'])
expect(subject.map(&:identifier)).to eq(['/2/', '/1/'])
end
end

Expand Down

0 comments on commit 5ed5f20

Please sign in to comment.