diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-02-12 12:17:59 +0100 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2015-02-16 08:24:42 -0500 |
commit | 27c03a6dd0bd003c77cdd4ded19ca8c3033e6476 (patch) | |
tree | c50c912106780709c3065864e4eccdee46a83c19 /transform | |
parent | 366f9916941c675b57153d63fed95a50610a858c (diff) |
Add benchmark for AbsUrlInXml
And a general test cleanup in /transform.
See #894
Diffstat (limited to 'transform')
-rw-r--r-- | transform/chain_test.go | 64 | ||||
-rw-r--r-- | transform/post.go | 1 | ||||
-rw-r--r-- | transform/posttrans_test.go | 52 |
3 files changed, 55 insertions, 62 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 +} diff --git a/transform/post.go b/transform/post.go deleted file mode 100644 index 5796f91c6..000000000 --- a/transform/post.go +++ /dev/null @@ -1 +0,0 @@ -package transform diff --git a/transform/posttrans_test.go b/transform/posttrans_test.go deleted file mode 100644 index 297b4f66e..000000000 --- a/transform/posttrans_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package transform - -import ( - "bytes" - "strings" - "testing" -) - -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>" - -func TestAbsUrlify(t *testing.T) { - tr, _ := AbsURL("http://base") - chain := NewChain(tr...) - apply(t.Errorf, chain, abs_url_tests) -} - -type test struct { - content string - expected string -} - -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}, -} - -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())) - } - } -} |