2019-05-31 01:11:13 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2019-06-20 19:25:39 +03:00
|
|
|
"reflect"
|
2019-05-31 01:11:13 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/config/reader"
|
|
|
|
"github.com/micro/go-micro/v3/config/source"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValues(t *testing.T) {
|
2019-06-20 19:25:39 +03:00
|
|
|
emptyStr := ""
|
2019-05-31 01:11:13 +03:00
|
|
|
testData := []struct {
|
2019-06-20 19:25:39 +03:00
|
|
|
csdata []byte
|
|
|
|
path []string
|
|
|
|
accepter interface{}
|
|
|
|
value interface{}
|
2019-05-31 01:11:13 +03:00
|
|
|
}{
|
|
|
|
{
|
2019-06-20 19:25:39 +03:00
|
|
|
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
|
2019-05-31 01:11:13 +03:00
|
|
|
[]string{"foo"},
|
2019-06-20 19:25:39 +03:00
|
|
|
emptyStr,
|
2019-05-31 01:11:13 +03:00
|
|
|
"bar",
|
|
|
|
},
|
|
|
|
{
|
2019-06-20 19:25:39 +03:00
|
|
|
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
|
2019-05-31 01:11:13 +03:00
|
|
|
[]string{"baz", "bar"},
|
2019-06-20 19:25:39 +03:00
|
|
|
emptyStr,
|
2019-05-31 01:11:13 +03:00
|
|
|
"cat",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:25:39 +03:00
|
|
|
for idx, test := range testData {
|
|
|
|
values, err := newValues(&source.ChangeSet{
|
|
|
|
Data: test.csdata,
|
2020-07-06 22:13:35 +03:00
|
|
|
}, reader.Options{})
|
2019-06-20 19:25:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2019-06-20 19:25:39 +03: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-31 01:11:13 +03:00
|
|
|
}
|
2019-06-20 19:25:39 +03:00
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2019-06-20 19:25:39 +03: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 18:07:53 +03:00
|
|
|
[]T{{Foo: "bar"}},
|
2019-06-20 19:25:39 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, test := range testData {
|
|
|
|
values, err := newValues(&source.ChangeSet{
|
|
|
|
Data: test.csdata,
|
2020-07-06 22:13:35 +03:00
|
|
|
}, reader.Options{})
|
2019-06-20 19:25:39 +03:00
|
|
|
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-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|