|
|
@ -4,43 +4,94 @@ |
|
|
|
package util |
|
|
|
package util |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
"strings" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func TestSplitString(t *testing.T) { |
|
|
|
func TestEllipsisString(t *testing.T) { |
|
|
|
type testCase struct { |
|
|
|
cases := []struct { |
|
|
|
input string |
|
|
|
limit int |
|
|
|
n int |
|
|
|
|
|
|
|
leftSub string |
|
|
|
input, left, right string |
|
|
|
ellipsis string |
|
|
|
}{ |
|
|
|
|
|
|
|
{limit: 0, input: "abcde", left: "", right: "…abcde"}, |
|
|
|
|
|
|
|
{limit: 1, input: "abcde", left: "", right: "…abcde"}, |
|
|
|
|
|
|
|
{limit: 2, input: "abcde", left: "", right: "…abcde"}, |
|
|
|
|
|
|
|
{limit: 3, input: "abcde", left: "…", right: "…abcde"}, |
|
|
|
|
|
|
|
{limit: 4, input: "abcde", left: "a…", right: "…bcde"}, |
|
|
|
|
|
|
|
{limit: 5, input: "abcde", left: "abcde", right: ""}, |
|
|
|
|
|
|
|
{limit: 6, input: "abcde", left: "abcde", right: ""}, |
|
|
|
|
|
|
|
{limit: 7, input: "abcde", left: "abcde", right: ""}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// a CJK char or emoji is considered as 2-ASCII width, the ellipsis is 3-ASCII width
|
|
|
|
|
|
|
|
{limit: 0, input: "测试文本", left: "", right: "…测试文本"}, |
|
|
|
|
|
|
|
{limit: 1, input: "测试文本", left: "", right: "…测试文本"}, |
|
|
|
|
|
|
|
{limit: 2, input: "测试文本", left: "", right: "…测试文本"}, |
|
|
|
|
|
|
|
{limit: 3, input: "测试文本", left: "…", right: "…测试文本"}, |
|
|
|
|
|
|
|
{limit: 4, input: "测试文本", left: "…", right: "…测试文本"}, |
|
|
|
|
|
|
|
{limit: 5, input: "测试文本", left: "测…", right: "…试文本"}, |
|
|
|
|
|
|
|
{limit: 6, input: "测试文本", left: "测…", right: "…试文本"}, |
|
|
|
|
|
|
|
{limit: 7, input: "测试文本", left: "测试…", right: "…文本"}, |
|
|
|
|
|
|
|
{limit: 8, input: "测试文本", left: "测试文本", right: ""}, |
|
|
|
|
|
|
|
{limit: 9, input: "测试文本", left: "测试文本", right: ""}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for _, c := range cases { |
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("%s(%d)", c.input, c.limit), func(t *testing.T) { |
|
|
|
|
|
|
|
left, right := EllipsisDisplayStringX(c.input, c.limit) |
|
|
|
|
|
|
|
assert.Equal(t, c.left, left, "left") |
|
|
|
|
|
|
|
assert.Equal(t, c.right, right, "right") |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
test := func(tc []*testCase, f func(input string, n int) (left, right string)) { |
|
|
|
t.Run("LongInput", func(t *testing.T) { |
|
|
|
for _, c := range tc { |
|
|
|
left, right := EllipsisDisplayStringX(strings.Repeat("abc", 240), 90) |
|
|
|
l, r := f(c.input, c.n) |
|
|
|
assert.Equal(t, strings.Repeat("abc", 29)+"…", left) |
|
|
|
if c.ellipsis != "" { |
|
|
|
assert.Equal(t, "…"+strings.Repeat("abc", 211), right) |
|
|
|
assert.Equal(t, c.leftSub+c.ellipsis, l, "test split %q at %d, expected leftSub: %q", c.input, c.n, c.leftSub) |
|
|
|
}) |
|
|
|
assert.Equal(t, c.ellipsis+c.input[len(c.leftSub):], r, "test split %s at %d, expected rightSub: %q", c.input, c.n, c.input[len(c.leftSub):]) |
|
|
|
|
|
|
|
} else { |
|
|
|
t.Run("InvalidUtf8", func(t *testing.T) { |
|
|
|
assert.Equal(t, c.leftSub, l, "test split %q at %d, expected leftSub: %q", c.input, c.n, c.leftSub) |
|
|
|
invalidCases := []struct { |
|
|
|
assert.Empty(t, r, "test split %q at %d, expected rightSub: %q", c.input, c.n, "") |
|
|
|
limit int |
|
|
|
} |
|
|
|
left, right string |
|
|
|
|
|
|
|
}{ |
|
|
|
|
|
|
|
{limit: 0, left: "", right: "...\xef\x03\xfe\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 1, left: "", right: "...\xef\x03\xfe\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 2, left: "", right: "...\xef\x03\xfe\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 3, left: "...", right: "...\xef\x03\xfe\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 4, left: "...", right: "...\xef\x03\xfe\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 5, left: "\xef\x03\xfe...", right: "...\xef\x03\xfe"}, |
|
|
|
|
|
|
|
{limit: 6, left: "\xef\x03\xfe\xef\x03\xfe", right: ""}, |
|
|
|
|
|
|
|
{limit: 7, left: "\xef\x03\xfe\xef\x03\xfe", right: ""}, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
for _, c := range invalidCases { |
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("%d", c.limit), func(t *testing.T) { |
|
|
|
|
|
|
|
left, right := EllipsisDisplayStringX("\xef\x03\xfe\xef\x03\xfe", c.limit) |
|
|
|
|
|
|
|
assert.Equal(t, c.left, left, "left") |
|
|
|
|
|
|
|
assert.Equal(t, c.right, right, "right") |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
tc := []*testCase{ |
|
|
|
t.Run("IsLikelyEllipsisLeftPart", func(t *testing.T) { |
|
|
|
{"abc123xyz", 0, "", utf8Ellipsis}, |
|
|
|
assert.True(t, IsLikelyEllipsisLeftPart("abcde…")) |
|
|
|
{"abc123xyz", 1, "", utf8Ellipsis}, |
|
|
|
assert.True(t, IsLikelyEllipsisLeftPart("abcde...")) |
|
|
|
{"abc123xyz", 4, "a", utf8Ellipsis}, |
|
|
|
}) |
|
|
|
{"啊bc123xyz", 4, "", utf8Ellipsis}, |
|
|
|
} |
|
|
|
{"啊bc123xyz", 6, "啊", utf8Ellipsis}, |
|
|
|
|
|
|
|
{"啊bc", 5, "啊bc", ""}, |
|
|
|
func TestTruncateRunes(t *testing.T) { |
|
|
|
{"啊bc", 6, "啊bc", ""}, |
|
|
|
assert.Equal(t, "", TruncateRunes("", 0)) |
|
|
|
{"abc\xef\x03\xfe", 3, "", asciiEllipsis}, |
|
|
|
assert.Equal(t, "", TruncateRunes("", 1)) |
|
|
|
{"abc\xef\x03\xfe", 4, "a", asciiEllipsis}, |
|
|
|
|
|
|
|
{"\xef\x03", 1, "\xef\x03", ""}, |
|
|
|
assert.Equal(t, "", TruncateRunes("ab", 0)) |
|
|
|
} |
|
|
|
assert.Equal(t, "a", TruncateRunes("ab", 1)) |
|
|
|
test(tc, SplitStringAtByteN) |
|
|
|
assert.Equal(t, "ab", TruncateRunes("ab", 2)) |
|
|
|
|
|
|
|
assert.Equal(t, "ab", TruncateRunes("ab", 3)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "", TruncateRunes("测试", 0)) |
|
|
|
|
|
|
|
assert.Equal(t, "测", TruncateRunes("测试", 1)) |
|
|
|
|
|
|
|
assert.Equal(t, "测试", TruncateRunes("测试", 2)) |
|
|
|
|
|
|
|
assert.Equal(t, "测试", TruncateRunes("测试", 3)) |
|
|
|
} |
|
|
|
} |
|
|
|