compareMap compares two maps, returns nil if they are equal, or else returns error.
(value, expect any)
| 325 | |
| 326 | // compareMap compares two maps, returns nil if they are equal, or else returns error. |
| 327 | func 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. |
| 374 | func AssertNil(value any) { |