-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuid_test.go
More file actions
75 lines (66 loc) · 1.86 KB
/
fuid_test.go
File metadata and controls
75 lines (66 loc) · 1.86 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
package fuid_test
import (
"testing"
"github.com/bsm/fuid"
)
var fixtures = []struct {
F string
U string
}{
{F: "WCSV3oHUQbOlNGLc1qUZqA", U: "582495de-81d4-41b3-a534-62dcd6a519a8"},
{F: "t-dJ9X1zQ4mmZ84hX-Hl3Q", U: "b7e749f5-7d73-4389-a667-ce215fe1e5dd"},
{F: "HpegzQv3QRKqbDJkMn_TcA", U: "1e97a0cd-0bf7-4112-aa6c-3264327fd370"},
{F: "qaysnQSLR4mXtRv__A0SdA", U: "a9acac9d-048b-4789-97b5-1bfffc0d1274"},
{F: "_IlPjHRej2ZRtujH6zf--Q", U: "fc894f8c-745e-8f66-51b6-e8c7eb37fef9"},
}
func TestFUIDString(t *testing.T) {
u := fuid.New()
if s := u.String(); len(s) != 22 {
t.Errorf("expected %q to be 22 chars long", s)
}
}
func TestParse(t *testing.T) {
t.Run("base64", func(t *testing.T) {
for _, c := range fixtures {
u, err := fuid.Parse(c.F)
if err != nil {
t.Errorf("unexpected error %v, for input %q", err, c.F)
} else if got := u.UUID().String(); c.U != got {
t.Errorf("expected %q for input %q but got: %q", c.U, c.F, got)
}
}
})
t.Run("bad base64", func(t *testing.T) {
cases := []struct {
I string
C string
}{
{I: "WC3oHUQbOlNGLc1qUZqA", C: "too short"},
{I: "qoVz0bXNjZ3492SbdNZ/p6", C: "not URL encoded"},
}
for _, c := range cases {
_, err := fuid.Parse(c.I)
if err == nil {
t.Errorf("unexpected error %q, got not", c.C)
}
}
})
t.Run("quoted", func(t *testing.T) {
u, err := fuid.Parse(`"WCSV3oHUQbOlNGLc1qUZqA"`)
if err != nil {
t.Error("unexpected error", err)
} else if exp, got := fixtures[0].F, u.String(); exp != got {
t.Errorf("expected %q but got: %q", exp, got)
}
})
t.Run("from UUIDs", func(t *testing.T) {
for _, c := range fixtures {
u, err := fuid.Parse(c.U)
if err != nil {
t.Errorf("unexpected error %v, for input %q", err, c.U)
} else if exp, got := c.F, u.String(); exp != got {
t.Errorf("expected %q for input %q but got: %q", c.F, c.U, got)
}
}
})
}