1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package json
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
)
type StructA struct {
Name string
ID int `json:"id"`
Meta *StructB
AdditionalFields map[string]interface{}
}
type StructB struct {
Address string
AdditionalFields map[string]interface{}
}
type StructC struct {
Time time.Time
Project StructD
AdditionalFields map[string]interface{}
}
type StructD struct {
Project string
Info StructE
AdditionalFields map[string]interface{}
}
type StructE struct {
Employees int
AdditionalFields map[string]interface{}
}
func TestUnmarshal(t *testing.T) {
now := time.Now()
nowJSON, err := now.MarshalJSON()
if err != nil {
panic(err)
}
tests := []struct {
desc string
b []byte
got interface{}
want interface{}
err bool
}{
{
desc: "receiver not a pointer",
got: StructA{},
b: []byte(`{"content": "value"}`),
err: true,
},
{
desc: "receiver not a pointer to a struct",
got: new(string),
b: []byte(`{"content": "value"}`),
err: true,
},
{
desc: "AdditionalFields not a map",
b: []byte(`{"content": "value"}`),
got: &struct {
AdditionalFields string
}{},
err: true,
},
{
desc: "Success, no json.Unmarshaler types",
b: []byte(
`
{
"Name": "John",
"id": 3,
"Meta": {
"Address": "291 Street",
"unknown0": 3.2
},
"unknown0": 10,
"unknown1": "hello"
}
`,
),
got: &StructA{},
want: &StructA{
Name: "John",
ID: 3,
Meta: &StructB{
Address: "291 Street",
AdditionalFields: map[string]interface{}{
"unknown0": MarshalRaw(3.2),
},
},
AdditionalFields: map[string]interface{}{
"unknown0": MarshalRaw(10),
"unknown1": MarshalRaw("hello"),
},
},
},
{
desc: "Success, a type has json.Unmarshaler",
b: []byte(fmt.Sprintf(`
{
"Time":%s,
"Project": {
"Project":"myProject",
"Info":{
"Employees":2
}
}
}
`, string(nowJSON))),
got: &StructC{},
want: &StructC{
Time: now,
Project: StructD{
Project: "myProject",
Info: StructE{
Employees: 2,
},
},
},
},
}
for _, test := range tests {
err := Unmarshal(test.b, test.got)
switch {
case err == nil && test.err:
t.Errorf("TestUnmarshal(%s): got err == nil, want err != nil", test.desc)
continue
case err != nil && !test.err:
t.Errorf("TestUnmarshal(%s): got err == %s, want err == nil", test.desc, err)
continue
case err != nil:
continue
}
if diff := (&pretty.Config{IncludeUnexported: false}).Compare(test.want, test.got); diff != "" {
t.Errorf("TestUnmarshal(%s): -want/+got:\n%s", test.desc, diff)
}
}
}
func TestIsDelim(t *testing.T) {
tests := []struct {
desc string
token json.Token
want bool
}{
{desc: "Is delim", token: json.Delim('{'), want: true},
{desc: "Not a delim", token: json.Token("{"), want: false},
}
for _, test := range tests {
got := isDelim(test.token)
if got != test.want {
t.Errorf("TestIsDelim(%s): got %v, want %v", test.desc, got, test.want)
}
}
}
func TestDelimIs(t *testing.T) {
tests := []struct {
desc string
token json.Token
delim rune
want bool
}{
{desc: "Token is a match", token: json.Delim('{'), delim: '{', want: true},
{desc: "Token is not a match", token: json.Delim('{'), delim: '}', want: false},
}
for _, test := range tests {
got := delimIs(test.token, test.delim)
if got != test.want {
t.Errorf("TestDelimIs(%s): got %v, want %v", test.desc, got, test.want)
}
}
}
|