test/helpers/test_capturing.rb in nanoc-3.3.6 vs test/helpers/test_capturing.rb in nanoc-3.3.7

- old
+ new

@@ -7,10 +7,15 @@ include Nanoc::Helpers::Capturing def test_content_for require 'erb' + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + # Build content to be evaluated content = "head <% content_for :sidebar do %>\n" + " <%= 1+2 %>\n" + "<% end %> foot" @@ -47,10 +52,15 @@ end def test_content_for_recursively require 'erb' + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + content = <<EOS head <% content_for :box do %> basic <% end %> @@ -72,10 +82,15 @@ end def test_different_sites require 'erb' + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + @site = Nanoc3::Site.new({}) @item = Nanoc::Item.new('content', {}, '/') content = "<% content_for :a do %>Content One<% end %>" ::ERB.new(content).result(binding) @@ -87,8 +102,127 @@ content = "<% content_for :b do %>Content Two<% end %>" ::ERB.new(content).result(binding) assert_equal nil, content_for(@item, :a) assert_equal 'Content Two', content_for(@item, :b) + end + + def test_dependencies + with_site do |site| + # Prepare + File.open('lib/helpers.rb', 'w') do |io| + io.write 'include Nanoc::Helpers::Capturing' + end + File.open('content/includer.erb', 'w') do |io| + io.write '[<%= content_for(@items.find { |i| i.identifier == \'/includee/\' }, :blah) %>]' + end + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + + # Compile once + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>Old content<% end %>}' + end + Nanoc::CLI.run(%w(compile)) + assert_equal '[Old content]', File.read('output/includer/index.html') + + # Compile again + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>New content<% end %>}' + end + Nanoc::CLI.run(%w(compile)) + assert_equal '[New content]', File.read('output/includer/index.html') + end + end + + def test_dependency_without_item_variable + with_site do |site| + # Prepare + File.open('lib/helpers.rb', 'w') do |io| + io.write "include Nanoc::Helpers::Capturing\n" + io.write "include Nanoc::Helpers::Rendering\n" + end + File.open('content/includer.erb', 'w') do |io| + io.write '{<%= render \'partial\', :item => nil %>}' + end + File.open('layouts/partial.erb', 'w') do |io| + io.write '[<%= @item.inspect %>-<%= content_for(@items.find { |i| i.identifier == \'/includee/\' }, :blah) %>]' + end + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + io.write "layout '*', :erb\n" + end + + # Compile once + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>Old content<% end %>}' + end + Nanoc::CLI.run(%w(compile)) + assert_equal '{[nil-Old content]}', File.read('output/includer/index.html') + + # Compile again + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>New content<% end %>}' + end + Nanoc::CLI.run(%w(compile)) + assert_equal '{[nil-New content]}', File.read('output/includer/index.html') + end + end + + def test_self + with_site do |site| + File.open('lib/helpers.rb', 'w') do |io| + io.write 'include Nanoc::Helpers::Capturing' + end + + File.open('content/self.erb', 'w') do |io| + io.write "<% content_for :foo do %>Foo!<% end %>" + io.write "<%= content_for(@item, :foo) %>" + end + + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + + Nanoc::CLI.run(%w(compile)) + assert_equal 'Foo!', File.read('output/self/index.html') + end + end + + def test_recompile_dependency + with_site do |site| + # Prepare + File.open('lib/helpers.rb', 'w') do |io| + io.write 'include Nanoc::Helpers::Capturing' + end + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>Content<% end %>}' + end + File.open('Rules', 'w') do |io| + io.write "compile '*' do ; filter :erb ; end\n" + io.write "route '*' do ; item.identifier + 'index.html' ; end\n" + end + + # Compile once + File.open('content/includer.erb', 'w') do |io| + io.write 'Old-<%= content_for(@items.find { |i| i.identifier == \'/includee/\' }, :blah) %>' + end + Nanoc::CLI.run(%w(compile)) + assert_equal 'Old-Content', File.read('output/includer/index.html') + + # Compile again + $LOUD = true + File.open('content/includer.erb', 'w') do |io| + io.write 'New-<%= content_for(@items.find { |i| i.identifier == \'/includee/\' }, :blah) %>' + end + Nanoc::CLI.run(%w(compile)) + assert_equal 'New-Content', File.read('output/includer/index.html') + end + ensure + $LOUD = false end end