micro/config/reader/json/values_test.go

86 lines
1.4 KiB
Go
Raw Normal View History

2019-05-30 23:11:13 +01:00
package json
import (
"reflect"
2019-05-30 23:11:13 +01:00
"testing"
"github.com/micro/go-micro/v2/config/source"
2019-05-30 23:11:13 +01:00
)
func TestValues(t *testing.T) {
emptyStr := ""
2019-05-30 23:11:13 +01:00
testData := []struct {
csdata []byte
path []string
accepter interface{}
value interface{}
2019-05-30 23:11:13 +01:00
}{
{
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
2019-05-30 23:11:13 +01:00
[]string{"foo"},
emptyStr,
2019-05-30 23:11:13 +01:00
"bar",
},
{
[]byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
2019-05-30 23:11:13 +01:00
[]string{"baz", "bar"},
emptyStr,
2019-05-30 23:11:13 +01:00
"cat",
},
}
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
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-05-30 23:11:13 +01: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,
[]T{{Foo: "bar"}},
},
}
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
}
}
}