-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
len_test.go
102 lines (96 loc) · 1.52 KB
/
len_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package text
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLen(t *testing.T) {
cases := []struct {
Input string
Length int
}{
// A simple word
{
"foo",
3,
},
// A simple word with colors
{
"\x1b[31mbar\x1b[0m",
3,
},
// Handle prefix and suffix properly
{
"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
17,
},
// Handle chinese
{
"快檢什麼望對",
12,
},
// Handle chinese with colors
{
"快\x1b[31m檢什麼\x1b[0m望對",
12,
},
{
"❌",
2,
},
{
"✔",
1,
},
{
// used in Wrap()
"⭬",
1,
},
{
"\u2714\ufe0f ",
3,
},
{
"✔️ ",
3,
},
{
"❌ ",
3,
},
}
for i, tc := range cases {
l := Len(tc.Input)
if l != tc.Length {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%d`\n\nActual Output:\n\n`%d`",
i, tc.Input, tc.Length, l)
}
}
}
func BenchmarkLen(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Len("快\x1b[31m檢什麼\x1b[0m望對")
}
}
func TestMaxLineLen(t *testing.T) {
cases := []struct {
text string
length int
}{
{
` The Lorem ipsum text is typically composed of
pseudo-Latin words. It is commonly used as
placeholder text to examine or demonstrate the visual
effects of various graphic design.`,
59,
},
{
"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
12,
},
}
for _, tc := range cases {
assert.Equal(t, tc.length, MaxLineLen(tc.text))
}
}