-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathyaml_test.go
74 lines (65 loc) · 1.29 KB
/
yaml_test.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: Apache-2.0
package output
import (
"errors"
"testing"
)
func TestOutput_YAML(t *testing.T) {
// setup tests
tests := []struct {
failure bool
input interface{}
}{
{
failure: false,
input: "hello",
},
{ // map
failure: false,
input: map[string]string{"hello": "world"},
},
{ // slice
failure: false,
input: []interface{}{1, 2, 3},
},
{ // slice complex
failure: false,
input: []interface{}{struct{ Foo string }{Foo: "bar"}},
},
{ // complex
failure: false,
input: []struct{ Foo string }{{"bar"}, {"baz"}},
},
{
failure: true,
input: new(failMarshaler),
},
{
failure: true,
input: nil,
},
{
failure: true,
input: "",
},
}
// run tests
for _, test := range tests {
err := YAML(test.input)
if test.failure {
if err == nil {
t.Errorf("YAML should have returned err")
}
continue
}
if err != nil {
t.Errorf("YAML returned err: %v", err)
}
}
}
func (f *failMarshaler) MarshalYAML() (interface{}, error) {
return nil, errors.New("this is a marshaler that fails when you try to marshal")
}
func (f *failMarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
return errors.New("this is a marshaler that fails when you try to unmarshal")
}