1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
package compare
import (
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
var AnythingIsFine = "reql_test.AnythingIsFine"
func Assert(t *testing.T, expected, actual interface{}) {
expectedVal := expected
if e, ok := expected.(Expected); ok {
expectedVal = e.Val
}
ok, msg := Compare(expected, actual)
if !ok {
assert.Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n != %#v (actual)", expectedVal, actual), msg)
}
}
func AssertFalse(t *testing.T, expected, actual interface{}) {
expectedVal := expected
if e, ok := expected.(Expected); ok {
expectedVal = e.Val
}
ok, msg := Compare(expected, actual)
if ok {
assert.Fail(t, fmt.Sprintf("Should not be equal: %#v (expected)\n == %#v (actual)", expectedVal, actual), msg)
}
}
func AssertPrecision(t *testing.T, expected, actual interface{}, precision float64) {
expectedVal := expected
if e, ok := expected.(Expected); ok {
expectedVal = e.Val
}
ok, msg := ComparePrecision(expected, actual, precision)
if !ok {
assert.Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n != %#v (actual)", expectedVal, actual), msg)
}
}
func AssertPrecisionFalse(t *testing.T, expected, actual interface{}, precision float64) {
expectedVal := expected
if e, ok := expected.(Expected); ok {
expectedVal = e.Val
}
ok, msg := ComparePrecision(expected, actual, precision)
if ok {
assert.Fail(t, fmt.Sprintf("Should not be equal: %#v (expected)\n == %#v (actual)", expectedVal, actual), msg)
}
}
func Compare(expected, actual interface{}) (bool, string) {
return ComparePrecision(expected, actual, 0.00000000001)
}
func ComparePrecision(expected, actual interface{}, precision float64) (bool, string) {
return compare(expected, actual, true, false, precision)
}
func compare(expected, actual interface{}, ordered, partial bool, precision float64) (bool, string) {
if e, ok := expected.(Expected); ok {
partial = e.Partial
ordered = e.Ordered
expected = e.Val
}
// Anything
if expected == AnythingIsFine {
return true, ""
}
expectedVal := reflect.ValueOf(expected)
actualVal := reflect.ValueOf(actual)
// Nil
if expected == nil {
switch actualVal.Kind() {
case reflect.Bool:
expected = false
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
expected = 0.0
case reflect.String:
expected = ""
}
if expected == actual {
return true, ""
}
}
// Regex
if expr, ok := expected.(Regex); ok {
re, err := regexp.Compile(string(expr))
if err != nil {
return false, fmt.Sprintf("Failed to compile regexp: %s", err)
}
if actualVal.Kind() != reflect.String {
return false, fmt.Sprintf("Expected string, got %t (%T)", actual, actual)
}
if !re.MatchString(actualVal.String()) {
return false, fmt.Sprintf("Value %v did not match regexp '%s'", actual, expr)
}
return true, ""
}
switch expectedVal.Kind() {
// Bool
case reflect.Bool:
if expected == actual {
return true, ""
}
// Number
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
switch actualVal.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String:
diff := math.Abs(reflectNumber(expectedVal) - reflectNumber(actualVal))
if diff <= precision {
return true, ""
}
if precision != 0 {
return false, fmt.Sprintf("Value %v was not within %f of %v", expected, precision, actual)
}
return false, fmt.Sprintf("Expected %v but got %v", expected, actual)
}
// String
case reflect.String:
actualStr := fmt.Sprintf("%v", actual)
if expected == actualStr {
return true, ""
}
// Struct
case reflect.Struct:
// Convert expected struct to map and compare with actual value
return compare(reflectMap(expectedVal), actual, ordered, partial, precision)
// Map
case reflect.Map:
switch actualVal.Kind() {
case reflect.Struct:
// Convert actual struct to map and compare with expected map
return compare(expected, reflectMap(actualVal), ordered, partial, precision)
case reflect.Map:
expectedKeys := expectedVal.MapKeys()
actualKeys := actualVal.MapKeys()
for _, expectedKey := range expectedKeys {
keyFound := false
for _, actualKey := range actualKeys {
if ok, _ := Compare(expectedKey.Interface(), actualKey.Interface()); ok {
keyFound = true
break
}
}
if !keyFound {
return false, fmt.Sprintf("Expected field %v but not found", expectedKey)
}
}
if !partial {
expectedKeyVals := reflectMapKeys(expectedKeys)
actualKeyVals := reflectMapKeys(actualKeys)
if ok, _ := compare(expectedKeyVals, actualKeyVals, false, false, 0.0); !ok {
return false, fmt.Sprintf(
"Unmatched keys from either side: expected fields %v, got %v",
expectedKeyVals, actualKeyVals,
)
}
}
expectedMap := reflectMap(expectedVal)
actualMap := reflectMap(actualVal)
for k, v := range expectedMap {
if ok, reason := compare(v, actualMap[k], ordered, partial, precision); !ok {
return false, reason
}
}
return true, ""
default:
return false, fmt.Sprintf("Expected map, got %v (%T)", actual, actual)
}
// Slice/Array
case reflect.Slice, reflect.Array:
switch actualVal.Kind() {
case reflect.Slice, reflect.Array:
if ordered {
expectedArr := reflectSlice(expectedVal)
actualArr := reflectSlice(actualVal)
j := 0
for i := 0; i < len(expectedArr); i++ {
expectedArrVal := expectedArr[i]
for {
if j >= len(actualArr) {
return false, fmt.Sprintf("Ran out of results before finding %v", expectedArrVal)
}
actualArrVal := actualArr[j]
j++
if ok, _ := compare(expectedArrVal, actualArrVal, ordered, partial, precision); ok {
break
} else if !partial {
return false, fmt.Sprintf("Unexpected item %v while looking for %v", actualArrVal, expectedArrVal)
}
}
}
if !partial && j < len(actualArr) {
return false, fmt.Sprintf("Unexpected extra results: %v", actualArr[j:])
}
} else {
expectedArr := reflectSlice(expectedVal)
actualArr := reflectSlice(actualVal)
for _, expectedArrVal := range expectedArr {
found := false
for j, actualArrVal := range actualArr {
if ok, _ := compare(expectedArrVal, actualArrVal, ordered, partial, precision); ok {
found = true
actualArr = append(actualArr[:j], actualArr[j+1:]...)
break
}
}
if !found {
return false, fmt.Sprintf("Missing expected item %v", expectedArrVal)
}
}
if !partial && len(actualArr) > 0 {
return false, fmt.Sprintf("Extra items returned: %v", expectedArr)
}
}
return true, ""
}
// Other
default:
if expected == actual {
return true, ""
}
}
return false, fmt.Sprintf("Expected %v (%T) but got %v (%T)", expected, expected, actual, actual)
}
func reflectNumber(v reflect.Value) float64 {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint())
case reflect.Float32, reflect.Float64:
return v.Float()
case reflect.String:
f, _ := strconv.ParseFloat(v.String(), 64)
return f
default:
return float64(0)
}
}
func reflectMap(v reflect.Value) map[interface{}]interface{} {
switch v.Kind() {
case reflect.Struct:
m := map[interface{}]interface{}{}
for i := 0; i < v.NumField(); i++ {
sf := v.Type().Field(i)
if sf.PkgPath != "" && !sf.Anonymous {
continue // unexported
}
k := sf.Name
v := v.Field(i).Interface()
m[k] = v
}
return m
case reflect.Map:
m := map[interface{}]interface{}{}
for _, mk := range v.MapKeys() {
k := ""
if mk.Interface() != nil {
k = fmt.Sprintf("%v", mk.Interface())
}
v := v.MapIndex(mk).Interface()
m[k] = v
}
return m
default:
return nil
}
}
func reflectSlice(v reflect.Value) []interface{} {
switch v.Kind() {
case reflect.Slice, reflect.Array:
s := []interface{}{}
for i := 0; i < v.Len(); i++ {
s = append(s, v.Index(i).Interface())
}
return s
default:
return nil
}
}
func reflectMapKeys(keys []reflect.Value) []interface{} {
s := []interface{}{}
for _, key := range keys {
s = append(s, key.Interface())
}
return s
}
func reflectInterfaces(vals []reflect.Value) []interface{} {
ret := []interface{}{}
for _, val := range vals {
ret = append(ret, val.Interface())
}
return ret
}
|