test(render): improve test coverage

This commit is contained in:
2024-03-17 23:07:12 +00:00
parent 2ad77f0b1b
commit 7df40b1578
12 changed files with 648 additions and 62 deletions

View File

@@ -2,19 +2,33 @@ package render_test
import (
"bytes"
"errors"
"testing"
"github.com/jimeh/go-render"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
type mockYAMLMarshaler struct {
val any
err error
}
var _ yaml.Marshaler = (*mockYAMLMarshaler)(nil)
func (m *mockYAMLMarshaler) MarshalYAML() (any, error) {
return m.val, m.err
}
func TestYAML_Render(t *testing.T) {
tests := []struct {
name string
indent int
value interface{}
want string
wantErr string
wantErrIs []error
wantPanic string
}{
{
@@ -44,6 +58,17 @@ func TestYAML_Render(t *testing.T) {
},
want: "user:\n age: 30\n name: John Doe\n",
},
{
name: "implements yaml.Marshaler",
value: &mockYAMLMarshaler{val: map[string]int{"age": 30}},
want: "age: 30\n",
},
{
name: "error from yaml.Marshaler",
value: &mockYAMLMarshaler{err: errors.New("mock error")},
wantErr: "render: mock error",
wantErrIs: []error{render.Err},
},
{
name: "invalid value",
indent: 0,
@@ -51,7 +76,6 @@ func TestYAML_Render(t *testing.T) {
wantPanic: "cannot marshal type: chan int",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &render.YAML{
@@ -70,11 +94,21 @@ func TestYAML_Render(t *testing.T) {
err = j.Render(&buf, tt.value)
}()
got := buf.String()
if tt.wantPanic != "" {
assert.Equal(t, tt.wantPanic, panicRes)
} else {
require.NoError(t, err)
got := buf.String()
}
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
}
for _, e := range tt.wantErrIs {
assert.ErrorIs(t, err, e)
}
if tt.wantPanic == "" &&
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})