This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
107 lines (83 loc) · 2.36 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
package uuid_test
import (
"github.com/4xoc/uuid"
"testing"
)
func TestMain(t *testing.T) {
var (
myScopes [64]string
mySetScopes [64]string
myUUID *uuid.UUID
myUUID2 *uuid.UUID
err error
)
//trying uninitialized package (no scopes set)
_, err = uuid.New("foo")
if err == nil {
t.Error("There are no scopes defined thus there should be no new uuid")
}
//setting scopes
myScopes = [64]string{"one", "two", "three", "four", "five", "six", "seven", "eight"}
err = uuid.SetScopes(myScopes)
if err != nil {
t.Error("scopes should have been set")
}
//getting scopes
mySetScopes = uuid.Scopes()
if len(mySetScopes) != len(myScopes) {
t.Error("Expected ", len(myScopes), "# of scopes but got ", mySetScopes)
}
//getting scope on nil ptr
if myUUID.Scope() != "" {
t.Error("Scope wasn't empty string as expected")
}
//getting hex on nil ptr
if myUUID.Hex() != "" {
t.Error("Hex wasn't empty string as expected")
}
//getting bin on nil ptr
if myUUID.Bin() != [16]byte{} {
t.Error("Bin data should be an empty byte arra but wasn't.")
}
//creating new uuid with known scope
myUUID, err = uuid.New("five")
if err != nil {
t.Error("Expected UUID to be generated but failed with error ", err.Error())
}
if myUUID.Scope() != "five" {
t.Error("UUID does not match the scope defined on creation time.")
}
//read good UUID
myUUID2, err = uuid.Read(myUUID.Hex())
if err != nil {
t.Error("Expected UUID to be generated but failed with error ", err.Error())
}
if myUUID.Hex() != myUUID2.Hex() ||
myUUID.Bin() != myUUID2.Bin() ||
myUUID.Scope() != myUUID2.Scope() {
t.Error("UUIDs should be identical but aren't")
}
if !myUUID2.ScopeMatches(myScopes[:]) {
t.Error("Scope should match but did not.")
}
if myUUID2.ScopeMatches([]string{"ten"}) {
t.Error("Scope should match but did not.")
}
//now to the bad things
//reading a bad UUID
_, err = uuid.Read(myUUID.Hex()[:1])
if err == nil {
t.Error("UUID shouldn't have been generated")
}
//now setting new set of scopes
myScopes = [64]string{"one"}
err = uuid.SetScopes(myScopes)
if err == nil {
t.Error("setting new scopes should not have been possible")
}
//reading a previously valid UUID which now has an unknown scope
_, err = uuid.Read("ff8cb1d0-84f3-9d8d-76cc-682d1ca34dae")
if err == nil {
t.Error("UUID shouldn't have been generated")
}
}