github.com/annwntech/go-micro/v2@v2.9.5/config/reader/json/values_test.go (about) 1 package json 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/annwntech/go-micro/v2/config/source" 8 ) 9 10 func TestValues(t *testing.T) { 11 emptyStr := "" 12 testData := []struct { 13 csdata []byte 14 path []string 15 accepter interface{} 16 value interface{} 17 }{ 18 { 19 []byte(`{"foo": "bar", "baz": {"bar": "cat"}}`), 20 []string{"foo"}, 21 emptyStr, 22 "bar", 23 }, 24 { 25 []byte(`{"foo": "bar", "baz": {"bar": "cat"}}`), 26 []string{"baz", "bar"}, 27 emptyStr, 28 "cat", 29 }, 30 } 31 32 for idx, test := range testData { 33 values, err := newValues(&source.ChangeSet{ 34 Data: test.csdata, 35 }) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 err = values.Get(test.path...).Scan(&test.accepter) 41 if err != nil { 42 t.Fatal(err) 43 } 44 if test.accepter != test.value { 45 t.Fatalf("No.%d Expected %v got %v for path %v", idx, test.value, test.accepter, test.path) 46 } 47 } 48 } 49 50 func TestStructArray(t *testing.T) { 51 type T struct { 52 Foo string 53 } 54 55 emptyTSlice := []T{} 56 57 testData := []struct { 58 csdata []byte 59 accepter []T 60 value []T 61 }{ 62 { 63 []byte(`[{"foo": "bar"}]`), 64 emptyTSlice, 65 []T{{Foo: "bar"}}, 66 }, 67 } 68 69 for idx, test := range testData { 70 values, err := newValues(&source.ChangeSet{ 71 Data: test.csdata, 72 }) 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 err = values.Get().Scan(&test.accepter) 78 if err != nil { 79 t.Fatal(err) 80 } 81 if !reflect.DeepEqual(test.accepter, test.value) { 82 t.Fatalf("No.%d Expected %v got %v", idx, test.value, test.accepter) 83 } 84 } 85 }