57 lines
952 B
Go
57 lines
952 B
Go
package yamlutil
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLoadYamlFile(t *testing.T) {
|
|
type structA struct {
|
|
A int
|
|
B string
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
body string
|
|
want any
|
|
}{
|
|
{
|
|
name: "empty body",
|
|
body: "",
|
|
want: structA{},
|
|
},
|
|
{
|
|
name: "all comments",
|
|
body: `# foo
|
|
# bar`,
|
|
want: structA{},
|
|
},
|
|
{
|
|
name: "success",
|
|
body: "a: 1\nb: foo\n",
|
|
want: structA{A: 1, B: "foo"},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var (
|
|
dir = t.TempDir()
|
|
path = filepath.Join(dir, "f.yaml")
|
|
intoV = reflect.New(reflect.TypeOf(test.want))
|
|
into = intoV.Interface()
|
|
)
|
|
|
|
require.NoError(t, os.WriteFile(path, []byte(test.body), 0444))
|
|
assert.NoError(t, LoadYamlFile(into, path))
|
|
assert.Equal(t, test.want, intoV.Elem().Interface())
|
|
})
|
|
}
|
|
}
|