ref: 27c03a6dd0bd003c77cdd4ded19ca8c3033e6476
parent: 366f9916941c675b57153d63fed95a50610a858c
author: bep <[email protected]>
date: Thu Feb 12 07:17:59 EST 2015
Add benchmark for AbsUrlInXml And a general test cleanup in /transform. See #894
--- a/transform/chain_test.go
+++ b/transform/chain_test.go
@@ -2,23 +2,31 @@
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 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 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_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,18 +38,56 @@
}
}
-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)
+ }
+}
+
func TestXMLAbsUrl(t *testing.T) {
absURLInXML, _ := AbsURLInXML("http://xml")
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
}
--- a/transform/post.go
+++ /dev/null
@@ -1,1 +1,0 @@
-package transform
--- a/transform/posttrans_test.go
+++ /dev/null
@@ -1,52 +1,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()))
- }
- }
-}