Skip to content

Commit

Permalink
adding tests for custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ja1ns committed Sep 6, 2022
1 parent 179b1b0 commit cedebfb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ func (u *unmarshalJSON) UnmarshalJSON(b []byte) error {
return nil
}

func Test_unmarshalCustomTag(t *testing.T) {
type v struct {
Name string `cus:"n" json:"name"`
Age string `cus:"a" json:"age"`
ID string `cus:"no" json:"id"`
}
content := []byte(`{"n": "name", "a": "age", "no": "id"}`)
var v1 v
json.UnmarshalWithOption(content, &v1, json.DecodeWithTag("cus"))
assertEq(t, "v1.Name", "name", v1.Name)
assertEq(t, "v1.Age", "age", v1.Age)
assertEq(t, "v1.ID", "id", v1.ID)
}

func Test_UnmarshalJSON(t *testing.T) {
t.Run("*struct", func(t *testing.T) {
var v unmarshalJSON
Expand Down
13 changes: 13 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,19 @@ func TestRecursivePtrHead(t *testing.T) {
}
}

func Test_MarshalCustomTag(t *testing.T) {
type v struct {
A int `tag:"a"`
B int `tag:"b"`
}
v1 := v{A: 2, B: 3}
response, err := json.MarshalWithOption(v1, json.EncodeWithTag("tag"))
if err != nil {
t.Fatal(err)
}
assertEq(t, "marshalled string", `{"a":2,"b":3}`, string(response))
}

func TestMarshalIndent(t *testing.T) {
v := map[string]map[string]interface{}{
"a": {
Expand Down

0 comments on commit cedebfb

Please sign in to comment.