From 27c03a6dd0bd003c77cdd4ded19ca8c3033e6476 Mon Sep 17 00:00:00 2001 From: bep Date: Thu, 12 Feb 2015 12:17:59 +0100 Subject: [PATCH] Add benchmark for AbsUrlInXml And a general test cleanup in /transform. See #894 --- transform/chain_test.go | 64 +++++++++++++++++++++++++++++++------ transform/post.go | 1 - transform/posttrans_test.go | 52 ------------------------------ 3 files changed, 55 insertions(+), 62 deletions(-) delete mode 100644 transform/post.go delete mode 100644 transform/posttrans_test.go 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 = "
content foobar. Follow up
" - -const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "
content foobar. Follow up
" +const H5_JS_CONTENT_DOUBLE_QUOTE = "
content foobar. Follow up
" +const H5_JS_CONTENT_SINGLE_QUOTE = "
content foobar. Follow up
" +const H5_JS_CONTENT_ABS_URL = "
content foobar. Follow up
" +const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "
content foobar. Follow up
" +const CORRECT_OUTPUT_SRC_HREF_SQ = "
content foobar. Follow up
" const H5_XML_CONTENT_ABS_URL = "<p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p>" - const CORRECT_OUTPUT_SRC_HREF_IN_XML = "<p><a href="http://xml/foobar">foobar</a></p> <p>A video: <iframe src='http://xml/foo'></iframe></p>" +const H5_XML_CONTENT_GUARDED = "<p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p>" -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 = "
content foobar. Follow up
" - -const H5_JS_CONTENT_SINGLE_QUOTE = "
content foobar. Follow up
" - -const H5_JS_CONTENT_ABS_URL = "
content foobar. Follow up
" - -const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "
content foobar. Follow up
" - -const CORRECT_OUTPUT_SRC_HREF_SQ = "
content foobar. Follow up
" - -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())) - } - } -}