require 'helper' class Nanoc::Filters::RelativizePathsTest < Nanoc::TestCase def test_filter_html_with_double_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %(foo) expected_content = %(foo) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_with_single_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %(foo) expected_content = %(foo) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_without_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %(foo) expected_content = %(foo) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_with_boilerplate # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = < Hello foo EOS expected0 = %r{foo} expected1 = %r{\A\s*\s*\s*(.|\s)*Hello\s*\s*\s*foo\s*\s*\s*\Z}m # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_match(expected0, actual_content) assert_match(expected1, actual_content) end def test_filter_html5_with_boilerplate # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = < Hello foo EOS expected0 = %r{foo} expected1 = %r{\A\s*\s*\s*\s*Hello\s*\s*\s*foo\s*\s*\s*\Z}m # Test actual_content = filter.setup_and_run(raw_content, type: :html5) assert_match(expected0, actual_content) assert_match(expected1, actual_content) end def test_filter_html_multiple # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %(foo bar) expected_content = %(foo bar) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_nested # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %() expected_content = %() # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_outside_tag # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %(stuff href="/foo" more stuff) expected_content = %(stuff href="/foo" more stuff) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_root # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %(foo) expected_content = %(foo) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_network_path # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %(example.com) expected_content = %(example.com) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_with_anchor # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %(Max Payne) expected_content = %(Max Payne) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_with_url # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %(Example) expected_content = %(Example) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_with_relative_path # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %(Example) expected_content = %(Example) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_html_object_with_relative_path # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end raw_content = %() actual_content = filter.setup_and_run(raw_content, type: :html) assert_match(//, actual_content) assert_match(/) expected_content = %(
) # Test actual_content = filter.setup_and_run(raw_content, type: :html) assert_equal(expected_content, actual_content) end def test_filter_implicit # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Test assert_raises(RuntimeError) do filter.setup_and_run('moo') end end def test_filter_css_with_double_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %[background: url("/foo/bar/background.png");] expected_content = %[background: url("../background.png");] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_css_with_single_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %[background: url('/foo/bar/background.png');] expected_content = %[background: url('../background.png');] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_css_without_quotes # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %[background: url(/foo/bar/background.png);] expected_content = %[background: url(../background.png);] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_css_multiple # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = %[background: url(/foo/bar/a.png) url(/foo/bar/b.png);] expected_content = %[background: url(../a.png) url(../b.png);] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_css_root # It is probably a bit weird to have “url(/)” in CSS, but I’ve made a # test case for this situation anyway. Can’t hurt… # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %[background: url(/);] expected_content = %[background: url(../../);] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_css_network_path # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/woof/meow/'] end # Set content raw_content = %[background: url(//example.com);] expected_content = %[background: url(//example.com);] # Test actual_content = filter.setup_and_run(raw_content, type: :css) assert_equal(expected_content, actual_content) end def test_filter_xml if_have 'nokogiri' do # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content expected = /baz<\/bar>/ raw_content = <<-XML baz XML actual_content = filter.setup_and_run(raw_content, type: :xml, select: ['*/@boo']) assert_match(expected, actual_content) end end def test_filter_fragment_xml if_have 'nokogiri' do # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = <<-XML baz XML actual_content = filter.setup_and_run(raw_content, type: :xml, select: ['far/@href']) assert_match(//, actual_content) assert_match(/baz<\/far><\/bar>/, actual_content) end end def test_filter_xml_with_namespaces if_have 'nokogiri' do # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = <<-XML baz XML options = { type: :xml, namespaces: { ex: 'http://example.org' }, select: ['ex:a/@href'], } actual_content = filter.setup_and_run(raw_content, options) assert_match(//, actual_content) assert_match(/baz<\/a><\/bar>/, actual_content) end end def test_filter_xhtml if_have 'nokogiri' do # Create filter with mock item filter = Nanoc::Filters::RelativizePaths.new # Mock item filter.instance_eval do @item_rep = Nanoc::Int::ItemRep.new( Nanoc::Int::Item.new( 'content', {}, '/foo/bar/baz/', ), :blah, ) @item_rep.paths[:last] = ['/foo/bar/baz/'] end # Set content raw_content = <<-XML bar XML actual_content = filter.setup_and_run(raw_content, type: :xhtml) assert_match(/]*href="..\/..\/..\/css"[^>]*\/>/, actual_content) assert_match(/