Add benchmark for AbsUrlInXml
authorbep <bjorn.erik.pedersen@gmail.com>
Thu, 12 Feb 2015 11:17:59 +0000 (12:17 +0100)
committerspf13 <steve.francia@gmail.com>
Mon, 16 Feb 2015 13:24:42 +0000 (08:24 -0500)
And a general test cleanup in /transform.

See #894

transform/chain_test.go
transform/post.go [deleted file]
transform/posttrans_test.go [deleted file]

index 936a6a1ad7ebf28b49560c6b5a1c7a3eda40bddd..71037d4557689483544cba8f1f660a397bc12928 100644 (file)
@@ -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\">&lt;p&gt;&lt;a href=&#34;/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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\">&lt;p&gt;&lt;a href=&#34;http://xml/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;http://xml/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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\">&lt;p&gt;&lt;a href=&#34;//foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;//foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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 (file)
index 5796f91..0000000
+++ /dev/null
@@ -1 +0,0 @@
-package transform
diff --git a/transform/posttrans_test.go b/transform/posttrans_test.go
deleted file mode 100644 (file)
index 297b4f6..0000000
+++ /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()))
-               }
-       }
-}