Skip to content

Commit 6d9254e

Browse files
committed
test(marshal): add marshal/unmarshal tests
1 parent 2d3c2a9 commit 6d9254e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

marshal_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import (
8+
"encoding/json"
9+
"testing"
10+
)
11+
12+
func TestMarshalText(t *testing.T) {
13+
u := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
14+
expected := "01234567-89ab-cdef-fedc-ba9876543210"
15+
16+
data, err := u.MarshalText()
17+
if err != nil {
18+
t.Fatalf("MarshalText failed: %v", err)
19+
}
20+
21+
if string(data) != expected {
22+
t.Errorf("MarshalText() = %s; want %s", data, expected)
23+
}
24+
}
25+
26+
func TestUnmarshalText(t *testing.T) {
27+
var u UUID
28+
input := []byte("01234567-89ab-cdef-fedc-ba9876543210")
29+
expected := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
30+
31+
err := u.UnmarshalText(input)
32+
if err != nil {
33+
t.Fatalf("UnmarshalText failed: %v", err)
34+
}
35+
36+
if u != expected {
37+
t.Errorf("UnmarshalText() = %v; want %v", u, expected)
38+
}
39+
}
40+
41+
func TestUnmarshalTextInvalidFormat(t *testing.T) {
42+
var u UUID
43+
input := []byte("invalid-uuid-format")
44+
45+
err := u.UnmarshalText(input)
46+
if err == nil {
47+
t.Fatalf("UnmarshalText should have failed for invalid format")
48+
}
49+
}
50+
51+
func TestMarshalBinary(t *testing.T) {
52+
u := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
53+
expected := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
54+
55+
data, err := u.MarshalBinary()
56+
if err != nil {
57+
t.Fatalf("MarshalBinary failed: %v", err)
58+
}
59+
60+
if string(data) != string(expected) {
61+
t.Errorf("MarshalBinary() = %v; want %v", data, expected)
62+
}
63+
}
64+
65+
func TestUnmarshalBinary(t *testing.T) {
66+
var u UUID
67+
input := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
68+
expected := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
69+
70+
err := u.UnmarshalBinary(input)
71+
if err != nil {
72+
t.Fatalf("UnmarshalBinary failed: %v", err)
73+
}
74+
75+
if u != expected {
76+
t.Errorf("UnmarshalBinary() = %v; want %v", u, expected)
77+
}
78+
}
79+
80+
func TestUnmarshalBinaryInvalidLength(t *testing.T) {
81+
var u UUID
82+
input := []byte{0x01, 0x23, 0x45} // Invalid length
83+
84+
err := u.UnmarshalBinary(input)
85+
if err == nil {
86+
t.Fatalf("UnmarshalBinary should have failed for invalid length")
87+
}
88+
}
89+
90+
func TestJSONSerialization(t *testing.T) {
91+
u := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
92+
expected := `"01234567-89ab-cdef-fedc-ba9876543210"`
93+
94+
jsonData, err := json.Marshal(u)
95+
if err != nil {
96+
t.Fatalf("JSON serialization failed: %v", err)
97+
}
98+
99+
if string(jsonData) != expected {
100+
t.Errorf("JSON serialization = %s; want %s", jsonData, expected)
101+
}
102+
}
103+
104+
func TestJSONDeserialization(t *testing.T) {
105+
var u UUID
106+
input := `"01234567-89ab-cdef-fedc-ba9876543210"`
107+
expected := UUID{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
108+
109+
err := json.Unmarshal([]byte(input), &u)
110+
if err != nil {
111+
t.Fatalf("JSON deserialization failed: %v", err)
112+
}
113+
114+
if u != expected {
115+
t.Errorf("JSON deserialization = %v; want %v", u, expected)
116+
}
117+
}

0 commit comments

Comments
 (0)