Skip to content

Commit

Permalink
Merge pull request #916 from nanoc/better-no-layout-rule-memory-error
Browse files Browse the repository at this point in the history
Better no layout rule memory error
  • Loading branch information
denisdefreyne committed Jul 24, 2016
2 parents c241395 + b255927 commit 89bae20
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 45 deletions.
24 changes: 21 additions & 3 deletions lib/nanoc/rule_dsl/rule_memory_calculator.rb
Expand Up @@ -12,6 +12,18 @@ def initialize(obj)
end
end

class NoRuleMemoryForLayoutException < ::Nanoc::Error
def initialize(layout)
super("There is no layout rule specified for #{layout.inspect}")
end
end

class NoRuleMemoryForItemRepException < ::Nanoc::Error
def initialize(item)
super("There is no compilation rule specified for #{item.inspect}")
end
end

# @api private
attr_accessor :rules_collection

Expand Down Expand Up @@ -57,14 +69,16 @@ def snapshots_defs_for(rep)
#
# @return [Nanoc::Int::RuleMemory]
def new_rule_memory_for_rep(rep)
# FIXME: What if #compilation_rule_for returns nil?

dependency_tracker = Nanoc::Int::DependencyTracker::Null.new
view_context = @site.compiler.create_view_context(dependency_tracker)

executor = Nanoc::RuleDSL::RecordingExecutor.new(rep, @rules_collection, @site)
rule = @rules_collection.compilation_rule_for(rep)

unless rule
raise NoRuleMemoryForItemRepException.new(rep)
end

executor.snapshot(rep, :raw)
executor.snapshot(rep, :pre, final: false)
rule.apply_to(rep, executor: executor, site: @site, view_context: view_context)
Expand All @@ -83,7 +97,11 @@ def new_rule_memory_for_rep(rep)
# @return [Nanoc::Int::RuleMemory]
def new_rule_memory_for_layout(layout)
res = @rules_collection.filter_for_layout(layout)
# FIXME: what if res is nil?

unless res
raise NoRuleMemoryForLayoutException.new(layout)
end

Nanoc::Int::RuleMemory.new(layout).tap do |rm|
rm.add_filter(res[0], res[1])
end
Expand Down
99 changes: 57 additions & 42 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -21,66 +21,80 @@
let(:view_context) { double(:view_context) }

before do
rules_proc = proc do
filter :erb, speed: :over_9000
layout '/default.*'
filter :typohero
end
rule = Nanoc::RuleDSL::Rule.new(Nanoc::Int::Pattern.from('/list.*'), :csv, rules_proc)
rules_collection.add_item_compilation_rule(rule)

expect(compiler).to receive(:create_view_context).and_return(view_context)
end

example do
subject
context 'no rules exist' do
it 'raises error' do
error = Nanoc::RuleDSL::RuleMemoryCalculator::NoRuleMemoryForItemRepException
expect { subject }.to raise_error(error)
end
end

expect(subject.size).to eql(8)
context 'rules exist' do
before do
rules_proc = proc do
filter :erb, speed: :over_9000
layout '/default.*'
filter :typohero
end
rule = Nanoc::RuleDSL::Rule.new(Nanoc::Int::Pattern.from('/list.*'), :csv, rules_proc)
rules_collection.add_item_compilation_rule(rule)
end

example do
subject

expect(subject.size).to eql(8)

expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
expect(subject[0]).to be_final
expect(subject[0].path).to be_nil
expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
expect(subject[0]).to be_final
expect(subject[0].path).to be_nil

expect(subject[1]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[1].snapshot_name).to eql(:pre)
expect(subject[1]).not_to be_final
expect(subject[1].path).to be_nil
expect(subject[1]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[1].snapshot_name).to eql(:pre)
expect(subject[1]).not_to be_final
expect(subject[1].path).to be_nil

expect(subject[2]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[2].filter_name).to eql(:erb)
expect(subject[2].params).to eql({ speed: :over_9000 })
expect(subject[2]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[2].filter_name).to eql(:erb)
expect(subject[2].params).to eql({ speed: :over_9000 })

expect(subject[3]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[3].snapshot_name).to eql(:pre)
expect(subject[3]).to be_final
expect(subject[3].path).to be_nil
expect(subject[3]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[3].snapshot_name).to eql(:pre)
expect(subject[3]).to be_final
expect(subject[3].path).to be_nil

expect(subject[4]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(subject[4].layout_identifier).to eql('/default.*')
expect(subject[4].params).to be_nil
expect(subject[4]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(subject[4].layout_identifier).to eql('/default.*')
expect(subject[4].params).to be_nil

expect(subject[5]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[5].filter_name).to eql(:typohero)
expect(subject[5].params).to eql({})
expect(subject[5]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[5].filter_name).to eql(:typohero)
expect(subject[5].params).to eql({})

expect(subject[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[6].snapshot_name).to eql(:post)
expect(subject[6]).to be_final
expect(subject[6].path).to be_nil
expect(subject[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[6].snapshot_name).to eql(:post)
expect(subject[6]).to be_final
expect(subject[6].path).to be_nil

expect(subject[7]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[7].snapshot_name).to eql(:last)
expect(subject[7]).to be_final
expect(subject[7].path).to be_nil
expect(subject[7]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[7].snapshot_name).to eql(:last)
expect(subject[7]).to be_final
expect(subject[7].path).to be_nil
end
end
end

context 'with layout' do
let(:obj) { Nanoc::Int::Layout.new('content', {}, '/default.erb') }

context 'no rules exist' do
# FIXME: This case is broken
it 'raises error' do
error = Nanoc::RuleDSL::RuleMemoryCalculator::NoRuleMemoryForLayoutException
expect { subject }.to raise_error(error)
end
end

context 'rule exists' do
Expand All @@ -102,7 +116,8 @@
let(:obj) { :donkey }

it 'errors' do
expect { subject }.to raise_error(Nanoc::RuleDSL::RuleMemoryCalculator::UnsupportedObjectTypeException)
error = Nanoc::RuleDSL::RuleMemoryCalculator::UnsupportedObjectTypeException
expect { subject }.to raise_error(error)
end
end
end
Expand Down

0 comments on commit 89bae20

Please sign in to comment.