diff --git a/test/gtest/gtest_util.go b/test/gtest/gtest_util.go index 49ec4b26911..c9ccb9f0b49 100644 --- a/test/gtest/gtest_util.go +++ b/test/gtest/gtest_util.go @@ -315,35 +315,44 @@ func compareMap(value, expect interface{}) error { rvValue = reflect.ValueOf(value) rvExpect = reflect.ValueOf(expect) ) - if rvExpect.Kind() == reflect.Map { - if rvValue.Kind() == reflect.Map { - if rvExpect.Len() == rvValue.Len() { - // Turn two interface maps to the same type for comparison. - // Direct use of rvValue.MapIndex(key).Interface() will panic - // when the key types are inconsistent. - mValue := make(map[string]string) - mExpect := make(map[string]string) - ksValue := rvValue.MapKeys() - ksExpect := rvExpect.MapKeys() - for _, key := range ksValue { - mValue[gconv.String(key.Interface())] = gconv.String(rvValue.MapIndex(key).Interface()) - } - for _, key := range ksExpect { - mExpect[gconv.String(key.Interface())] = gconv.String(rvExpect.MapIndex(key).Interface()) - } - for k, v := range mExpect { - if v != mValue[k] { - return fmt.Errorf(`[ASSERT] EXPECT VALUE map["%v"]:%v == map["%v"]:%v`+ - "\nGIVEN : %v\nEXPECT: %v", k, mValue[k], k, v, mValue, mExpect) - } - } - } else { - return fmt.Errorf(`[ASSERT] EXPECT MAP LENGTH %d == %d`, rvValue.Len(), rvExpect.Len()) - } - } else { - return fmt.Errorf(`[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "%s"`, rvValue.Kind()) + + if rvExpect.Kind() != reflect.Map { + return nil + } + + if rvValue.Kind() != reflect.Map { + return fmt.Errorf(`[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "%s"`, rvValue.Kind()) + } + + if rvExpect.Len() != rvValue.Len() { + return fmt.Errorf(`[ASSERT] EXPECT MAP LENGTH %d == %d`, rvValue.Len(), rvExpect.Len()) + } + + // Turn two interface maps to the same type for comparison. + // Direct use of rvValue.MapIndex(key).Interface() will panic + // when the key types are inconsistent. + var ( + mValue = make(map[string]string) + mExpect = make(map[string]string) + ksValue = rvValue.MapKeys() + ksExpect = rvExpect.MapKeys() + ) + + for _, key := range ksValue { + mValue[gconv.String(key.Interface())] = gconv.String(rvValue.MapIndex(key).Interface()) + } + + for _, key := range ksExpect { + mExpect[gconv.String(key.Interface())] = gconv.String(rvExpect.MapIndex(key).Interface()) + } + + for k, v := range mExpect { + if v != mValue[k] { + return fmt.Errorf(`[ASSERT] EXPECT VALUE map["%v"]:%v == map["%v"]:%v`+ + "\nGIVEN : %v\nEXPECT: %v", k, mValue[k], k, v, mValue, mExpect) } } + return nil } @@ -364,9 +373,9 @@ func AssertNil(value interface{}) { // which will be joined with current system separator and returned with the path. func DataPath(names ...string) string { _, path, _ := gdebug.CallerWithFilter([]string{pathFilterKey}) - path = filepath.Dir(path) + "/testdata" + path = filepath.Join(filepath.Dir(path), "testdata") for _, name := range names { - path += "/" + name + path = filepath.Join(path, name) } return filepath.FromSlash(path) } diff --git a/test/gtest/gtest_z_unit_test.go b/test/gtest/gtest_z_unit_test.go index 06872a35212..2c1197f22e6 100644 --- a/test/gtest/gtest_z_unit_test.go +++ b/test/gtest/gtest_z_unit_test.go @@ -399,7 +399,7 @@ func TestAssertError(t *testing.T) { func TestDataPath(t *testing.T) { gtest.C(t, func(t *gtest.T) { - t.Assert(filepath.ToSlash(gtest.DataPath("testdata.txt")), `./testdata/testdata.txt`) + t.Assert(filepath.ToSlash(gtest.DataPath("testdata.txt")), `testdata/testdata.txt`) }) }