forked from nu7hatch/gouuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
164 lines (151 loc) · 5.09 KB
/
uuid_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
// This package provides immutable UUID structs and the functions
// NewV3, NewV4, NewV5 and Parse() for generating versions 3, 4
// and 5 UUIDs as specified in RFC 4122.
//
// Copyright (C) 2011 by Krzysztof Kowalik <[email protected]>
package uuid
import (
"encoding/json"
"regexp"
"testing"
)
const format = "^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$"
func TestParse(t *testing.T) {
_, err := Parse([]byte{1, 2, 3, 4, 5})
if err == nil {
t.Errorf("Expected error due to invalid UUID sequence")
}
base, _ := NewV4()
u, err := Parse(base[:])
if err != nil {
t.Errorf("Expected to parse UUID sequence without problems")
return
}
if u.String() != base.String() {
t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
}
}
func TestParseString(t *testing.T) {
_, err := ParseHex("foo")
if err == nil {
t.Errorf("Expected error due to invalid UUID string")
}
base, _ := NewV4()
u, err := ParseHex(base.String())
if err != nil {
t.Errorf("Expected to parse UUID sequence without problems")
return
}
if u.String() != base.String() {
t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
}
}
func TestNewV3(t *testing.T) {
u, err := NewV3(NamespaceURL, []byte("golang.org"))
if err != nil {
t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
return
}
if u.Version() != 3 {
t.Errorf("Expected to generate UUIDv3, given %d", u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected to generate UUIDv3 RFC4122 variant, given %x", u.Variant())
}
re := regexp.MustCompile(format)
if !re.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given %s", u.String())
}
u2, _ := NewV3(NamespaceURL, []byte("golang.org"))
if u2.String() != u.String() {
t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
}
u3, _ := NewV3(NamespaceDNS, []byte("golang.org"))
if u3.String() == u.String() {
t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
}
u4, _ := NewV3(NamespaceURL, []byte("code.google.com"))
if u4.String() == u.String() {
t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
}
}
func TestNewV4(t *testing.T) {
u, err := NewV4()
if err != nil {
t.Errorf("Expected to generate UUID without problems, error thrown: %s", err.Error())
return
}
if u.Version() != 4 {
t.Errorf("Expected to generate UUIDv4, given %d", u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected to generate UUIDv4 RFC4122 variant, given %x", u.Variant())
}
re := regexp.MustCompile(format)
if !re.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given %s", u.String())
}
}
func TestNewV5(t *testing.T) {
u, err := NewV5(NamespaceURL, []byte("golang.org"))
if err != nil {
t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
return
}
if u.Version() != 5 {
t.Errorf("Expected to generate UUIDv5, given %d", u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected to generate UUIDv5 RFC4122 variant, given %x", u.Variant())
}
re := regexp.MustCompile(format)
if !re.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given %s", u.String())
}
u2, _ := NewV5(NamespaceURL, []byte("golang.org"))
if u2.String() != u.String() {
t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
}
u3, _ := NewV5(NamespaceDNS, []byte("golang.org"))
if u3.String() == u.String() {
t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
}
u4, _ := NewV5(NamespaceURL, []byte("code.google.com"))
if u4.String() == u.String() {
t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
}
}
func TestMarshalJSON(t *testing.T) {
str := "426af4e2-3f13-46e0-45ae-0dd5941536b6"
u, err := ParseHex(str)
if err != nil {
t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
return
}
jsonBytes, err := json.Marshal(u)
if err != nil {
t.Errorf("Expected to marshal json without problems, error thrown: %d", err.Error())
return
}
jsonString := string(jsonBytes)
if jsonString != ("\"" + str + "\"") {
t.Errorf("Expected marshalled json string to be the quoted version of the original string, json string %s != original string \"%s\"", jsonString, str)
}
}
func TestMarshalJSONValue(t *testing.T) {
str := "426af4e2-3f13-46e0-45ae-0dd5941536b6"
u, err := ParseHex(str)
if err != nil {
t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
return
}
jsonBytes, err := json.Marshal(*u)
if err != nil {
t.Errorf("Expected to marshal json without problems, error thrown: %d", err.Error())
return
}
jsonString := string(jsonBytes)
if jsonString != ("\"" + str + "\"") {
t.Errorf("Expected marshalled json string to be the quoted version of the original string, json string %s != original string \"%s\"", jsonString, str)
}
}