-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_test.go
175 lines (167 loc) · 5.58 KB
/
marshal_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
package uuid
import (
"reflect"
"testing"
)
func TestParse(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
wantUUID UUID
wantErr bool
}{
{"Normal", args{"686e7778-f9f0-4622-a13e-c2441ce4ae41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"UpperCase", args{"686E7778-F9F0-4622-A13E-C2441CE4AE41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"MixedCase", args{"686E7778-F9f0-4622-A13e-C2441cE4aE41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"WrongLength", args{"01234567-89ab-cdef"}, UUID{}, true},
{"WrongDashPlacement", args{"012345678-9ab-cdef-012345-6789abcdef"}, UUID{}, true},
{"NonHexCharactersSection1", args{"ghijklmn-abcd-abcd-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection2", args{"abcdef01-opqr-abcd-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection3", args{"abcdef01-abcd-stuv-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection4", args{"abcdef01-abcd-abcd-wxyz-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection5", args{"abcdef01-abcd-abcd-abcd-ghijklmnopqr"}, UUID{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUUID, err := Parse(tt.args.str)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotUUID, tt.wantUUID) {
t.Errorf("Parse() = %v, want %v", gotUUID, tt.wantUUID)
}
})
}
}
func TestParseBytes(t *testing.T) {
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantUUID UUID
wantErr bool
}{
{"Normal", args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"WrongLength", args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e}}, UUID{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUUID, err := ParseBytes(tt.args.bytes)
if (err != nil) != tt.wantErr {
t.Errorf("ParseBytes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotUUID, tt.wantUUID) {
t.Errorf("ParseBytes() = %v, want %v", gotUUID, tt.wantUUID)
}
})
}
}
func TestUUID_String(t *testing.T) {
tests := []struct {
name string
UUID *UUID
wantOut string
}{
{"ConvertToString", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, "686e7778-f9f0-4622-a13e-c2441ce4ae41"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := tt.UUID.String(); gotOut != tt.wantOut {
t.Errorf("UUID.String() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func TestUUID_MarshalText(t *testing.T) {
tests := []struct {
name string
uuid UUID
want []byte
wantErr bool
}{
{"Normal", UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, []byte("686e7778-f9f0-4622-a13e-c2441ce4ae41"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.uuid.MarshalText()
if (err != nil) != tt.wantErr {
t.Errorf("UUID.MarshalText() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UUID.MarshalText() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_UnmarshalText(t *testing.T) {
type args struct {
in []byte
}
tests := []struct {
name string
uuid *UUID
args args
wantErr bool
}{
{"Normal", &UUID{}, args{[]byte{0x36, 0x38, 0x36, 0x65, 0x37, 0x37, 0x37, 0x38, 0x2d, 0x66, 0x39, 0x66, 0x30, 0x2d, 0x34, 0x36, 0x32, 0x32, 0x2d, 0x61, 0x31, 0x33, 0x65, 0x2d, 0x63, 0x32, 0x34, 0x34, 0x31, 0x63, 0x65, 0x34, 0x61, 0x65, 0x34, 0x31}}, false},
{"InvalidID", &UUID{}, args{[]byte{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.uuid.UnmarshalText(tt.args.in); (err != nil) != tt.wantErr {
t.Errorf("UUID.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUUID_MarshalBinary(t *testing.T) {
tests := []struct {
name string
uuid UUID
want []byte
wantErr bool
}{
{"Normal", UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, []byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.uuid.MarshalBinary()
if (err != nil) != tt.wantErr {
t.Errorf("UUID.MarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UUID.MarshalBinary() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_UnmarshalBinary(t *testing.T) {
type args struct {
in []byte
}
tests := []struct {
name string
uuid *UUID
args args
wantErr bool
}{
{"Normal", &UUID{}, args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}}, false},
{"InvalidID", &UUID{}, args{[]byte{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.uuid.UnmarshalBinary(tt.args.in); (err != nil) != tt.wantErr {
t.Errorf("UUID.UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}