-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
align_test.go
123 lines (115 loc) · 3.26 KB
/
align_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package text
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLineAlignLeft(t *testing.T) {
cases := []struct {
line string
width int
output string
}{
{
" foo foo bar",
30,
"foo foo bar",
},
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
70,
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// width too low return the same input
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
10,
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// respect escape sequences and wide chars
{
" 敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
60,
"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
},
}
for _, tc := range cases {
out := LineAlignLeft(tc.line, tc.width)
assert.Equal(t, tc.output, out)
}
}
func BenchmarkLineAlignLeft(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LineAlignLeft("敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。", 60)
}
}
func TestLineAlignCenter(t *testing.T) {
cases := []struct {
line string
width int
output string
}{
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
75,
" The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// width too low return the same input
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
10,
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// respect escape sequences and wide chars
{
"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
60,
" 敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
},
}
for _, tc := range cases {
out := LineAlignCenter(tc.line, tc.width)
assert.Equal(t, tc.output, out)
}
}
func BenchmarkLineAlignCenter(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LineAlignCenter("敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。", 60)
}
}
func TestLineAlignRight(t *testing.T) {
cases := []struct {
line string
width int
output string
}{
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
70,
" The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// width too low return the same input
{
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
10,
"The Lorem ipsum text is typically composed of pseudo-Latin words.",
},
// respect escape sequences and wide chars
{
"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
60,
" 敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
},
}
for _, tc := range cases {
out := LineAlignRight(tc.line, tc.width)
assert.Equal(t, tc.output, out)
}
}
func BenchmarkLineAlignRight(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LineAlignRight("敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。", 60)
}
}