-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ef60ba
commit d8927ad
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package jsn | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
want any | ||
errStr string | ||
}{ | ||
{ | ||
input: `"abc"`, | ||
want: "abc", | ||
}, | ||
{ | ||
input: `123`, | ||
want: float64(123), | ||
}, | ||
{ | ||
input: `{"abc": 123}`, | ||
want: map[string]any{"abc": float64(123)}, | ||
}, | ||
{ | ||
input: `{"abc": 123} `, | ||
want: map[string]any{"abc": float64(123)}, | ||
}, | ||
{ | ||
input: ` ["abc", 123] `, | ||
want: []any{"abc", float64(123)}, | ||
}, | ||
{ | ||
input: `{"abc": 123}a`, | ||
errStr: "body must contain only one JSON entity", | ||
}, | ||
{ | ||
input: `[123]{"a":"b"}`, | ||
errStr: "body must contain only one JSON entity", | ||
}, | ||
{ | ||
input: `{"abc`, | ||
errStr: "unexpected EOF", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
var val any | ||
err := Unmarshal([]byte(tc.input), &val) | ||
if err != nil { | ||
if tc.errStr == "" { | ||
t.Fatal(err) | ||
} | ||
if have := err.Error(); have != tc.errStr { | ||
t.Fatalf("\nhave: %+v\nwant: %+v\n", have, tc.errStr) | ||
} | ||
continue | ||
} | ||
|
||
if !reflect.DeepEqual(val, tc.want) { | ||
t.Fatalf("\nhave: %+v\nwant: %+v\n", val, tc.want) | ||
} | ||
} | ||
} |