Use more eloquent way to load test cases

This commit is contained in:
Reza Behzadan 2024-02-18 06:56:01 +03:30
parent d2aba08773
commit d3e56f2eca
2 changed files with 16 additions and 8 deletions

View File

@ -6,7 +6,8 @@ import (
) )
func TestSoftMerge(t *testing.T) { func TestSoftMerge(t *testing.T) {
testCases := loadDictDictToDictTestCases("test_data/soft_merge_cases.yaml") testCases := []dictDictToDictTestCase{}
loadYamlFile("test_data/soft_merge_cases.yaml", &testCases)
for _, testCase := range testCases { for _, testCase := range testCases {
SoftMerge(&testCase.Dict1, testCase.Dict2) SoftMerge(&testCase.Dict1, testCase.Dict2)
if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { if !reflect.DeepEqual(testCase.Dict1, testCase.Result) {
@ -16,7 +17,8 @@ func TestSoftMerge(t *testing.T) {
} }
func TestSoftMergeWith(t *testing.T) { func TestSoftMergeWith(t *testing.T) {
testCases := loadDictDictToDictTestCases("test_data/soft_merge_cases.yaml") testCases := []dictDictToDictTestCase{}
loadYamlFile("test_data/soft_merge_cases.yaml", &testCases)
for _, testCase := range testCases { for _, testCase := range testCases {
testCase.Dict1.SoftMergeWith(testCase.Dict2) testCase.Dict1.SoftMergeWith(testCase.Dict2)
if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { if !reflect.DeepEqual(testCase.Dict1, testCase.Result) {
@ -26,7 +28,8 @@ func TestSoftMergeWith(t *testing.T) {
} }
func TestMerge(t *testing.T) { func TestMerge(t *testing.T) {
testCases := loadDictDictToDictTestCases("test_data/merge_cases.yaml") testCases := []dictDictToDictTestCase{}
loadYamlFile("test_data/merge_cases.yaml", &testCases)
for _, testCase := range testCases { for _, testCase := range testCases {
Merge(&testCase.Dict1, testCase.Dict2) Merge(&testCase.Dict1, testCase.Dict2)
if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { if !reflect.DeepEqual(testCase.Dict1, testCase.Result) {
@ -36,7 +39,8 @@ func TestMerge(t *testing.T) {
} }
func TestMergeWith(t *testing.T) { func TestMergeWith(t *testing.T) {
testCases := loadDictDictToDictTestCases("test_data/merge_cases.yaml") testCases := []dictDictToDictTestCase{}
loadYamlFile("test_data/merge_cases.yaml", &testCases)
for _, testCase := range testCases { for _, testCase := range testCases {
testCase.Dict1.MergeWith(testCase.Dict2) testCase.Dict1.MergeWith(testCase.Dict2)
if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { if !reflect.DeepEqual(testCase.Dict1, testCase.Result) {

View File

@ -14,18 +14,22 @@ type dictDictToDictTestCase struct {
Result Dict `yaml:"result"` Result Dict `yaml:"result"`
} }
func loadDictDictToDictTestCases(fn string) []dictDictToDictTestCase { type dictStrToDictTestCase struct {
Arg1 Dict `yaml:"arg1"`
Arg2 string `yaml:"arg2"`
Result Dict `yaml:"result"`
}
func loadYamlFile(fn string, out interface{}) {
content, err := os.ReadFile(fn) content, err := os.ReadFile(fn)
if err != nil { if err != nil {
panic(err) panic(err)
} }
var testCases []dictDictToDictTestCase err = yaml.Unmarshal(content, out)
err = yaml.Unmarshal(content, &testCases)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return testCases
} }
func sampleDict() Dict { func sampleDict() Dict {