# encoding: utf-8 class Nanoc::Helpers::FilteringTest < Nanoc::TestCase include Nanoc::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 assert_raises(Nanoc::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 def test_notifications notifications = Set.new Nanoc::NotificationCenter.on(:filtering_started) { notifications << :filtering_started } Nanoc::NotificationCenter.on(:filtering_ended) { notifications << :filtering_ended } # Build content to be evaluated content = "<% filter :erb do %>\n" \ " ... stuff ...\n" \ "<% end %>\n" # Mock item and rep @item_rep = mock @item_rep.expects(:assigns).returns({}) ::ERB.new(content).result(binding) assert notifications.include?(:filtering_started) assert notifications.include?(:filtering_ended) end end