MCPcopy
hub / github.com/gogf/gf / compareMap

Function compareMap

test/gtest/gtest_util.go:327–371  ·  view source on GitHub ↗

compareMap compares two maps, returns nil if they are equal, or else returns error.

(value, expect any)

Source from the content-addressed store, hash-verified

325
326// compareMap compares two maps, returns nil if they are equal, or else returns error.
327func compareMap(value, expect any) error {
328 var (
329 rvValue = reflect.ValueOf(value)
330 rvExpect = reflect.ValueOf(expect)
331 )
332
333 if rvExpect.Kind() != reflect.Map {
334 return nil
335 }
336
337 if rvValue.Kind() != reflect.Map {
338 return fmt.Errorf(`[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "%s"`, rvValue.Kind())
339 }
340
341 if rvExpect.Len() != rvValue.Len() {
342 return fmt.Errorf(`[ASSERT] EXPECT MAP LENGTH %d == %d`, rvValue.Len(), rvExpect.Len())
343 }
344
345 // Turn two interface maps to the same type for comparison.
346 // Direct use of rvValue.MapIndex(key).Interface() will panic
347 // when the key types are inconsistent.
348 var (
349 mValue = make(map[string]string)
350 mExpect = make(map[string]string)
351 ksValue = rvValue.MapKeys()
352 ksExpect = rvExpect.MapKeys()
353 )
354
355 for _, key := range ksValue {
356 mValue[gconv.String(key.Interface())] = gconv.String(rvValue.MapIndex(key).Interface())
357 }
358
359 for _, key := range ksExpect {
360 mExpect[gconv.String(key.Interface())] = gconv.String(rvExpect.MapIndex(key).Interface())
361 }
362
363 for k, v := range mExpect {
364 if v != mValue[k] {
365 return fmt.Errorf(`[ASSERT] EXPECT VALUE map["%v"]:%v == map["%v"]:%v`+
366 "\nGIVEN : %v\nEXPECT: %v", k, mValue[k], k, v, mValue, mExpect)
367 }
368 }
369
370 return nil
371}
372
373// AssertNil asserts `value` is nil.
374func AssertNil(value any) {

Callers 3

AssertFunction · 0.85
AssertEQFunction · 0.85
AssertNEFunction · 0.85

Calls 5

StringFunction · 0.92
KindMethod · 0.80
ErrorfMethod · 0.65
LenMethod · 0.65
InterfaceMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…