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{})
| 1146 | |
| 1147 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. |
| 1148 | func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { |
| 1149 | if h, ok := t.(tHelper); ok { |
| 1150 | h.Helper() |
| 1151 | } |
| 1152 | if expected == nil || actual == nil || |
| 1153 | reflect.TypeOf(actual).Kind() != reflect.Map || |
| 1154 | reflect.TypeOf(expected).Kind() != reflect.Map { |
| 1155 | return Fail(t, "Arguments must be maps", msgAndArgs...) |
| 1156 | } |
| 1157 | |
| 1158 | expectedMap := reflect.ValueOf(expected) |
| 1159 | actualMap := reflect.ValueOf(actual) |
| 1160 | |
| 1161 | if expectedMap.Len() != actualMap.Len() { |
| 1162 | return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) |
| 1163 | } |
| 1164 | |
| 1165 | for _, k := range expectedMap.MapKeys() { |
| 1166 | ev := expectedMap.MapIndex(k) |
| 1167 | av := actualMap.MapIndex(k) |
| 1168 | |
| 1169 | if !ev.IsValid() { |
| 1170 | return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) |
| 1171 | } |
| 1172 | |
| 1173 | if !av.IsValid() { |
| 1174 | return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) |
| 1175 | } |
| 1176 | |
| 1177 | if !InDelta( |
| 1178 | t, |
| 1179 | ev.Interface(), |
| 1180 | av.Interface(), |
| 1181 | delta, |
| 1182 | msgAndArgs..., |
| 1183 | ) { |
| 1184 | return false |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return true |
| 1189 | } |
| 1190 | |
| 1191 | func calcRelativeError(expected, actual interface{}) (float64, error) { |
| 1192 | af, aok := toFloat(expected) |
searching dependent graphs…