-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
147 lines (140 loc) · 2.16 KB
/
util_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import "testing"
func TestCountDigits(t *testing.T) {
for _, tc := range []struct {
val int
exp int
}{
{123, 3},
{1, 1},
{13, 2},
{0, 1},
{1e9, 10},
{-100, 3},
{-1, 1},
} {
if exp, got := tc.exp, countDigits(tc.val); exp != got {
t.Errorf("\ncountDigits(%d): -%d +%d", tc.val, exp, got)
}
}
}
func TestParseSize(t *testing.T) {
for _, tc := range []struct {
in string
out int64
}{
{"100b", 100},
{"1B", 1},
{"101 B", 101},
{"1 b", 1},
{"1 kb", 1000},
{"124Kb", 124 * 1000},
} {
exp := tc.out
got, err := parseSize(tc.in)
if err != nil {
t.Error(err)
continue
}
if exp != got {
t.Errorf("\nparseSize(%q) => -%d +%d", tc.in, exp, got)
}
}
}
func TestTrimWhitespace(t *testing.T) {
for _, tc := range []struct {
in string
out string
ws []whitespace
}{
{
" a b c ",
" a b c ",
[]whitespace{wsBOF},
},
{
" a b c ",
"a b c ",
[]whitespace{wsBOL},
},
{
" a b c ",
"a b c",
[]whitespace{wsBOL, wsEOL},
},
{
" a b c ",
"a b c ",
[]whitespace{wsBOL, wsEOF},
},
{
" a b c \n",
"a b c ",
[]whitespace{wsBOL, wsEOF},
},
{
"\n\n\n\na\nb\nc",
"a\nb\nc",
[]whitespace{wsBOF},
},
{
"\n\n\n\n\ta\n b\n\tc",
"a\nb\nc",
[]whitespace{wsBOF, wsBOL},
},
{
`
a
b
c
`,
"a\n\nb\n\nc",
[]whitespace{wsBOF, wsBOL, wsEOF},
},
} {
if exp, got := tc.out, trimWhitespace(tc.in, tc.ws...); exp != got {
t.Errorf("\n%q - %v:\n -%q\n +%q", tc.in, tc.ws, exp, got)
}
}
}
func TestJustify(t *testing.T) {
for _, tc := range []struct {
lr rune
in string
width int
pad string
out string
}{
{
lr: 'r',
in: "abc",
width: 5,
out: " abc",
},
{
lr: 'l',
in: "abc",
width: 5,
out: "abc ",
},
{
lr: 'l',
in: "abc",
pad: "XYZ",
width: 5,
out: "abcXY",
},
{
lr: 'l',
in: "abc",
pad: "XYZ",
width: 6,
out: "abcXYZ",
},
} {
exp, got := tc.out, justify(tc.lr, tc.in, tc.width, tc.pad)
if exp != got {
t.Errorf("\njustify(%c, %q, %d, %q): -%q +%q", tc.lr, tc.in, tc.width, tc.pad, exp, got)
}
}
}