1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
package dencoding_test
import (
"bytes"
"github.com/tomwright/dasel/v2/dencoding"
"io"
"reflect"
"testing"
)
func TestJSONDecoder_Decode(t *testing.T) {
b := []byte(`{"x":1,"a":"hello"}{"x":2,"a":"there"}{"a":"Tom","x":3}`)
dec := dencoding.NewJSONDecoder(bytes.NewReader(b))
maps := make([]any, 0)
for {
var v any
if err := dec.Decode(&v); err != nil {
if err == io.EOF {
break
}
t.Errorf("unexpected error: %v", err)
return
}
maps = append(maps, v)
}
exp := [][]dencoding.KeyValue{
{
{Key: "x", Value: int64(1)},
{Key: "a", Value: "hello"},
},
{
{Key: "x", Value: int64(2)},
{Key: "a", Value: "there"},
},
{
{Key: "a", Value: "Tom"},
{Key: "x", Value: int64(3)},
},
}
got := make([][]dencoding.KeyValue, 0)
for _, v := range maps {
if m, ok := v.(*dencoding.Map); ok {
got = append(got, m.KeyValues())
}
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
}
}
|