2019-05-30 23:11:13 +01:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2019-06-21 00:25:39 +08:00
|
|
|
"reflect"
|
2019-05-30 23:11:13 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/config/source"
|
2019-05-30 23:11:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValues(t *testing.T) {
|
2019-06-21 00:25:39 +08:00
|
|
|
emptyStr := ""
|
2019-05-30 23:11:13 +01:00
|
|
|
testData := []struct {
|
2019-06-21 00:25:39 +08:00
|
|
|
csdata []byte
|
|
|
|
path []string
|
|
|
|
accepter interface{}
|
|
|
|
value interface{}
|
2019-05-30 23:11:13 +01:00
|
|
|
}{
|
|
|
|
{
|
2019-06-21 00:25:39 +08:00
|
|
|
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
|
2019-05-30 23:11:13 +01:00
|
|
|
[]string{"foo"},
|
2019-06-21 00:25:39 +08:00
|
|
|
emptyStr,
|
2019-05-30 23:11:13 +01:00
|
|
|
"bar",
|
|
|
|
},
|
|
|
|
{
|
2019-06-21 00:25:39 +08:00
|
|
|
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
|
2019-05-30 23:11:13 +01:00
|
|
|
[]string{"baz", "bar"},
|
2019-06-21 00:25:39 +08:00
|
|
|
emptyStr,
|
2019-05-30 23:11:13 +01:00
|
|
|
"cat",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:25:39 +08:00
|
|
|
for idx, test := range testData {
|
|
|
|
values, err := newValues(&source.ChangeSet{
|
|
|
|
Data: test.csdata,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
|
2019-06-21 00:25:39 +08:00
|
|
|
err = values.Get(test.path...).Scan(&test.accepter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if test.accepter != test.value {
|
|
|
|
t.Fatalf("No.%d Expected %v got %v for path %v", idx, test.value, test.accepter, test.path)
|
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
2019-06-21 00:25:39 +08:00
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
|
2019-06-21 00:25:39 +08:00
|
|
|
func TestStructArray(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyTSlice := []T{}
|
|
|
|
|
|
|
|
testData := []struct {
|
|
|
|
csdata []byte
|
|
|
|
accepter []T
|
|
|
|
value []T
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]byte(`[{"foo": "bar"}]`),
|
|
|
|
emptyTSlice,
|
2019-11-01 15:07:53 +00:00
|
|
|
[]T{{Foo: "bar"}},
|
2019-06-21 00:25:39 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, test := range testData {
|
|
|
|
values, err := newValues(&source.ChangeSet{
|
|
|
|
Data: test.csdata,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = values.Get().Scan(&test.accepter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(test.accepter, test.value) {
|
|
|
|
t.Fatalf("No.%d Expected %v got %v", idx, test.value, test.accepter)
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|