-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmstr_test.go
148 lines (134 loc) · 4.08 KB
/
mstr_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
148
package mstr_test
import (
"testing"
"github.com/creachadair/mds/mstr"
gocmp "github.com/google/go-cmp/cmp"
)
func TestTrunc(t *testing.T) {
tests := []struct {
input string
size int
want string
}{
{"", 0, ""}, // n == length
{"", 1000, ""}, // n > length
{"abc", 4, "abc"}, // n > length
{"abc", 3, "abc"}, // n == length
{"abcdefg", 4, "abcd"}, // n < length, safe
{"abcdefg", 0, ""}, // n < length, safe
{"abc\U0001f60a", 3, "abc"}, // n < length, at boundary
{"abc\U0001f60a", 4, "abc"}, // n < length, mid-rune
{"abc\U0001f60a", 5, "abc"}, // n < length, mid-rune
{"abc\U0001f60a", 6, "abc"}, // n < length, mid-rune
{"abc\U0001f60axxx", 7, "abc"}, // n < length, cut multibyte
{"abc\U0001f60axxx", 8, "abc\U0001f60ax"}, // n < length, keep multibyte
}
for _, tc := range tests {
t.Logf("Input %q len=%d n=%d", tc.input, len(tc.input), tc.size)
got := mstr.Trunc(tc.input, tc.size)
if got != tc.want {
t.Errorf("Trunc(%q, %d) [string]: got %q, want %q", tc.input, tc.size, got, tc.want)
}
if got := mstr.Trunc([]byte(tc.input), tc.size); string(got) != tc.want {
t.Errorf("Trunc(%q, %d) [bytes]: got %q, want %q", tc.input, tc.size, got, tc.want)
}
}
}
func TestLines(t *testing.T) {
tests := []struct {
input string
want []string
}{
{"", nil},
{" ", []string{" "}},
{"\n", []string{""}},
{"\n ", []string{"", " "}},
{"a\n", []string{"a"}},
{"\na\n", []string{"", "a"}},
{"a\nb\n", []string{"a", "b"}},
{"a\nb", []string{"a", "b"}},
{"\n\n\n", []string{"", "", ""}},
{"\n\nq", []string{"", "", "q"}},
{"\n\nq\n", []string{"", "", "q"}},
{"a b\nc\n\n", []string{"a b", "c", ""}},
{"a b\nc\n\nd\n", []string{"a b", "c", "", "d"}},
}
for _, tc := range tests {
if diff := gocmp.Diff(mstr.Lines(tc.input), tc.want); diff != "" {
t.Errorf("Lines %q (-got, +want):\n%s", tc.input, diff)
}
}
}
func TestSplit(t *testing.T) {
tests := []struct {
input, sep string
want []string
}{
{"", "x", nil},
{"y", "x", []string{"y"}},
{"x", "x", []string{"", ""}},
{"ax", "x", []string{"a", ""}},
{"xa", "x", []string{"", "a"}},
{"axbxc", "x", []string{"a", "b", "c"}},
{"axxc", "x", []string{"a", "", "c"}},
{"a,b,c,,d", ",", []string{"a", "b", "c", "", "d"}},
}
for _, tc := range tests {
if diff := gocmp.Diff(mstr.Split(tc.input, tc.sep), tc.want); diff != "" {
t.Errorf("Split %q on %q (-got, +want):\n%s", tc.input, tc.sep, diff)
}
}
}
func TestCompareNatural(t *testing.T) {
tests := []struct {
a, b string
want int
}{
{"", "", 0},
// Non-empty vs. empty with non-digits and digits.
{"x", "", 1},
{"", "x", -1},
{"0", "", 1},
{"", "0", -1},
// Leading zeroes do not change the value.
{"1", "1", 0},
{"01", "1", 0},
{"1", "01", 0},
// Mixed values.
{"a1", "a1", 0},
{"a2", "a1", 1},
{"a1", "a2", -1},
{"6c", "06c", 0},
{"06c", "6c", 0},
{"5c", "06c", -1},
{"07c", "6c", 1},
// Multi-digit numeric runs.
{"a2b", "a25b", -1},
{"a12b", "a2", 1},
{"a25b", "a21b", 1},
{"a025b", "a25b", 0},
// Non-matching types compare lexicographically.
// Note it is not possible for these to be equal.
{"123", "a", -1}, // because 'a' > '1'
{"123", ".", 1}, // because '.' < '1'
{"12c9", "12cv", -1}, // because 'v' > '9'
// Normal lexicographic comparison, without digits.
{"a-b-c", "a-b-c", 0},
{"a-b-c", "a-b-d", -1},
{"a-b-c-d", "a-b-d", -1},
{"a-q", "a-b-c", 1},
{"a-q-c", "a-b-c", 1},
// Complicated cases ("v" indicates the point of divergence).
// v v
{"test1-143a19", "test01-143b13", -1},
// v v
{"test5-143a21", "test04-999", 1},
// v v 'w' > '9'
{"test5-word-5", "test5-999-5", 1},
}
for _, tc := range tests {
if got := mstr.CompareNatural(tc.a, tc.b); got != tc.want {
t.Errorf("Compare(%q, %q): got %v, want %v", tc.a, tc.b, got, tc.want)
}
}
}