Skip to content

Commit

Permalink
Mark views as frozen
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jan 10, 2016
1 parent 20de3c4 commit bd7f52c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/item_without_reps_view.rb
Expand Up @@ -33,7 +33,7 @@ def parent

parent = @context.items[parent_identifier]

parent && self.class.new(parent, @context).freeze
parent && self.class.new(parent, @context)
end

# @return [Boolean] True if the item is binary, false otherwise
Expand Down
14 changes: 14 additions & 0 deletions lib/nanoc/base/views/view.rb
Expand Up @@ -8,5 +8,19 @@ def initialize(context)
def _context
@context
end

# @api private
def unwrap
raise NotImplementedError
end

# True if the wrapped object is frozen; false otherwise.
#
# @return [Boolean]
#
# @see Object#frozen?
def frozen?
unwrap.frozen?
end
end
end
13 changes: 13 additions & 0 deletions spec/nanoc/base/views/config_view_spec.rb
Expand Up @@ -5,6 +5,19 @@

let(:view) { described_class.new(config, nil) }

describe '#frozen?' do
subject { view.frozen? }

context 'non-frozen config' do
it { is_expected.to be(false) }
end

context 'frozen config' do
before { config.freeze }
it { is_expected.to be(true) }
end
end

describe '#[]' do
subject { view[key] }

Expand Down
16 changes: 16 additions & 0 deletions spec/nanoc/base/views/document_view_spec.rb
@@ -1,4 +1,20 @@
shared_examples 'a document view' do
describe '#frozen?' do
let(:document) { entity_class.new('content', {}, '/asdf/') }
let(:view) { described_class.new(document, nil) }

subject { view.frozen? }

context 'non-frozen document' do
it { is_expected.to be(false) }
end

context 'frozen document' do
before { document.freeze }
it { is_expected.to be(true) }
end
end

describe '#== and #eql?' do
let(:document) { entity_class.new('content', {}, '/asdf/') }
let(:view) { described_class.new(document, nil) }
Expand Down
24 changes: 24 additions & 0 deletions spec/nanoc/base/views/identifiable_collection_view_spec.rb
Expand Up @@ -8,6 +8,30 @@
{ string_pattern_type: 'glob' }
end

describe '#frozen?' 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'))
end
end

subject { view.frozen? }

context 'non-frozen collection' do
it { is_expected.to be(false) }
end

context 'frozen collection' do
before do
wrapped.each { |o| expect(o).to receive(:freeze) }
wrapped.freeze
end

it { is_expected.to be(true) }
end
end

describe '#unwrap' do
let(:wrapped) do
Nanoc::Int::IdentifiableCollection.new(config).tap do |arr|
Expand Down
13 changes: 13 additions & 0 deletions spec/nanoc/base/views/item_rep_collection_view_spec.rb
Expand Up @@ -17,6 +17,19 @@
it { should equal(wrapped) }
end

describe '#frozen?' do
subject { view.frozen? }

context 'non-frozen collection' do
it { is_expected.to be(false) }
end

context 'frozen collection' do
before { wrapped.freeze }
it { is_expected.to be(true) }
end
end

describe '#each' do
it 'yields' do
actual = [].tap { |res| view.each { |v| res << v } }
Expand Down
17 changes: 17 additions & 0 deletions spec/nanoc/base/views/item_rep_view_spec.rb
@@ -1,6 +1,23 @@
describe Nanoc::ItemRepView do
let(:view_context) { double(:view_context) }

describe '#frozen?' do
let(:item_rep) { Nanoc::Int::ItemRep.new(item, :jacques) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo/') }
let(:view) { described_class.new(item_rep, view_context) }

subject { view.frozen? }

context 'non-frozen item rep' do
it { is_expected.to be(false) }
end

context 'frozen item rep' do
before { item_rep.freeze }
it { is_expected.to be(true) }
end
end

describe '#== and #eql?' do
let(:item_rep) { Nanoc::Int::ItemRep.new(item, :jacques) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo/') }
Expand Down
9 changes: 8 additions & 1 deletion spec/nanoc/base/views/item_view_spec.rb
Expand Up @@ -58,7 +58,14 @@
expect(subject._context).to equal(view_context)
end

it { is_expected.to be_frozen }
context 'frozen parent' do
before { parent_item.freeze }
it { is_expected.to be_frozen }
end

context 'non-frozen parent' do
it { is_expected.not_to be_frozen }
end

context 'with root parent' do
let(:parent_item) { Nanoc::Int::Item.new('parent', {}, '/') }
Expand Down
19 changes: 19 additions & 0 deletions spec/nanoc/regressions/gh_795_spec.rb
@@ -0,0 +1,19 @@
describe 'GH-795', site: true, stdio: true do
before do
File.write('content/items.md', 'Frozen? <%= @items.unwrap.frozen? %>!')
File.write('content/items-view.md', 'Frozen? <%= @items.frozen? %>!')
File.write('Rules', <<EOS)
compile '/**/*' do
filter :erb
write item.identifier.without_ext + '.html'
end
EOS
end

it 'freezes @items' do
Nanoc::CLI.run(['compile'])

expect(File.read('output/items.html')).to eql('Frozen? true!')
expect(File.read('output/items-view.html')).to eql('Frozen? true!')
end
end

0 comments on commit bd7f52c

Please sign in to comment.