forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunes_test.go
60 lines (50 loc) · 1.71 KB
/
runes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package strutil_test
import (
"testing"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestRuneWidth(t *testing.T) {
assert.Eq(t, 3, len("你"))
assert.Eq(t, 1, len("\n"))
assert.Eq(t, 2, strutil.RuneWidth('你'))
assert.Eq(t, 1, strutil.RuneWidth('a'))
assert.Eq(t, 0, strutil.RuneWidth('\n'))
}
func TestUtf8Len(t *testing.T) {
str := "Hello, 世界"
assert.Eq(t, 7, len("Hello, "))
assert.Eq(t, 13, len(str))
assert.Eq(t, 9, strutil.RuneCount(str))
assert.Eq(t, 9, strutil.Utf8len(str))
assert.Eq(t, 9, strutil.Utf8Len(str))
assert.Eq(t, 11, strutil.TextWidth(str))
assert.Eq(t, 11, strutil.Utf8Width(str))
assert.True(t, strutil.IsValidUtf8(str))
}
func TestUtf8Width(t *testing.T) {
assert.Eq(t, 0, strutil.TextWidth(""))
}
func TestUtf8Truncate(t *testing.T) {
s := "hello 你好, world 世界"
assert.Eq(t, "hello 你好", strutil.Truncate(s, 10, ""))
assert.Eq(t, "hello ...", strutil.TextTruncate(s, 10, "..."))
assert.Eq(t, "hello 你好", strutil.TextTruncate("hello 你好", 20, "..."))
}
func TestUtf8Split(t *testing.T) {
s := "hello 你好, world 世界"
assert.Eq(t, []string{"hello ", "你好, ", "world ", "世界"}, strutil.TextSplit(s, 6))
assert.Eq(t, []string{"hello 你好"}, strutil.TextSplit("hello 你好", 10))
}
func TestWidthWrap(t *testing.T) {
s := "hello 你好, world 世界"
assert.Eq(t, "hello \n你好, \nworld \n世界", strutil.TextWrap(s, 6))
s = "hello, world"
assert.Eq(t, "hel\nlo,\n wo\nrld", strutil.TextWrap(s, 3))
}
func TestWordWrap(t *testing.T) {
s := "hello 你好, world 世界"
assert.Eq(t, "hello\n你好,\nworld\n世界", strutil.WordWrap(s, 6))
s = "hello, world"
assert.Eq(t, "hello,\nworld", strutil.WordWrap(s, 3))
}