InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{})
| 1416 | |
| 1417 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. |
| 1418 | func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { |
| 1419 | if h, ok := t.(tHelper); ok { |
| 1420 | h.Helper() |
| 1421 | } |
| 1422 | if expected == nil || actual == nil || |
| 1423 | reflect.TypeOf(actual).Kind() != reflect.Map || |
| 1424 | reflect.TypeOf(expected).Kind() != reflect.Map { |
| 1425 | return Fail(t, "Arguments must be maps", msgAndArgs...) |
| 1426 | } |
| 1427 | |
| 1428 | expectedMap := reflect.ValueOf(expected) |
| 1429 | actualMap := reflect.ValueOf(actual) |
| 1430 | |
| 1431 | if expectedMap.Len() != actualMap.Len() { |
| 1432 | return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) |
| 1433 | } |
| 1434 | |
| 1435 | for _, k := range expectedMap.MapKeys() { |
| 1436 | ev := expectedMap.MapIndex(k) |
| 1437 | av := actualMap.MapIndex(k) |
| 1438 | |
| 1439 | if !ev.IsValid() { |
| 1440 | return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) |
| 1441 | } |
| 1442 | |
| 1443 | if !av.IsValid() { |
| 1444 | return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) |
| 1445 | } |
| 1446 | |
| 1447 | if !InDelta( |
| 1448 | t, |
| 1449 | ev.Interface(), |
| 1450 | av.Interface(), |
| 1451 | delta, |
| 1452 | msgAndArgs..., |
| 1453 | ) { |
| 1454 | return false |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | return true |
| 1459 | } |
| 1460 | |
| 1461 | func calcRelativeError(expected, actual interface{}) (float64, error) { |
| 1462 | af, aok := toFloat(expected) |
searching dependent graphs…