formidable/internal/merge/merge_test.go
William Petit f4b3d8f532 feat: aggregate defaults and values
- Output merged defaults and values
- Add "check" command
2022-09-27 22:23:18 +02:00

70 lines
1.3 KiB
Go

package merge
import (
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
type mergeTestCase struct {
Name string
Dst interface{}
Dflts interface{}
ExpectedResult interface{}
ShouldFail bool
}
var mergeTestCases = []mergeTestCase{
{
Name: "simple-maps",
Dst: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "test",
},
},
Dflts: map[string]interface{}{
"other": true,
"foo": map[string]interface{}{
"bar": true,
"baz": 1,
},
},
ExpectedResult: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "test",
"baz": 1,
},
"other": true,
},
},
{
Name: "string-slices",
Dst: []string{"foo"},
Dflts: []string{"bar"},
ExpectedResult: []string{"foo"},
},
}
func TestMerge(t *testing.T) {
t.Parallel()
for _, tc := range mergeTestCases {
func(tc *mergeTestCase) {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
err := Merge(&tc.Dst, tc.Dflts)
if tc.ShouldFail && err == nil {
t.Error("merge should have failed")
}
if !reflect.DeepEqual(tc.Dst, tc.ExpectedResult) {
t.Errorf("tc.Dst should have been the same as tc.ExpectedResult. Expected: %s, got %s", spew.Sdump(tc.ExpectedResult), spew.Sdump(tc.Dst))
}
})
}(&tc)
}
}