hugo/helpers/content_test.go
bep 43e48a8989 Add benchmark test for StripHTML
go test -test.run=NONE -bench=".*" -test.benchmem=true ./helpers

Old vs new impl (string.Replace vs string.Replacer):

benchmark              old ns/op     new ns/op     delta
BenchmarkStripHTML     10210         6572          -35.63%

benchmark              old allocs     new allocs     delta
BenchmarkStripHTML     6              5              -16.67%

benchmark              old bytes     new bytes     delta
BenchmarkStripHTML     1456          848           -41.76%
2015-02-06 10:00:42 +01:00

73 lines
2.2 KiB
Go

package helpers
import (
"github.com/stretchr/testify/assert"
"html/template"
"strings"
"testing"
)
const tstHtmlContent = "<!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><p>This is some text.<br>And some more.</p></body></html>"
func TestStripHTML(t *testing.T) {
type test struct {
input, expected string
}
data := []test{
{"<h1>strip h1 tag <h1>", "strip h1 tag "},
{"<p> strip p tag </p>", " strip p tag \n"},
{"</br> strip br<br>", " strip br\n"},
{"</br> strip br2<br />", " strip br2\n"},
{"This <strong>is</strong> a\nnewline", "This is a newline"},
}
for i, d := range data {
output := StripHTML(d.input)
if d.expected != output {
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
}
}
}
func BenchmarkStripHTML(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripHTML(tstHtmlContent)
}
}
func TestStripEmptyNav(t *testing.T) {
cleaned := StripEmptyNav([]byte("do<nav>\n</nav>\n\nbedobedo"))
assert.Equal(t, []byte("dobedobedo"), cleaned)
}
func TestBytesToHTML(t *testing.T) {
assert.Equal(t, template.HTML("dobedobedo"), BytesToHTML([]byte("dobedobedo")))
}
func TestTruncateWordsToWholeSentence(t *testing.T) {
type test struct {
input, expected string
max int
truncated bool
}
data := []test{
{"a b c", "a b c", 12, false},
{"a b c", "a b c", 3, false},
{"a", "a", 1, false},
{"This is a sentence.", "This is a sentence.", 5, false},
{"This is also a sentence!", "This is also a sentence!", 1, false},
{"To be. Or not to be. That's the question.", "To be.", 1, true},
{" \nThis is not a sentence\n ", "This is not a", 4, true},
}
for i, d := range data {
output, truncated := TruncateWordsToWholeSentence(strings.Fields(d.input), d.max)
if d.expected != output {
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
}
if d.truncated != truncated {
t.Errorf("Test %d failed. Expected truncated=%t got %t", i, d.truncated, truncated)
}
}
}