Skip to content

Commit 06716f6

Browse files
authored
chore(tests): Add json.Unmarshal test with empty value cases (#116)
1 parent b3cae7c commit 06716f6

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Diff for: json_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,57 @@ func TestJSON(t *testing.T) {
3131
}
3232
}
3333

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+
3485
func BenchmarkUUID_MarshalJSON(b *testing.B) {
3586
x := &struct {
3687
UUID UUID `json:"uuid"`

0 commit comments

Comments
 (0)