micro-codec-json/codec_test.go
Vasiliy Tolstov cddee4ddb0 update to latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-22 02:05:09 +03:00

45 lines
742 B
Go

package json
import (
"bytes"
"testing"
)
func TestStructByTag(t *testing.T) {
type Str struct {
Name []string `json:"name" codec:"flatten"`
}
val := &Str{Name: []string{"first", "second"}}
c := NewCodec()
buf, err := c.Marshal(val)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte(`["first","second"]`)) {
t.Fatalf("invalid marshal: %v != %v", buf, []byte(`["first","second"]`))
}
err = c.Unmarshal([]byte(`["1","2"]`), val)
if err != nil {
t.Fatal(err)
}
if len(val.Name) != 2 {
t.Fatalf("invalid unmarshal: %v", val)
}
}
func TestReadBody(t *testing.T) {
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
if err != nil {
t.Fatal(err)
}
}