gosocketio/json/json_test.go

115 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-11-25 22:27:57 +08:00
package json
import (
"fmt"
"reflect"
"testing"
)
func TestJSONObject_UnmarshalJSON(t *testing.T) {
type fields struct {
m map[string]interface{}
}
type args struct {
data []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "TestUnmarshalJsonObject",
fields: fields{m: make(map[string]interface{})},
args: args{
data: []byte(`{"name":"test","age":10, "class":["a","b"]}`),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &JSONObject{
m: tt.fields.m,
}
if err := j.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("JSONObject.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
fmt.Println(j.m)
})
}
}
func TestJSONObject_getBySlice(t *testing.T) {
type fields struct {
m map[string]interface{}
}
type args struct {
path []string
}
tests := []struct {
name string
fields fields
args args
want interface{}
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &JSONObject{
m: tt.fields.m,
}
if got := j.getBySlice(tt.args.path); !reflect.DeepEqual(got, tt.want) {
t.Errorf("JSONObject.getBySlice() = %v, want %v", got, tt.want)
}
})
}
}
func TestJSONObject_Get(t *testing.T) {
type fields struct {
m map[string]interface{}
}
type args struct {
path string
}
tests := []struct {
name string
fields fields
args args
want interface{}
}{
{
name: "can get direct property",
fields: fields{m: map[string]interface{}{"m": 1}},
args: args{path: "m"},
want: 1,
},
{
name: "can get object property by path",
fields: fields{m: map[string]interface{}{"m": map[string]interface{}{"x": "a"}}},
args: args{path: "m.x"},
want: "a",
},
{
name: "can get array property with index",
fields: fields{m: map[string]interface{}{"m": map[string]interface{}{
"data": []interface{}{"a", "b", "c"},
}}},
args: args{path: "m.0"},
want: "a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &JSONObject{
m: tt.fields.m,
}
if got := j.Get(tt.args.path); !reflect.DeepEqual(got, tt.want) {
t.Errorf("JSONObject.Get() = %v, want %v", got, tt.want)
}
})
}
}