Skip to content

Commit

Permalink
style(gtest/test): improve code (#3891)
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git authored Nov 12, 2024
1 parent a63af5d commit 070efec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
67 changes: 38 additions & 29 deletions test/gtest/gtest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion test/gtest/gtest_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
}

Expand Down

0 comments on commit 070efec

Please sign in to comment.