From d3e56f2eca5effff2aa4ab496e88b36b6471c0f7 Mon Sep 17 00:00:00 2001 From: Reza Behzadan Date: Sun, 18 Feb 2024 06:56:01 +0330 Subject: [PATCH] Use more eloquent way to load test cases --- internal/dict/dict_merge_test.go | 12 ++++++++---- internal/dict/testutils_test.go | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/dict/dict_merge_test.go b/internal/dict/dict_merge_test.go index be947b7..a0a9049 100644 --- a/internal/dict/dict_merge_test.go +++ b/internal/dict/dict_merge_test.go @@ -6,7 +6,8 @@ import ( ) 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 { SoftMerge(&testCase.Dict1, testCase.Dict2) if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { @@ -16,7 +17,8 @@ func TestSoftMerge(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 { testCase.Dict1.SoftMergeWith(testCase.Dict2) if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { @@ -26,7 +28,8 @@ func TestSoftMergeWith(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 { Merge(&testCase.Dict1, testCase.Dict2) if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { @@ -36,7 +39,8 @@ func TestMerge(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 { testCase.Dict1.MergeWith(testCase.Dict2) if !reflect.DeepEqual(testCase.Dict1, testCase.Result) { diff --git a/internal/dict/testutils_test.go b/internal/dict/testutils_test.go index 96845b7..496fb5a 100644 --- a/internal/dict/testutils_test.go +++ b/internal/dict/testutils_test.go @@ -14,18 +14,22 @@ type dictDictToDictTestCase struct { 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) if err != nil { panic(err) } - var testCases []dictDictToDictTestCase - err = yaml.Unmarshal(content, &testCases) + err = yaml.Unmarshal(content, out) if err != nil { panic(err) } - return testCases } func sampleDict() Dict {