answer/pkg/htmltext/htmltext_test.go

69 lines
1.6 KiB
Go
Raw Normal View History

2022-11-07 14:27:54 +08:00
package htmltext
import (
"testing"
2022-12-09 10:43:54 +08:00
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
2022-11-07 14:27:54 +08:00
)
func TestClearText(t *testing.T) {
var (
expected,
clearedText string
)
// test code clear text
expected = "hello{code...}"
clearedText = ClearText("<p>hello<pre>var a = \"good\"</pre></p>")
assert.Equal(t, expected, clearedText)
// test link clear text
expected = "hello [example.com]"
clearedText = ClearText("<p>hello <a href=\"http://example.com/\">example.com</a></p>")
2022-11-07 14:27:54 +08:00
assert.Equal(t, expected, clearedText)
clearedText = ClearText("<p>hello<a href=\"https://example.com/\">example.com</a></p>")
assert.Equal(t, expected, clearedText)
expected = "hello world"
clearedText = ClearText("<div> hello</div>\n<div>world</div>")
assert.Equal(t, expected, clearedText)
}
func TestFetchExcerpt(t *testing.T) {
var (
expected,
text string
)
// test english string
2022-11-07 14:29:49 +08:00
expected = "hello..."
2022-11-07 14:27:54 +08:00
text = FetchExcerpt("<p>hello world</p>", "...", 5)
assert.Equal(t, expected, text)
// test mixed string
2022-11-07 14:29:49 +08:00
expected = "hello你好..."
2022-11-07 14:27:54 +08:00
text = FetchExcerpt("<p>hello你好world</p>", "...", 7)
assert.Equal(t, expected, text)
// test mixed string with emoticon
2022-11-07 14:29:49 +08:00
expected = "hello你好😂..."
2022-11-07 14:27:54 +08:00
text = FetchExcerpt("<p>hello你好😂world</p>", "...", 8)
assert.Equal(t, expected, text)
expected = "hello你好"
text = FetchExcerpt("<p>hello你好</p>", "...", 8)
assert.Equal(t, expected, text)
2022-11-07 14:27:54 +08:00
}
2022-12-09 10:43:54 +08:00
func TestUrlTitle(t *testing.T) {
list := []string{
"hello你好😂...",
"这是一个标题title",
}
for _, title := range list {
formatTitle := UrlTitle(title)
spew.Dump(formatTitle)
}
}