Skip to content

Commit

Permalink
Make :full type default
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed May 18, 2015
1 parent c39d421 commit af7520a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 65 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/base/source_data/identifier.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/nanoc/data_sources/filesystem_unified.rb
Expand Up @@ -88,15 +88,15 @@ 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(\.[^\/]+)?$/
regex = @config && @config[:allow_periods_in_identifiers] ? /\/?(index)?(\.[^\/\.]+)?$/ : /\/?index(\.[^\/]+)?$/
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
4 changes: 2 additions & 2 deletions lib/nanoc/data_sources/filesystem_verbose.rb
Expand Up @@ -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
70 changes: 35 additions & 35 deletions spec/nanoc/base/identifier_spec.rb
Expand Up @@ -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 == '<Nanoc::Identifier type=legacy "/foo/bar/">' }
it { should == '<Nanoc::Identifier type=full "/foo/bar">' }
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)
Expand All @@ -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)
Expand All @@ -96,17 +96,17 @@

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)
end
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)
Expand All @@ -115,7 +115,7 @@
end

describe '#=~' do
let(:identifier) { described_class.new('/foo/bar/') }
let(:identifier) { described_class.new('/foo/bar') }

subject { identifier =~ pat }

Expand All @@ -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) }

Expand Down Expand Up @@ -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
Expand All @@ -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' }
Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -267,23 +267,23 @@
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
end
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')
end
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')
Expand Down
28 changes: 14 additions & 14 deletions spec/nanoc/base/views/identifiable_collection_spec.rb
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions test/base/test_item.rb
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions test/base/test_layout.rb
Expand Up @@ -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
Expand Down

0 comments on commit af7520a

Please sign in to comment.