From af7520a284f1939a9a63e68998a3e3aa59b65e2f Mon Sep 17 00:00:00 2001 From: Denis Defreyne Date: Mon, 18 May 2015 21:21:41 +0200 Subject: [PATCH] Make :full type default --- lib/nanoc/base/source_data/identifier.rb | 2 +- lib/nanoc/data_sources/filesystem_unified.rb | 4 +- lib/nanoc/data_sources/filesystem_verbose.rb | 4 +- spec/nanoc/base/identifier_spec.rb | 70 +++++++++---------- .../views/identifiable_collection_spec.rb | 28 ++++---- test/base/test_item.rb | 6 -- test/base/test_layout.rb | 6 +- 7 files changed, 55 insertions(+), 65 deletions(-) diff --git a/lib/nanoc/base/source_data/identifier.rb b/lib/nanoc/base/source_data/identifier.rb index 89983cd665..899a51f15e 100644 --- a/lib/nanoc/base/source_data/identifier.rb +++ b/lib/nanoc/base/source_data/identifier.rb @@ -16,7 +16,7 @@ def self.from(obj) end def initialize(string, params = {}) - @type = params.fetch(:type, :legacy) + @type = params.fetch(:type, :full) case @type when :legacy diff --git a/lib/nanoc/data_sources/filesystem_unified.rb b/lib/nanoc/data_sources/filesystem_unified.rb index eac233c241..9d7a9535f0 100644 --- a/lib/nanoc/data_sources/filesystem_unified.rb +++ b/lib/nanoc/data_sources/filesystem_unified.rb @@ -88,7 +88,7 @@ def filename_for(base_filename, ext) # the given directory name off the filename. def identifier_for_filename(filename) if config[:identifier_type] == 'full' - return Nanoc::Identifier.new(filename, type: :full) + return Nanoc::Identifier.new(filename) end if filename =~ /(^|\/)index(\.[^\/]+)?$/ @@ -96,7 +96,7 @@ def identifier_for_filename(filename) else regex = @config && @config[:allow_periods_in_identifiers] ? /\.[^\/\.]+$/ : /\.[^\/]+$/ end - filename.sub(regex, '').__nanoc_cleaned_identifier + Nanoc::Identifier.new(filename.sub(regex, ''), type: :legacy) end end end diff --git a/lib/nanoc/data_sources/filesystem_verbose.rb b/lib/nanoc/data_sources/filesystem_verbose.rb index 1ed7f78fce..c36fee1505 100644 --- a/lib/nanoc/data_sources/filesystem_verbose.rb +++ b/lib/nanoc/data_sources/filesystem_verbose.rb @@ -62,10 +62,10 @@ def filename_for(base_filename, ext) # See {Nanoc::DataSources::Filesystem#identifier_for_filename}. def identifier_for_filename(filename) if config[:identifier_type] == 'full' - return Nanoc::Identifier.new(filename, type: :full) + return Nanoc::Identifier.new(filename) end - filename.sub(/[^\/]+\.yaml$/, '') + Nanoc::Identifier.new(filename.sub(/[^\/]+\.yaml$/, ''), type: :legacy) end end end diff --git a/spec/nanoc/base/identifier_spec.rb b/spec/nanoc/base/identifier_spec.rb index 2ebe06b1d5..919af70f95 100644 --- a/spec/nanoc/base/identifier_spec.rb +++ b/spec/nanoc/base/identifier_spec.rb @@ -4,68 +4,68 @@ describe '#initialize' do context 'legacy type' do it 'does not convert already clean paths' do - expect(described_class.new('/foo/bar/').to_s).to eql('/foo/bar/') + expect(described_class.new('/foo/bar/', type: :legacy).to_s).to eql('/foo/bar/') end it 'prepends slash if necessary' do - expect(described_class.new('foo/bar/').to_s).to eql('/foo/bar/') + expect(described_class.new('foo/bar/', type: :legacy).to_s).to eql('/foo/bar/') end it 'appends slash if necessary' do - expect(described_class.new('/foo/bar').to_s).to eql('/foo/bar/') + expect(described_class.new('/foo/bar', type: :legacy).to_s).to eql('/foo/bar/') end it 'removes double slashes at start' do - expect(described_class.new('//foo/bar/').to_s).to eql('/foo/bar/') + expect(described_class.new('//foo/bar/', type: :legacy).to_s).to eql('/foo/bar/') end it 'removes double slashes at end' do - expect(described_class.new('/foo/bar//').to_s).to eql('/foo/bar/') + expect(described_class.new('/foo/bar//', type: :legacy).to_s).to eql('/foo/bar/') end end context 'full type' do it 'refuses string not starting with a slash' do - expect { described_class.new('foo', type: :full) }.to raise_error('Invalid identifier (does not start with a slash): "foo"') + expect { described_class.new('foo') }.to raise_error('Invalid identifier (does not start with a slash): "foo"') end it 'has proper string representation' do - expect(described_class.new('/foo', type: :full).to_s).to eql('/foo') + expect(described_class.new('/foo').to_s).to eql('/foo') end end end describe '#to_s' do it 'returns immutable string' do - expect { described_class.new('foo/').to_s << 'lols' }.to raise_error - expect { described_class.new('/foo', type: :full).to_s << 'lols' }.to raise_error + expect { described_class.new('foo/', type: :legacy).to_s << 'lols' }.to raise_error + expect { described_class.new('/foo').to_s << 'lols' }.to raise_error end end describe '#to_str' do it 'returns immutable string' do - expect { described_class.new('foo/bar/').to_str << 'lols' }.to raise_error + expect { described_class.new('/foo/bar').to_str << 'lols' }.to raise_error end end describe 'Comparable' do it 'can be compared' do - expect(described_class.new('foo/bar/') <= '/qux/').to eql(true) + expect(described_class.new('/foo/bar') <= '/qux').to eql(true) end end describe '#inspect' do - let(:identifier) { described_class.new('foo/bar/') } + let(:identifier) { described_class.new('/foo/bar') } subject { identifier.inspect } - it { should == '' } + it { should == '' } end describe '#== and #eql?' do context 'equal identifiers' do - let(:identifier_a) { described_class.new('//foo/bar/') } - let(:identifier_b) { described_class.new('/foo/bar//') } + 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 expect(identifier_a).to eq(identifier_b) @@ -79,8 +79,8 @@ end context 'different identifiers' do - let(:identifier_a) { described_class.new('//foo/bar/') } - let(:identifier_b) { described_class.new('/baz/qux//') } + 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 expect(identifier_a).not_to eq(identifier_b) @@ -96,8 +96,8 @@ describe '#hash' do context 'equal identifiers' do - let(:identifier_a) { described_class.new('//foo/bar/') } - let(:identifier_b) { described_class.new('/foo/bar//') } + let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) } + let(:identifier_b) { described_class.new('/foo/bar//', type: :legacy) } it 'is the same' do expect(identifier_a.hash == identifier_b.hash).to eql(true) @@ -105,8 +105,8 @@ end context 'different identifiers' do - let(:identifier_a) { described_class.new('//foo/bar/') } - let(:identifier_b) { described_class.new('/monkey/') } + let(:identifier_a) { described_class.new('//foo/bar/', type: :legacy) } + let(:identifier_b) { described_class.new('/monkey/', type: :legacy) } it 'is different' do expect(identifier_a.hash == identifier_b.hash).to eql(false) @@ -115,7 +115,7 @@ end describe '#=~' do - let(:identifier) { described_class.new('/foo/bar/') } + let(:identifier) { described_class.new('/foo/bar') } subject { identifier =~ pat } @@ -133,29 +133,29 @@ context 'given a string' do context 'matching string' do - let(:pat) { '/foo/*/' } + let(:pat) { '/foo/*' } it { is_expected.to eql(0) } end context 'non-matching string' do - let(:pat) { '/qux/*/' } + let(:pat) { '/qux/*' } it { is_expected.to eql(nil) } end end end describe '#<=>' do - let(:identifier) { described_class.new('/foo/bar/') } + let(:identifier) { described_class.new('/foo/bar') } it 'compares by string' do - expect(identifier <=> '/foo/aarghh/').to eql(1) - expect(identifier <=> '/foo/bar/').to eql(0) - expect(identifier <=> '/foo/qux/').to eql(-1) + expect(identifier <=> '/foo/aarghh').to eql(1) + expect(identifier <=> '/foo/bar').to eql(0) + expect(identifier <=> '/foo/qux').to eql(-1) end end describe '#prefix' do - let(:identifier) { described_class.new('/foo', type: :full) } + let(:identifier) { described_class.new('/foo') } subject { identifier.prefix(prefix) } @@ -198,7 +198,7 @@ subject { identifier.with_ext(ext) } context 'legacy type' do - let(:identifier) { described_class.new('/foo/') } + let(:identifier) { described_class.new('/foo/', type: :legacy) } let(:ext) { 'html' } it 'raises an error' do @@ -207,7 +207,7 @@ end context 'identifier with no extension' do - let(:identifier) { described_class.new('/foo', type: :full) } + let(:identifier) { described_class.new('/foo') } context 'extension without dot given' do let(:ext) { 'html' } @@ -235,7 +235,7 @@ end context 'identifier with extension' do - let(:identifier) { described_class.new('/foo.md', type: :full) } + let(:identifier) { described_class.new('/foo.md') } context 'extension without dot given' do let(:ext) { 'html' } @@ -267,7 +267,7 @@ subject { identifier.without_ext } context 'legacy type' do - let(:identifier) { described_class.new('/foo/') } + let(:identifier) { described_class.new('/foo/', type: :legacy) } it 'raises an error' do expect { subject }.to raise_error @@ -275,7 +275,7 @@ end context 'identifier with no extension' do - let(:identifier) { described_class.new('/foo', type: :full) } + let(:identifier) { described_class.new('/foo') } it 'does nothing' do expect(subject).to eql('/foo') @@ -283,7 +283,7 @@ end context 'identifier with extension' do - let(:identifier) { described_class.new('/foo.md', type: :full) } + let(:identifier) { described_class.new('/foo.md') } it 'removes the extension' do expect(subject).to eql('/foo') diff --git a/spec/nanoc/base/views/identifiable_collection_spec.rb b/spec/nanoc/base/views/identifiable_collection_spec.rb index 9d67aaa237..c1209b6ecc 100644 --- a/spec/nanoc/base/views/identifiable_collection_spec.rb +++ b/spec/nanoc/base/views/identifiable_collection_spec.rb @@ -11,9 +11,9 @@ describe '#unwrap' do let(:wrapped) do Nanoc::Int::IdentifiableCollection.new(config).tap do |arr| - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz/')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz')) end end @@ -25,9 +25,9 @@ describe '#each' do let(:wrapped) do Nanoc::Int::IdentifiableCollection.new(config).tap do |arr| - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz/')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz')) end end @@ -39,9 +39,9 @@ describe '#size' do let(:wrapped) do Nanoc::Int::IdentifiableCollection.new(config).tap do |arr| - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar/')) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz/')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/foo')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/bar')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/baz')) end end @@ -52,11 +52,11 @@ describe '#[]' do let(:page_object) do - double(:identifiable, identifier: Nanoc::Identifier.new('/page.erb', type: :full)) + double(:identifiable, identifier: Nanoc::Identifier.new('/page.erb')) end let(:home_object) do - double(:identifiable, identifier: Nanoc::Identifier.new('/home.erb', type: :full)) + double(:identifiable, identifier: Nanoc::Identifier.new('/home.erb')) end let(:wrapped) do @@ -114,9 +114,9 @@ describe '#find_all' do let(:wrapped) do Nanoc::Int::IdentifiableCollection.new(config).tap do |arr| - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/about.css', type: :full)) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/about.md', type: :full)) - arr << double(:identifiable, identifier: Nanoc::Identifier.new('/style.css', type: :full)) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/about.css')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/about.md')) + arr << double(:identifiable, identifier: Nanoc::Identifier.new('/style.css')) end end diff --git a/test/base/test_item.rb b/test/base/test_item.rb index c1ed2ca7d6..ff83b2053b 100644 --- a/test/base/test_item.rb +++ b/test/base/test_item.rb @@ -8,12 +8,6 @@ def test_initialize_with_attributes_with_string_keys assert_equal 'xyz', item.attributes[:abc] end - def test_initialize_with_unclean_identifier - item = Nanoc::Int::Item.new('foo', {}, '/foo') - - assert_equal '/foo/', item.identifier.to_s - end - def test_reference item = Nanoc::Int::Item.new( 'content', diff --git a/test/base/test_layout.rb b/test/base/test_layout.rb index 00071c3dfe..49d82e8dc8 100644 --- a/test/base/test_layout.rb +++ b/test/base/test_layout.rb @@ -3,12 +3,8 @@ class Nanoc::Int::LayoutTest < Nanoc::TestCase def test_initialize # Make sure attributes are cleaned - layout = Nanoc::Int::Layout.new('content', { 'foo' => 'bar' }, '/foo/') + layout = Nanoc::Int::Layout.new('content', { 'foo' => 'bar' }, '/foo') assert_equal({ foo: 'bar' }, layout.attributes) - - # Make sure identifier is cleaned - layout = Nanoc::Int::Layout.new('content', { 'foo' => 'bar' }, 'foo') - assert_equal(Nanoc::Identifier.new('/foo/'), layout.identifier) end def test_lookup_with_known_attribute