2022-11-07 14:27:54 +08:00
|
|
|
|
package htmltext
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-18 18:15:44 +08:00
|
|
|
|
"fmt"
|
2022-11-07 14:27:54 +08:00
|
|
|
|
"testing"
|
2022-12-09 10:43:54 +08:00
|
|
|
|
|
|
|
|
|
"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
|
2022-11-10 11:23:07 +08:00
|
|
|
|
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)
|
2022-12-27 10:46:34 +08:00
|
|
|
|
|
|
|
|
|
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)
|
2023-08-18 18:15:44 +08:00
|
|
|
|
fmt.Println(formatTitle)
|
2022-12-09 10:43:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|