-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_unmarshal_v2_test.go
129 lines (111 loc) · 3.41 KB
/
json_unmarshal_v2_test.go
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
package ordmap_test
import (
"reflect"
"testing"
"github.com/MarkRosemaker/ordmap"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
func (om *UserDefinedOrderedMap) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return ordmap.UnmarshalJSONFrom(om, dec, opts, setIndex)
}
func TestUnmarshalJSONFrom(t *testing.T) {
t.Parallel()
const want = `{"foo":{"foo":"foo","bar":1},"bar":{"foo":"foo","bar":1},"baz":{"foo":"foo","bar":1},"qux":{"foo":"","bar":0},"moo":{"foo":"","bar":0},"one":{"foo":"","bar":0},"two":{"foo":"","bar":0},"three":{"foo":"","bar":0}}`
for _, tc := range []struct {
name string
om any
}{
{"user defined ordered map", UserDefinedOrderedMap{}},
{"ordered map", OrderedMap{}},
{"ordered map with pointer value", OrderedMapPointer{}},
} {
t.Run(tc.name, func(t *testing.T) {
if err := json.Unmarshal([]byte(want), &tc.om); err != nil {
t.Fatal(err)
}
got, err := json.Marshal(tc.om)
if err != nil {
t.Fatal(err)
}
if string(got) != want {
t.Fatalf("got: %v, want: %v", string(got), want)
}
})
}
}
func TestUnmarshal_Errors(t *testing.T) {
t.Parallel()
for _, testType := range []struct {
name string
tp reflect.Type
val reflect.Type
}{
{
"user defined ordered map",
reflect.TypeFor[UserDefinedOrderedMap](), reflect.TypeFor[ValueWithIndex](),
},
{
"ordered map",
reflect.TypeFor[OrderedMap](), reflect.TypeFor[Value](),
},
{
"ordered map with pointer value",
reflect.TypeFor[OrderedMapPointer](), reflect.TypeFor[Value](),
},
} {
t.Run(testType.name, func(t *testing.T) {
t.Run("invalid key", func(t *testing.T) {
err := json.Unmarshal([]byte(`{1}`), reflect.New(testType.tp).Interface())
synErr := errAs[jsontext.SyntacticError](t, err)
if synErr.JSONPointer != "" {
t.Fatalf("got: %v, want: %v", synErr.JSONPointer, "")
}
if synErr.Err == nil {
t.Fatal("expected error")
} else if want := `object member name must be a string`; synErr.Err.Error() != want {
t.Fatalf("got: %q, want: %v", synErr.Err, want)
}
})
for _, tc := range []struct {
name string
data string
err string
}{
{"empty", ``, `EOF`},
{"string instead of object", `""`, `expected {, got string`},
{
"missing string for object name", `{"foo":{"foo":"foo","bar":1`,
`["foo"]: jsontext: unexpected EOF within "/foo" after offset 27`,
},
} {
t.Run(tc.name, func(t *testing.T) {
err := json.Unmarshal([]byte(tc.data), reflect.New(testType.tp).Interface())
semErr := errAs[json.SemanticError](t, err)
if semErr.GoType != testType.tp {
t.Fatalf("got: %v, want: %v", semErr.GoType, testType.tp)
}
if semErr.Err == nil {
t.Fatal("expected error")
} else if semErr.Err.Error() != tc.err {
t.Fatalf("got: %q, want: %q", err, tc.err)
}
})
}
t.Run("missing string for object name", func(t *testing.T) {
err := json.Unmarshal([]byte(`{"foo":1}`), reflect.New(testType.tp).Interface())
semErr := errAs[json.SemanticError](t, err)
if semErr.GoType != testType.tp {
t.Fatalf("got: %v, want: %v", semErr.GoType, testType.tp)
}
semErr = errAs[json.SemanticError](t, semErr.Err)
if semErr.GoType != testType.val {
t.Fatalf("got: %v, want: %v", semErr.GoType, testType.val)
}
if semErr.Err != nil {
t.Fatal(err)
}
})
})
}
}