1
2
3
4 package xstrings
5
6 import (
7 "strconv"
8 "strings"
9 "testing"
10 )
11
12 func TestExpandTabs(t *testing.T) {
13 runner := func(str string) (result string) {
14 defer func() {
15 if e := recover(); e != nil {
16 result = e.(string)
17 }
18 }()
19
20 input := strings.Split(str, separator)
21 n, _ := strconv.Atoi(input[1])
22 return ExpandTabs(input[0], n)
23 }
24
25 runTestCases(t, runner, _M{
26 sep("a\tbc\tdef\tghij\tk", "4"): "a bc def ghij k",
27 sep("abcdefg\thij\nk\tl", "4"): "abcdefg hij\nk l",
28 sep("z中\t文\tw", "4"): "z中 文 w",
29 sep("abcdef", "4"): "abcdef",
30
31 sep("abc\td\tef\tghij\nk\tl", "3"): "abc d ef ghij\nk l",
32 sep("abc\td\tef\tghij\nk\tl", "1"): "abc d ef ghij\nk l",
33
34 sep("abc", "0"): "tab size must be positive",
35 sep("abc", "-1"): "tab size must be positive",
36 })
37 }
38
39 func TestLeftJustify(t *testing.T) {
40 runner := func(str string) string {
41 input := strings.Split(str, separator)
42 n, _ := strconv.Atoi(input[1])
43 return LeftJustify(input[0], n, input[2])
44 }
45
46 runTestCases(t, runner, _M{
47 sep("hello", "4", " "): "hello",
48 sep("hello", "10", " "): "hello ",
49 sep("hello", "10", "123"): "hello12312",
50
51 sep("hello中文test", "4", " "): "hello中文test",
52 sep("hello中文test", "12", " "): "hello中文test ",
53 sep("hello中文test", "18", "测试!"): "hello中文test测试!测试!测",
54
55 sep("hello中文test", "0", "123"): "hello中文test",
56 sep("hello中文test", "18", ""): "hello中文test",
57 })
58 }
59
60 func TestRightJustify(t *testing.T) {
61 runner := func(str string) string {
62 input := strings.Split(str, separator)
63 n, _ := strconv.Atoi(input[1])
64 return RightJustify(input[0], n, input[2])
65 }
66
67 runTestCases(t, runner, _M{
68 sep("hello", "4", " "): "hello",
69 sep("hello", "10", " "): " hello",
70 sep("hello", "10", "123"): "12312hello",
71
72 sep("hello中文test", "4", " "): "hello中文test",
73 sep("hello中文test", "12", " "): " hello中文test",
74 sep("hello中文test", "18", "测试!"): "测试!测试!测hello中文test",
75
76 sep("hello中文test", "0", "123"): "hello中文test",
77 sep("hello中文test", "18", ""): "hello中文test",
78 })
79 }
80
81 func TestCenter(t *testing.T) {
82 runner := func(str string) string {
83 input := strings.Split(str, separator)
84 n, _ := strconv.Atoi(input[1])
85 return Center(input[0], n, input[2])
86 }
87
88 runTestCases(t, runner, _M{
89 sep("hello", "4", " "): "hello",
90 sep("hello", "10", " "): " hello ",
91 sep("hello", "10", "123"): "12hello123",
92
93 sep("hello中文test", "4", " "): "hello中文test",
94 sep("hello中文test", "12", " "): "hello中文test ",
95 sep("hello中文test", "18", "测试!"): "测试!hello中文test测试!测",
96
97 sep("hello中文test", "0", "123"): "hello中文test",
98 sep("hello中文test", "18", ""): "hello中文test",
99 })
100 }
101
View as plain text