Add unit test for utils.MarshalIntoFormat

pull/444/head
Tony-Sol 1 year ago
parent c6a0138083
commit 8ab751ef32
No known key found for this signature in database
GPG Key ID: 0B27B02E462B2C67

@ -257,3 +257,65 @@ func TestRenderTable(t *testing.T) {
}
}
}
func TestMarshalIntoFormat(t *testing.T) {
type innerData struct {
Foo int `json:"foo"`
Bar string `json:"bar"`
Baz bool `json:"baz"`
}
type data struct {
Qux int `json:"quz"`
Quux innerData `json:"quux"`
}
type scenario struct {
input data
format string
expected []byte
expectedErr error
}
scenarios := []scenario{
{
input: data{1, innerData{2, "foo", true}},
format: "json",
expected: []byte(`{
"quz": 1,
"quux": {
"foo": 2,
"bar": "foo",
"baz": true
}
}`),
expectedErr: nil,
},
{
input: data{1, innerData{2, "foo", true}},
format: "yaml",
expected: []byte(`quz: 1
quux:
bar: foo
baz: true
foo: 2
`),
expectedErr: nil,
},
{
input: data{1, innerData{2, "foo", true}},
format: "xml",
expected: nil,
expectedErr: errors.New("Unsupported detailization format: xml"),
},
}
for _, s := range scenarios {
output, err := MarshalIntoFormat(s.input, s.format)
assert.EqualValues(t, s.expected, output)
if s.expectedErr != nil {
assert.EqualError(t, err, s.expectedErr.Error())
} else {
assert.NoError(t, err)
}
}
}

Loading…
Cancel
Save