github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/tests/integration/v2/assert.go (about) 1 package tests 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func isZeroOfUnderlyingType(x interface{}) bool { 9 if x == nil { 10 return true 11 } 12 if _, ok := x.([]string); ok { 13 return true 14 } 15 return x == reflect.Zero(reflect.TypeOf(x)).Interface() 16 } 17 18 // func objEq(expected, actual interface{}) bool { 19 20 // if expected == nil || actual == nil { 21 // return expected == actual 22 // } 23 // if exp, ok := expected.([]byte); ok { 24 // act, ok := actual.([]byte) 25 // if !ok { 26 // return false 27 // } else if exp == nil || act == nil { 28 // return exp == nil && act == nil 29 // } 30 // return bytes.Equal(exp, act) 31 // } 32 // return reflect.DeepEqual(expected, actual) 33 34 // } 35 36 // func assertSlice(t *testing.T, expected, actual interface{}) { 37 // if objEq(expected, actual) { 38 // t.Logf("%s OK", reflect.TypeOf(actual)) 39 // return 40 // } 41 42 // actualType := reflect.TypeOf(actual) 43 // if actualType == nil { 44 // t.Fatal() 45 // } 46 // expectedValue := reflect.ValueOf(expected) 47 // if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) { 48 // // Attempt comparison after type conversion 49 // if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual) { 50 // t.Logf("%s OK", reflect.TypeOf(actual)) 51 // return 52 // } 53 // } 54 55 // t.Fatalf("FAIL %s: expected %#v, got %#v", reflect.TypeOf(expected), expected, actual) 56 // } 57 58 func isPrimitive(exp interface{}) bool { 59 t := reflect.TypeOf(exp) 60 switch t.Kind() { 61 case reflect.Interface: 62 return false 63 case reflect.Struct: 64 return false 65 case reflect.Array: 66 return false 67 case reflect.Func: 68 return false 69 case reflect.Map: 70 return false 71 case reflect.Ptr: 72 return false 73 case reflect.Slice: 74 return false 75 case reflect.UnsafePointer: 76 return false 77 default: 78 return true 79 } 80 } 81 82 // does not work on slices 83 func assert(t *testing.T, expected interface{}, actual interface{}) { 84 85 prexp := reflect.ValueOf(expected) 86 pract := reflect.ValueOf(actual) 87 88 if isPrimitive(actual) { 89 if expected != actual { 90 t.Fatalf("expected %#v, got %#v", expected, actual) 91 } 92 t.Logf("OK %s", reflect.TypeOf(expected).Name()) 93 return 94 } 95 96 if pract.IsNil() { 97 t.Errorf("nil actual value: %#v", actual) 98 t.Fail() 99 return 100 } 101 102 exp := prexp.Elem() 103 act := pract.Elem() 104 105 if !exp.IsValid() { 106 t.Errorf("reflected expectation not valid (%#v)", expected) 107 t.Fail() 108 } 109 110 if exp.Type() != act.Type() { 111 t.Errorf("expected type %s, got %s", exp.Type(), act.Type()) 112 t.Fail() 113 } 114 115 for i := 0; i < exp.NumField(); i++ { 116 expValueField := exp.Field(i) 117 expTypeField := exp.Type().Field(i) 118 119 actValueField := act.Field(i) 120 actTypeField := act.Type().Field(i) 121 122 if expTypeField.Name != actTypeField.Name { 123 t.Errorf("expected type %s, got %s", expTypeField.Name, actTypeField.Name) 124 t.Errorf("%#v", actual) 125 t.Fail() 126 } 127 if isZeroOfUnderlyingType(expValueField.Interface()) { 128 continue 129 } 130 if !isZeroOfUnderlyingType(expValueField.Interface()) && isZeroOfUnderlyingType(actValueField.Interface()) { 131 t.Errorf("expected %s, but was empty", expTypeField.Name) 132 t.Errorf("%#v", actual) 133 t.Fail() 134 return 135 } 136 assert(t, expValueField.Interface(), actValueField.Interface()) 137 /* 138 if expValueField.Interface() != actValueField.Interface() { 139 t.Errorf("expected %s %#v, got %#v", expTypeField.Name, expValueField.Interface(), actValueField.Interface()) 140 t.Fail() 141 } 142 */ 143 } 144 if t.Failed() { 145 t.Logf("FAIL %s", exp.Type().Name()) 146 return 147 } 148 t.Logf("OK %s", exp.Type().Name()) 149 }