# encoding: utf-8 class Nanoc3::Helpers::FilteringTest < MiniTest::Unit::TestCase include Nanoc3::TestHelpers include Nanoc3::Helpers::Filtering def test_filter_simple if_have 'rubypants' do # Build content to be evaluated content = "
Foo...
\n" + "<% filter :rubypants do %>\n" + "Bar...
\n" + "<% end %>\n" # Mock item and rep @item_rep = mock @item_rep.expects(:assigns).returns({}) # Evaluate content result = ::ERB.new(content).result(binding) # Check assert_match('Foo...
', result) assert_match('Bar…
', result) end end def test_filter_with_assigns if_have 'rubypants' do # Build content to be evaluated content = "Foo...
\n" + "<% filter :erb do %>\n" + "<%%= @item[:title] %>
\n" + "<% end %>\n" # Mock item and rep @item = mock @item.expects(:[]).with(:title).returns('Bar...') @item.expects(:identifier).returns('/blah/') @item_rep = mock @item_rep.expects(:name).returns('default') @item_rep.expects(:assigns).returns({ :item => @item, :item_rep => @item_rep }) # Evaluate content result = ::ERB.new(content).result(binding) # Check assert_match('Foo...
', result) assert_match('Bar...
', result) end end def test_filter_with_unknown_filter_name # Build content to be evaluated content = "Foo...
\n" + "<% filter :askjdflkawgjlkwaheflnvz do %>\n" + "Blah blah blah.
\n" + "<% end %>\n" # Evaluate content error = assert_raises(Nanoc3::Errors::UnknownFilter) do ::ERB.new(content).result(binding) end end def test_filter_with_arguments if_have 'coderay' do # Build content to be evaluated content = "<% filter :coderay, :language => 'ruby' do %>\n" + " def some_function ; x = blah.foo ; x.bar 'xyzzy' ; end\n" + "<% end %>\n" # Mock item and rep @item_rep = mock @item_rep.expects(:assigns).returns({}) # Evaluate content result = ::ERB.new(content).result(binding) assert_match(%r{def some_function}, result) end end def test_with_haml if_have 'haml' do # Build content to be evaluated content = "%p Foo.\n" + "- filter(:erb) do\n" + " <%= 'abc' + 'xyz' %>\n" + "%p Bar.\n" # Mock item and rep @item_rep = mock @item_rep.expects(:assigns).returns({}) # Evaluate content result = ::Haml::Engine.new(content).render(binding) assert_match(%r{^Foo.
\s*abcxyz\s*Bar.
$}, result) end end end