test/base/test_compiler.rb in nanoc-4.0.2 vs test/base/test_compiler.rb in nanoc-4.1.0a1
- old
+ new
@@ -1,58 +1,71 @@
class Nanoc::Int::CompilerTest < Nanoc::TestCase
- def test_compilation_rule_for
- # Mock rules
- rules = [mock, mock, mock]
- rules[0].expects(:applicable_to?).returns(false)
- rules[1].expects(:applicable_to?).returns(true)
- rules[1].expects(:rep_name).returns('wrong')
- rules[2].expects(:applicable_to?).returns(true)
- rules[2].expects(:rep_name).returns('right')
+ def new_compiler(site = nil)
+ site ||= Nanoc::Int::Site.new(
+ config: nil,
+ code_snippets: [],
+ items: [],
+ layouts: [],
+ )
- # Create compiler
- compiler = Nanoc::Int::Compiler.new(nil)
- compiler.rules_collection.instance_eval { @item_compilation_rules = rules }
+ rules_collection = Nanoc::Int::RulesCollection.new
- # Mock rep
- rep = mock
- rep.stubs(:name).returns('right')
- item = mock
- rep.stubs(:item).returns(item)
+ reps = Nanoc::Int::ItemRepRepo.new
- # Test
- assert_equal rules[2], compiler.rules_collection.compilation_rule_for(rep)
+ params = {
+ compiled_content_cache: Nanoc::Int::CompiledContentCache.new,
+ checksum_store: Nanoc::Int::ChecksumStore.new(site: site),
+ rule_memory_store: Nanoc::Int::RuleMemoryStore.new,
+ rule_memory_calculator: Nanoc::Int::RuleMemoryCalculator.new(
+ rules_collection: rules_collection,
+ site: site,
+ ),
+ dependency_store: Nanoc::Int::DependencyStore.new(
+ site.items.to_a + site.layouts.to_a),
+ reps: reps,
+ }
+
+ params[:outdatedness_checker] =
+ Nanoc::Int::OutdatednessChecker.new(
+ site: site,
+ checksum_store: params[:checksum_store],
+ dependency_store: params[:dependency_store],
+ rules_collection: params[:rules_collection],
+ rule_memory_store: params[:rule_memory_store],
+ rule_memory_calculator: params[:rule_memory_calculator],
+ reps: reps,
+ )
+
+ Nanoc::Int::Compiler.new(site, rules_collection, params)
end
- def test_routing_rule_for
+ def test_compilation_rule_for
# Mock rules
rules = [mock, mock, mock]
rules[0].expects(:applicable_to?).returns(false)
rules[1].expects(:applicable_to?).returns(true)
rules[1].expects(:rep_name).returns('wrong')
rules[2].expects(:applicable_to?).returns(true)
rules[2].expects(:rep_name).returns('right')
# Create compiler
- compiler = Nanoc::Int::Compiler.new(nil)
- compiler.rules_collection.instance_eval { @item_routing_rules = rules }
+ compiler = new_compiler
+ compiler.rules_collection.instance_eval { @item_compilation_rules = rules }
# Mock rep
rep = mock
rep.stubs(:name).returns('right')
item = mock
rep.stubs(:item).returns(item)
# Test
- assert_equal rules[2], compiler.rules_collection.routing_rule_for(rep)
+ assert_equal rules[2], compiler.rules_collection.compilation_rule_for(rep)
end
def test_filter_for_layout_with_existant_layout
- # Mock site
- site = mock
-
# Create compiler
- compiler = Nanoc::Int::Compiler.new(site)
+ compiler = new_compiler
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(/.*/)] = [:erb, { foo: 'bar' }]
# Mock layout
layout = MiniTest::Mock.new
layout.expect(:identifier, '/some_layout/')
@@ -60,15 +73,12 @@
# Check
assert_equal([:erb, { foo: 'bar' }], compiler.rules_collection.filter_for_layout(layout))
end
def test_filter_for_layout_with_existant_layout_and_unknown_filter
- # Mock site
- site = mock
-
# Create compiler
- compiler = Nanoc::Int::Compiler.new(site)
+ compiler = new_compiler
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(/.*/)] = [:some_unknown_filter, { foo: 'bar' }]
# Mock layout
layout = MiniTest::Mock.new
layout.expect(:identifier, '/some_layout/')
@@ -76,15 +86,12 @@
# Check
assert_equal([:some_unknown_filter, { foo: 'bar' }], compiler.rules_collection.filter_for_layout(layout))
end
def test_filter_for_layout_with_nonexistant_layout
- # Mock site
- site = mock
-
# Create compiler
- compiler = Nanoc::Int::Compiler.new(site)
+ compiler = new_compiler
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/foo/$})] = [:erb, { foo: 'bar' }]
# Mock layout
layout = MiniTest::Mock.new
layout.expect(:identifier, '/bar/')
@@ -92,15 +99,12 @@
# Check
assert_equal(nil, compiler.rules_collection.filter_for_layout(layout))
end
def test_filter_for_layout_with_many_layouts
- # Mock site
- site = mock
-
# Create compiler
- compiler = Nanoc::Int::Compiler.new(site)
+ compiler = new_compiler
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/a/b/c/.*/$})] = [:erb, { char: 'd' }]
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/a/.*/$})] = [:erb, { char: 'b' }]
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/a/b/.*/$})] = [:erb, { char: 'c' }] # never used!
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/.*/$})] = [:erb, { char: 'a' }]
@@ -157,11 +161,11 @@
site.stubs(:config).returns({})
site.stubs(:items).returns([])
site.stubs(:layouts).returns([layout])
# Create compiler
- compiler = Nanoc::Int::Compiler.new(site)
+ compiler = new_compiler(site)
compiler.rules_collection.expects(:compilation_rule_for).times(2).with(rep).returns(rule)
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/blah/$})] = [:erb, {}]
site.stubs(:compiler).returns(compiler)
# Compile
@@ -269,11 +273,11 @@
io.write "layout '/**/*', :erb\n"
end
# Create site
site = Nanoc::Int::SiteLoader.new.new_from_cwd
- error = assert_raises(RuntimeError) do
+ error = assert_raises(Nanoc::Error) do
site.compile
end
assert_match(/^The path returned for the.*does not start with a slash. Please ensure that all routing rules return a path that starts with a slash./, error.message)
end
end
@@ -297,35 +301,16 @@
File.write('content/foo.html', 'asdf')
File.write('content/bar.html', 'asdf')
# Create site
site = Nanoc::Int::SiteLoader.new.new_from_cwd
- assert_raises(Nanoc::Int::Compiler::IdenticalRoutesError) do
+ assert_raises(Nanoc::Int::ItemRepRouter::IdenticalRoutesError) do
site.compile
end
end
end
- def test_load_should_be_idempotent
- # Create site
- Nanoc::CLI.run %w( create_site bar)
-
- FileUtils.cd('bar') do
- site = Nanoc::Int::SiteLoader.new.new_from_cwd
-
- compiler = Nanoc::Int::Compiler.new(site)
- def compiler.route_reps
- raise 'oh my gosh it is borken'
- end
-
- assert site.instance_eval { !@loaded }
- assert_raises(RuntimeError) { compiler.load }
- assert site.instance_eval { !@loaded }
- assert_raises(RuntimeError) { compiler.load }
- end
- end
-
def test_compile_should_recompile_all_reps
Nanoc::CLI.run %w( create_site bar )
FileUtils.cd('bar') do
Nanoc::CLI.run %w( compile )
@@ -333,11 +318,11 @@
site = Nanoc::Int::SiteLoader.new.new_from_cwd
site.compile
# At this point, even the already compiled items in the previous pass
# should have their compiled content assigned, so this should work:
- site.items['/index.*'].reps[0].compiled_content
+ site.compiler.reps[site.items['/index.*']][0].compiled_content
end
end
def test_disallow_multiple_snapshots_with_the_same_name
# Create site
@@ -579,44 +564,9 @@
site = Nanoc::Int::SiteLoader.new.new_from_cwd
site.compile
# Check
assert Dir['tmp/text_items/*'].empty?
- end
- end
-
- def test_compiler_dependency_on_unmet_dependency
- with_site do
- File.open('content/a.html', 'w') do |io|
- io.write('<% @items["/b/"].compiled_content %>')
- end
- File.open('content/b.html', 'w') do |io|
- io.write('I am feeling so dependent!')
- end
- File.open('Rules', 'w') do |io|
- io.write "compile '*' do\n"
- io.write " filter :erb\n"
- io.write "end\n"
- io.write "\n"
- io.write "route '*' do\n"
- io.write " item.identifier.chop + '.' + item[:extension]\n"
- io.write "end\n"
- io.write "\n"
- io.write "layout '*', :erb\n"
- end
-
- site = Nanoc::Int::SiteLoader.new.new_from_cwd
- site.compiler.load
- rep = site.items['/a/'].reps[0]
- dt = site.compiler.dependency_tracker
- dt.start
- assert_raises Nanoc::Int::Errors::UnmetDependency do
- site.compiler.send :compile_rep, rep
- end
- dt.stop
-
- stack = dt.instance_eval { @stack }
- assert_empty stack
end
end
def test_find_layouts_by_glob
Nanoc::CLI.run %w( create_site bar )