package helpers import ( "github.com/stretchr/testify/assert" "html/template" "strings" "testing" ) const tstHTMLContent = "
content foobar. Follow up

This is some text.
And some more.

" func TestStripHTML(t *testing.T) { type test struct { input, expected string } data := []test{ {"

strip h1 tag

", "strip h1 tag "}, {"

strip p tag

", " strip p tag \n"}, {"
strip br
", " strip br\n"}, {"
strip br2
", " strip br2\n"}, {"This is 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\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}, {"Hello 中国", "Hello 中", 2, true}, {"Hello 中国", "Hello 中国", 3, 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) } } }