go-yaml/yaml_test.go

78 lines
1.1 KiB
Go
Raw Normal View History

2019-10-18 13:05:36 +09:00
package yaml_test
import (
"testing"
"github.com/goccy/go-yaml"
"golang.org/x/xerrors"
2019-10-18 13:05:36 +09:00
)
2019-10-19 18:28:36 +09:00
func TestMarshal(t *testing.T) {
var v struct {
A int
B string
}
v.A = 1
v.B = "hello"
bytes, err := yaml.Marshal(v)
if err != nil {
t.Fatalf("%+v", err)
}
if string(bytes) != "a: 1\nb: hello\n" {
t.Fatal("failed to marshal")
}
}
2019-10-18 13:05:36 +09:00
func TestUnmarshal(t *testing.T) {
yml := `
%YAML 1.2
---
a: 1
b: c
`
var v struct {
A int
B string
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
t.Fatalf("%+v", err)
}
}
type marshalTest struct{}
func (t *marshalTest) MarshalYAML() (interface{}, error) {
return yaml.MapSlice{
{
"a", 1,
},
{
"b", "hello",
},
{
"c", true,
},
}, nil
}
func TestMarshalYAML(t *testing.T) {
var v struct {
A *marshalTest
}
v.A = &marshalTest{}
bytes, err := yaml.Marshal(v)
if err != nil {
t.Fatalf("failed to Marshal: %+v", err)
}
expect := `
a:
a: 1
b: hello
c: true
`
actual := "\n" + string(bytes)
if expect != actual {
t.Fatalf("failed to MarshalYAML expect:[%s], actual:[%s]", expect, actual)
}
}