-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
227 lines (211 loc) · 6.19 KB
/
Copy pathparse_test.go
File metadata and controls
227 lines (211 loc) · 6.19 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package uuid
import (
"errors"
"strings"
"testing"
)
func TestParse(t *testing.T) {
tests := []struct {
input string
want string
}{
{"6ba7b810-9dad-11d1-80b4-00c04fd430c8", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"},
{"00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000"},
{"ffffffff-ffff-ffff-ffff-ffffffffffff", "ffffffff-ffff-ffff-ffff-ffffffffffff"},
{"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF", "ffffffff-ffff-ffff-ffff-ffffffffffff"},
{"550e8400-e29b-41d4-a716-446655440000", "550e8400-e29b-41d4-a716-446655440000"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
u, err := Parse(tt.input)
if err != nil {
t.Fatalf("Parse(%q) unexpected error: %v", tt.input, err)
}
if got := u.String(); got != tt.want {
t.Errorf("Parse(%q) = %s, want %s", tt.input, got, tt.want)
}
})
}
}
func TestParseErrors(t *testing.T) {
tests := []struct {
input string
desc string
}{
{"", "empty"},
{"6ba7b810-9dad-11d1-80b4-00c04fd430c", "too short"},
{"6ba7b810-9dad-11d1-80b4-00c04fd430c8a", "too long"},
{"6ba7b810+9dad-11d1-80b4-00c04fd430c8", "wrong separator"},
{"6ba7b810-9dad+11d1-80b4-00c04fd430c8", "wrong separator 2"},
{"6ba7b810-9dad-11d1+80b4-00c04fd430c8", "wrong separator 3"},
{"6ba7b810-9dad-11d1-80b4+00c04fd430c8", "wrong separator 4"},
{"6ba7b810-9dad-11d1-80b4-00c04fd430cg", "invalid hex"},
{"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8", "URN not accepted"},
{"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "braced not accepted"},
{"6ba7b8109dad11d180b400c04fd430c8", "compact not accepted"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
_, err := Parse(tt.input)
if err == nil {
t.Fatalf("Parse(%q) should return error", tt.input)
}
var perr *ParseError
if !errors.As(err, &perr) {
t.Fatalf("Parse(%q) error type = %T, want *ParseError", tt.input, err)
}
})
}
}
func TestParseErrorsAsType(t *testing.T) {
_, err := Parse("not-a-uuid")
perr, ok := errors.AsType[*ParseError](err)
if !ok {
t.Fatalf("errors.AsType[*ParseError] returned false")
}
if perr.Input != "not-a-uuid" {
t.Errorf("ParseError.Input = %q, want %q", perr.Input, "not-a-uuid")
}
}
func TestParseErrorMessage(t *testing.T) {
_, err := Parse("bad")
msg := err.Error()
if msg == "" {
t.Fatal("ParseError.Error() should not be empty")
}
if !strings.Contains(msg, "bad") {
t.Errorf("ParseError.Error() = %q, should contain input", msg)
}
}
func TestLengthErrorMessage(t *testing.T) {
_, err := FromBytes([]byte{1, 2})
msg := err.Error()
if msg == "" {
t.Fatal("LengthError.Error() should not be empty")
}
if !strings.Contains(msg, "2") {
t.Errorf("LengthError.Error() = %q, should contain length", msg)
}
}
func TestParseLenient(t *testing.T) {
want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
tests := []struct {
name string
input string
}{
{"standard", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"},
{"URN", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"},
{"braced", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"},
{"compact", "6ba7b8109dad11d180b400c04fd430c8"},
{"compact upper", "6BA7B8109DAD11D180B400C04FD430C8"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := ParseLenient(tt.input)
if err != nil {
t.Fatalf("ParseLenient(%q) unexpected error: %v", tt.input, err)
}
if got := u.String(); got != want {
t.Errorf("ParseLenient(%q) = %s, want %s", tt.input, got, want)
}
})
}
}
func TestParseLenientErrors(t *testing.T) {
tests := []struct {
input string
desc string
}{
{"", "empty"},
{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "invalid hex"},
{"6ba7b810+9dad+11d1+80b4+00c04fd430c8", "bad hyphens standard"},
{"abc:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8", "wrong URN prefix"},
{"[6ba7b810-9dad-11d1-80b4-00c04fd430c8]", "wrong braces"},
{"6ba7b8109dad11d180b400c04fd430cg", "invalid hex compact"},
{"6ba7b810-9dad-11d1-80b4-00c04fd430c8-extra", "too long"},
{"short", "too short"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
_, err := ParseLenient(tt.input)
if err == nil {
t.Fatalf("ParseLenient(%q) should return error", tt.input)
}
})
}
}
func TestParseLenientURNBadHyphens(t *testing.T) {
_, err := ParseLenient("urn:uuid:6ba7b810+9dad-11d1-80b4-00c04fd430c8")
if err == nil {
t.Fatal("expected error for URN with bad hyphens")
}
}
func TestParseLenientBracedBadHyphens(t *testing.T) {
_, err := ParseLenient("{6ba7b810+9dad-11d1-80b4-00c04fd430c8}")
if err == nil {
t.Fatal("expected error for braced with bad hyphens")
}
}
func TestMustParse(t *testing.T) {
u := MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if u.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
t.Errorf("MustParse returned wrong UUID")
}
}
func TestMustParsePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("MustParse should panic on invalid input")
}
}()
MustParse("invalid")
}
func TestFromBytes(t *testing.T) {
want := MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
b := want.Bytes()
got, err := FromBytes(b)
if err != nil {
t.Fatalf("FromBytes() unexpected error: %v", err)
}
if got != want {
t.Errorf("FromBytes() = %v, want %v", got, want)
}
}
func TestFromBytesError(t *testing.T) {
_, err := FromBytes([]byte{1, 2, 3})
if err == nil {
t.Fatal("FromBytes should fail for wrong length")
}
var lerr *LengthError
if !errors.As(err, &lerr) {
t.Fatalf("error type = %T, want *LengthError", err)
}
if lerr.Got != 3 {
t.Errorf("LengthError.Got = %d, want 3", lerr.Got)
}
// Also test errors.AsType
lerr2, ok := errors.AsType[*LengthError](err)
if !ok {
t.Fatal("errors.AsType[*LengthError] returned false")
}
if lerr2.Got != 3 {
t.Errorf("errors.AsType LengthError.Got = %d, want 3", lerr2.Got)
}
}
func TestParseRoundTrip(t *testing.T) {
inputs := []string{
"00000000-0000-0000-0000-000000000000",
"ffffffff-ffff-ffff-ffff-ffffffffffff",
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"550e8400-e29b-41d4-a716-446655440000",
}
for _, s := range inputs {
u, err := Parse(s)
if err != nil {
t.Fatalf("Parse(%q): %v", s, err)
}
if got := u.String(); got != s {
t.Errorf("round-trip: Parse(%q).String() = %q", s, got)
}
}
}