@@ -31,6 +31,57 @@ func TestJSON(t *testing.T) {
31
31
}
32
32
}
33
33
34
+ func TestJSONUnmarshal (t * testing.T ) {
35
+ type S struct {
36
+ ID1 UUID
37
+ ID2 UUID `json:"ID2,omitempty"`
38
+ }
39
+
40
+ testCases := map [string ]struct {
41
+ data []byte
42
+ expectedError error
43
+ expectedResult UUID
44
+ }{
45
+ "success" : {
46
+ data : []byte (`{"ID1": "f47ac10b-58cc-0372-8567-0e02b2c3d479"}` ),
47
+ expectedError : nil ,
48
+ expectedResult : testUUID ,
49
+ },
50
+ "zero" : {
51
+ data : []byte (`{"ID1": "00000000-0000-0000-0000-000000000000"}` ),
52
+ expectedError : nil ,
53
+ expectedResult : Nil ,
54
+ },
55
+ "null" : {
56
+ data : []byte (`{"ID1": null}` ),
57
+ expectedError : nil ,
58
+ expectedResult : Nil ,
59
+ },
60
+ "empty" : {
61
+ data : []byte (`{"ID1": ""}` ),
62
+ expectedError : invalidLengthError {len : 0 },
63
+ expectedResult : Nil ,
64
+ },
65
+ "omitempty" : {
66
+ data : []byte (`{"ID2": ""}` ),
67
+ expectedError : invalidLengthError {len : 0 },
68
+ expectedResult : Nil ,
69
+ },
70
+ }
71
+
72
+ for name , tc := range testCases {
73
+ t .Run (name , func (t * testing.T ) {
74
+ var s S
75
+ if err := json .Unmarshal (tc .data , & s ); err != tc .expectedError {
76
+ t .Errorf ("unexpected error: got %v, want %v" , err , tc .expectedError )
77
+ }
78
+ if ! reflect .DeepEqual (s .ID1 , tc .expectedResult ) {
79
+ t .Errorf ("got %#v, want %#v" , s .ID1 , tc .expectedResult )
80
+ }
81
+ })
82
+ }
83
+ }
84
+
34
85
func BenchmarkUUID_MarshalJSON (b * testing.B ) {
35
86
x := & struct {
36
87
UUID UUID `json:"uuid"`
0 commit comments