-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
80 lines (78 loc) · 1.69 KB
/
json_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
package function
import (
"reflect"
"testing"
)
func Test_unmarshalJSONFunctionArgs(t *testing.T) {
description := func(f any) Description {
info, err := ReflectDescription("f", f)
if err != nil {
panic(err)
}
return info
}
type args struct {
f Description
jsonObject []byte
}
tests := []struct {
name string
args args
wantArgs []any
wantErr bool
}{
{
name: "empty",
args: args{
f: description(func() {}),
jsonObject: []byte(`{"a0": "ignored"}`),
},
wantArgs: []any{},
},
{
name: "default 0",
args: args{
f: description(func(string, int) {}),
jsonObject: []byte(`{"a0": "default"}`),
},
wantArgs: []any{"default", 0},
},
{
name: "hello 666",
args: args{
f: description(func(string, int) {}),
jsonObject: []byte(`{"a0": "hello", "a1": 666, "a2": "ignored"}`),
},
wantArgs: []any{"hello", 666},
},
{
name: "ptr",
args: args{
f: description(func(*string, *string, any) {}),
jsonObject: []byte(`{"a0": "", "a2": null}`),
},
wantArgs: []any{new(string), (*string)(nil), nil},
},
// wantErr
{
name: "JSON array",
args: args{
f: description(func() {}),
jsonObject: []byte(`[]`),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotArgs, err := unmarshalJSONFunctionArgs(tt.args.f, tt.args.jsonObject)
if (err != nil) != tt.wantErr {
t.Errorf("unmarshalJSONFunctionArgs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
t.Errorf("unmarshalJSONFunctionArgs() = %v, want %v", gotArgs, tt.wantArgs)
}
})
}
}