diff options
Diffstat (limited to 'transform/chain_test.go')
-rw-r--r-- | transform/chain_test.go | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/transform/chain_test.go b/transform/chain_test.go index 936a6a1ad..71037d455 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -2,23 +2,31 @@ package transform import ( "bytes" + "strings" "testing" ) -const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"/foobar\">foobar</a>. Follow up</article></body></html>" - -const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article></body></html>" +const H5_JS_CONTENT_DOUBLE_QUOTE = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"/foobar\">Follow up</a></article></body></html>" +const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='/foobar'>Follow up</a></article></body></html>" +const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" +const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script><script src='//host2/barfoo.js'></head><body><nav><h1>title</h1></nav><article>content <a href=\"//host/foobar\">foobar</a>. <a href='//host2/foobar'>Follow up</a></article></body></html>" +const CORRECT_OUTPUT_SRC_HREF_DQ = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"http://base/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"http://base/foobar\">Follow up</a></article></body></html>" +const CORRECT_OUTPUT_SRC_HREF_SQ = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='http://base/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='http://base/foobar'>Follow up</a></article></body></html>" const H5_XML_CONTENT_ABS_URL = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p></content></entry></feed>" - const CORRECT_OUTPUT_SRC_HREF_IN_XML = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="http://xml/foobar">foobar</a></p> <p>A video: <iframe src='http://xml/foo'></iframe></p></content></entry></feed>" +const H5_XML_CONTENT_GUARDED = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p></content></entry></feed>" -var two_chain_tests = []test{ - {H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV}, +var abs_url_tests = []test{ + {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ}, + {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ}, + {H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL}, + {H5_JS_CONTENT_ABS_URL_SCHEMALESS, H5_JS_CONTENT_ABS_URL_SCHEMALESS}, } var xml_abs_url_tests = []test{ {H5_XML_CONTENT_ABS_URL, CORRECT_OUTPUT_SRC_HREF_IN_XML}, + {H5_XML_CONTENT_GUARDED, H5_XML_CONTENT_GUARDED}, } func TestChainZeroTransformers(t *testing.T) { @@ -30,13 +38,31 @@ func TestChainZeroTransformers(t *testing.T) { } } -func BenchmarkChain(b *testing.B) { - absURL, _ := AbsURL("http://two") +func BenchmarkAbsUrl(b *testing.B) { + absURL, _ := AbsURL("http://base") tr := NewChain(absURL...) b.ResetTimer() for i := 0; i < b.N; i++ { - apply(b.Errorf, tr, two_chain_tests) + apply(b.Errorf, tr, abs_url_tests) + } +} + +func TestAbsUrl(t *testing.T) { + absURL, _ := AbsURL("http://base") + tr := NewChain(absURL...) + + apply(t.Errorf, tr, abs_url_tests) + +} + +func BenchmarkXmlAbsUrl(b *testing.B) { + absURLInXML, _ := AbsURLInXML("http://xml") + tr := NewChain(absURLInXML...) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + apply(b.Errorf, tr, xml_abs_url_tests) } } @@ -45,3 +71,23 @@ func TestXMLAbsUrl(t *testing.T) { tr := NewChain(absURLInXML...) apply(t.Errorf, tr, xml_abs_url_tests) } + +type errorf func(string, ...interface{}) + +func apply(ef errorf, tr chain, tests []test) { + for _, test := range tests { + out := new(bytes.Buffer) + err := tr.Apply(out, strings.NewReader(test.content)) + if err != nil { + ef("Unexpected error: %s", err) + } + if test.expected != string(out.Bytes()) { + ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes())) + } + } +} + +type test struct { + content string + expected string +} |