shad-go/jsonlist/jsonlist_test.go

63 lines
1.3 KiB
Go
Raw Normal View History

2020-04-15 20:52:59 +00:00
package jsonlist
import (
"bytes"
2020-04-16 12:14:19 +00:00
"encoding/json"
2020-04-15 20:52:59 +00:00
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
type S struct {
A string
B int
C interface{}
}
func TestJsonList(t *testing.T) {
for _, test := range []struct {
js string
value interface{}
}{
{
js: `1 2 3`,
value: []int{1, 2, 3},
},
{
js: `"A" "B" "C"`,
value: []string{"A", "B", "C"},
},
{
js: `{"A": "A"} {"B": 2} {"C": 0.5}`,
value: []S{{A: "A"}, {B: 2}, {C: 0.5}},
},
{
js: `"A" 2`,
value: []interface{}{"A", 2.0},
},
} {
t.Run(test.js, func(t *testing.T) {
emptySlice := reflect.New(reflect.TypeOf(test.value))
require.NoError(t, Unmarshal(bytes.NewBufferString(test.js), emptySlice.Interface()))
require.Equal(t, test.value, emptySlice.Elem().Interface())
var buf bytes.Buffer
require.NoError(t, Marshal(&buf, test.value))
emptySlice = reflect.New(reflect.TypeOf(test.value))
2020-04-16 12:14:19 +00:00
require.NoError(t, Unmarshal(&buf, emptySlice.Interface()))
2020-04-15 20:52:59 +00:00
require.Equal(t, test.value, emptySlice.Elem().Interface())
})
}
}
2020-04-16 12:14:19 +00:00
func TestErrors(t *testing.T) {
var buf bytes.Buffer
require.Equal(t, &json.UnsupportedTypeError{Type: reflect.TypeOf([]int{})}, Unmarshal(&buf, []int{}))
require.Equal(t, &json.UnsupportedTypeError{Type: reflect.TypeOf(1)}, Marshal(&buf, 1))
}