-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_test.go
executable file
·111 lines (91 loc) · 3.06 KB
/
format_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
package xstr
import (
"strconv"
"strings"
"testing"
)
func TestExpandTabs(t *testing.T) {
runner := func(str string) (result string) {
defer func() {
if e := recover(); e != nil {
result = e.(string)
}
}()
input := strings.Split(str, separator)
n, _ := strconv.Atoi(input[1])
return ExpandTabs(input[0], n)
}
runTestCases(t, runner, _M{
sep("a\tbc\tdef\tghij\tk", "4"): "a bc def ghij k",
sep("abcdefg\thij\nk\tl", "4"): "abcdefg hij\nk l",
sep("z中\t文\tw", "4"): "z中 文 w",
sep("abcdef", "4"): "abcdef",
sep("abc\td\tef\tghij\nk\tl", "3"): "abc d ef ghij\nk l",
sep("abc\td\tef\tghij\nk\tl", "1"): "abc d ef ghij\nk l",
sep("abc", "0"): "tab size must be positive",
sep("abc", "-1"): "tab size must be positive",
})
}
func TestLeftJustify(t *testing.T) {
runner := func(str string) string {
input := strings.Split(str, separator)
n, _ := strconv.Atoi(input[1])
return LeftJustify(input[0], n, input[2])
}
runTestCases(t, runner, _M{
sep("hello", "4", " "): "hello",
sep("hello", "10", " "): "hello ",
sep("hello", "10", "123"): "hello12312",
sep("hello中文test", "4", " "): "hello中文test",
sep("hello中文test", "12", " "): "hello中文test ",
sep("hello中文test", "18", "测试!"): "hello中文test测试!测试!测",
sep("hello中文test", "0", "123"): "hello中文test",
sep("hello中文test", "18", ""): "hello中文test",
})
}
func TestRightJustify(t *testing.T) {
runner := func(str string) string {
input := strings.Split(str, separator)
n, _ := strconv.Atoi(input[1])
return RightJustify(input[0], n, input[2])
}
runTestCases(t, runner, _M{
sep("hello", "4", " "): "hello",
sep("hello", "10", " "): " hello",
sep("hello", "10", "123"): "12312hello",
sep("hello中文test", "4", " "): "hello中文test",
sep("hello中文test", "12", " "): " hello中文test",
sep("hello中文test", "18", "测试!"): "测试!测试!测hello中文test",
sep("hello中文test", "0", "123"): "hello中文test",
sep("hello中文test", "18", ""): "hello中文test",
})
}
func TestCenter(t *testing.T) {
runner := func(str string) string {
input := strings.Split(str, separator)
n, _ := strconv.Atoi(input[1])
return Center(input[0], n, input[2])
}
runTestCases(t, runner, _M{
sep("hello", "4", " "): "hello",
sep("hello", "10", " "): " hello ",
sep("hello", "10", "123"): "12hello123",
sep("hello中文test", "4", " "): "hello中文test",
sep("hello中文test", "12", " "): "hello中文test ",
sep("hello中文test", "18", "测试!"): "测试!hello中文test测试!测",
sep("hello中文test", "0", "123"): "hello中文test",
sep("hello中文test", "18", ""): "hello中文test",
})
}
func TestPrettyJSON(t *testing.T) {
str1 := `{"code":123456,"items":[1,2,3,4,5]}`
str2 := `"code":123456}`
_, err := PrettyJSON(str1, 2)
if err != nil {
t.Errorf("%s should be valid json", str1)
}
_, err = PrettyJSON(str2, 2)
if err == nil {
t.Errorf("%s should not be valid json", str2)
}
}