-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyuuid_test.go
219 lines (203 loc) · 6.51 KB
/
prettyuuid_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
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
package prettyuuid_test
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/ssoready/prettyuuid"
)
func Example() {
format := prettyuuid.MustNewFormat("invoice_", "0123456789abcdefghijklmnopqrstuvwxyz")
fmt.Println(format.Format(uuid.MustParse("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")))
id, _ := format.Parse("invoice_eoswzolg3bsx0zn8otq1p8oom")
fmt.Println(uuid.UUID(id).String())
// Output:
// invoice_eoswzolg3bsx0zn8otq1p8oom
// f81d4fae-7dec-11d0-a765-00a0c91e6bf6
}
func TestNewFormat(t *testing.T) {
testCases := []struct {
Alphabet string
WantErr string
}{
{
Alphabet: "",
WantErr: `alphabet must have len >= 2: ""`,
},
{
Alphabet: "a",
WantErr: `alphabet must have len >= 2: "a"`,
},
{
Alphabet: "aa",
WantErr: `alphabet must not contain duplicate chars: "aa"`,
},
}
for _, tt := range testCases {
t.Run(fmt.Sprintf("%q", tt.Alphabet), func(t *testing.T) {
_, err := prettyuuid.NewFormat("", tt.Alphabet)
if d := cmp.Diff(tt.WantErr, err.Error()); d != "" {
t.Fatalf("NewFormat(%v) did not return expected err (-want +got):\n%s", tt.Alphabet, d)
}
})
}
}
func TestFormat_Parse(t *testing.T) {
testCases := []struct {
Name string
Format prettyuuid.Format
ID string
WantErr string
}{
{
Name: "bad length",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
ID: "00000000000000000000000000000000x",
WantErr: `"00000000000000000000000000000000x" does not have expected length 32`,
},
{
Name: "bad prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdefghijklmnopqrstuvwxyz"),
ID: "xxxxxx_0000000000000000000000000",
WantErr: `"xxxxxx_0000000000000000000000000" does not have expected prefix "prefix_"`,
},
{
Name: "bad char",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
ID: "0000000000000000000000000000000x",
WantErr: `"0000000000000000000000000000000x" contains illegal char at position 31`,
},
{
Name: "bad length and bad prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdef"),
ID: "notprefix_00000000000000000000000000000000x",
WantErr: `"notprefix_00000000000000000000000000000000x" does not have expected prefix "prefix_"`,
},
}
for _, tt := range testCases {
t.Run(tt.Name, func(t *testing.T) {
_, err := tt.Format.Parse(tt.ID)
if d := cmp.Diff(tt.WantErr, err.Error()); d != "" {
t.Fatalf("%v.Parse(%v) did not return expected err (-want +got):\n%s", tt.Format, tt.ID, d)
}
})
}
}
func TestFormat_roundtrip(t *testing.T) {
testCases := []struct {
Name string
Format prettyuuid.Format
UUID string
Want string
}{
{
Name: "round-trip zero uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
UUID: "00000000-0000-0000-0000-000000000000",
Want: "00000000000000000000000000000000",
},
{
Name: "round-trip one uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
UUID: "00000000-0000-0000-0000-000000000001",
Want: "00000000000000000000000000000001",
},
{
Name: "round-trip max uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
UUID: "ffffffff-ffff-ffff-ffff-ffffffffffff",
Want: "ffffffffffffffffffffffffffffffff",
},
{
Name: "round-trip sample uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdef"),
UUID: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
Want: "f81d4fae7dec11d0a76500a0c91e6bf6",
},
{
Name: "alphanumeric zero uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "00000000-0000-0000-0000-000000000000",
Want: "0000000000000000000000000",
},
{
Name: "alphanumeric one uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "00000000-0000-0000-0000-000000000001",
Want: "0000000000000000000000001",
},
{
Name: "alphanumeric max uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "ffffffff-ffff-ffff-ffff-ffffffffffff",
Want: "f5lxx1zz5pnorynqglhzmsp33",
},
{
Name: "alphanumeric sample uuid",
Format: prettyuuid.MustNewFormat("", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
Want: "eoswzolg3bsx0zn8otq1p8oom",
},
{
Name: "alphanumeric zero uuid with prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "00000000-0000-0000-0000-000000000000",
Want: "prefix_0000000000000000000000000",
},
{
Name: "alphanumeric one uuid with prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "00000000-0000-0000-0000-000000000001",
Want: "prefix_0000000000000000000000001",
},
{
Name: "alphanumeric max uuid with prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "ffffffff-ffff-ffff-ffff-ffffffffffff",
Want: "prefix_f5lxx1zz5pnorynqglhzmsp33",
},
{
Name: "alphanumeric sample uuid with prefix",
Format: prettyuuid.MustNewFormat("prefix_", "0123456789abcdefghijklmnopqrstuvwxyz"),
UUID: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
Want: "prefix_eoswzolg3bsx0zn8otq1p8oom",
},
}
for _, tt := range testCases {
t.Run(tt.Name, func(t *testing.T) {
id := uuid.MustParse(tt.UUID)
got := tt.Format.Format(id)
if d := cmp.Diff(tt.Want, got); d != "" {
t.Fatalf("%v.Format(%v) != %v (-want +got)\n%s", tt.Format, tt.UUID, tt.Want, d)
}
parsed, err := tt.Format.Parse(tt.Want)
if err != nil {
t.Fatalf("%v.Parse(%v) returned unexpected err: %v", tt.Format, tt.Want, err)
}
if d := cmp.Diff(id, uuid.UUID(parsed)); d != "" {
t.Fatalf("%v.Parse(%v) != %v (-want +got)\n%s", tt.Format, tt.Want, id, d)
}
})
}
}
func FuzzFormat_roundtrip(f *testing.F) {
f.Fuzz(func(t *testing.T, prefix, alphabet string, fuzzID []byte) {
// we have to take a byte slice because go fuzz does not take arrays
if len(fuzzID) != 16 {
return
}
id := (*[16]byte)(fuzzID)
format, err := prettyuuid.NewFormat(prefix, alphabet)
if err != nil {
return // we don't care about invalid formats
}
formatted := format.Format(*id)
parsed, err := format.Parse(formatted)
if err != nil {
t.Fatalf("%v.Parse(%v) err: %v", format, formatted, err)
}
if d := cmp.Diff(*id, parsed); d != "" {
t.Fatalf("%v.Parse(Format(%v)) does not round-trip: (-want +got)\n%s", format, id, d)
}
})
}