test/helpers/test_capturing.rb in nanoc-4.0.2 vs test/helpers/test_capturing.rb in nanoc-4.1.0a1

- old
+ new

@@ -15,12 +15,12 @@ '<% end %> foot' # Build site site = Nanoc::Int::SiteLoader.new.new_empty item = Nanoc::Int::Item.new('moo', {}, '/blah/') - @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty) - @item = Nanoc::ItemView.new(item) + @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty, nil) + @item = Nanoc::ItemView.new(item, nil) # Evaluate content result = ::ERB.new(content).result(binding) # Check @@ -30,12 +30,12 @@ def test_capture require 'erb' # Build site - @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty) - @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('moo', {}, '/blah/')) + @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty, nil) + @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('moo', {}, '/blah/'), nil) # Capture _erbout = 'foo' captured_content = capture do _erbout << 'bar' @@ -57,19 +57,19 @@ content = <<EOS head <% content_for :box do %> basic <% end %> -<% content_for :box do %> +<% content_for :outerbox do %> before <%= content_for @item, :box %> after <% end %> -<%= content_for @item, :box %> +<%= content_for @item, :outerbox %> foot EOS - @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty) - @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/')) + @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty, nil) + @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/'), nil) result = ::ERB.new(content).result(binding) expected = %w( head before basic after foot ) actual = result.scan(/[a-z]+/) @@ -82,24 +82,143 @@ 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 = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty) - @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/')) + @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty, nil) + @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/'), nil) content = '<% content_for :a do %>Content One<% end %>' ::ERB.new(content).result(binding) assert_equal 'Content One', content_for(@item, :a) assert_equal nil, content_for(@item, :b) - @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty) - @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/')) + @site = Nanoc::SiteView.new(Nanoc::Int::SiteLoader.new.new_empty, nil) + @item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/'), nil) 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_content_for_with_existing_symbol + 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["/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 + + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>First content<% end %><% content_for :blah do %>Second content<% end %>}' + end + + # Using the same symbols twice now raises an error, to be changed to concatenating in a future version + assert_raises do + Nanoc::CLI.run(%w(compile)) + end + end + end + + def test_content_for_with_existing_symbol_with_error_option + 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["/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 + + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>First content<% end %><% content_for :blah, existing: :error do %>Second content<% end %>}' + end + + assert_raises do + Nanoc::CLI.run(%w(compile)) + end + end + end + + def test_content_for_with_existing_symbol_with_overwrite_option + 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["/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 + + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>First content<% end %><% content_for :blah, existing: :overwrite do %>Second content<% end %>}' + end + + Nanoc::CLI.run(%w(compile)) + assert_equal '[Second content]', File.read('output/includer/index.html') + end + end + + def test_content_for_with_existing_symbol_with_append_option + 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["/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 + + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah do %>First content<% end %><% content_for :blah, existing: :append do %>Second content<% end %>}' + end + + Nanoc::CLI.run(%w(compile)) + assert_equal '[First contentSecond content]', File.read('output/includer/index.html') + end + end + + def test_content_for_with_existing_symbol_with_unrecognised_option + 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["/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 + + File.open('content/includee.erb', 'w') do |io| + io.write '{<% content_for :blah, existing: :donkey do %>First content<% end %>}' + end + + assert_raises(ArgumentError) do + Nanoc::CLI.run(%w(compile)) + end + end end def test_dependencies with_site do |_site| # Prepare