| 1 |
package testutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
func AssertNil(t *testing.T, actual interface{}) { |
| 12 |
t.Helper() |
| 13 |
if !isNil(actual) { |
| 14 |
t.Errorf("assertion failed; expected actual to be nil") |
| 15 |
t.FailNow() |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func AssertNotNil(t *testing.T, actual interface{}, message ...interface{}) { |
| 21 |
t.Helper() |
| 22 |
if isNil(actual) { |
| 23 |
t.Error("assertion failed; expected actual to not be nil") |
| 24 |
if len(message) > 0 { |
| 25 |
t.Error(message...) |
| 26 |
} |
| 27 |
t.FailNow() |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func AssertEqual(t *testing.T, expected, actual interface{}, message ...interface{}) { |
| 33 |
t.Helper() |
| 34 |
if !equal(expected, actual) { |
| 35 |
t.Errorf("assertion failed; expected %v to equal %v", actual, expected) |
| 36 |
if len(message) > 0 { |
| 37 |
t.Error(message...) |
| 38 |
} |
| 39 |
t.FailNow() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func AssertNotEqual(t *testing.T, expected, actual interface{}, message ...interface{}) { |
| 45 |
t.Helper() |
| 46 |
if equal(expected, actual) { |
| 47 |
t.Errorf("assertion failed; expected %v to not equal %v", actual, expected) |
| 48 |
if len(message) > 0 { |
| 49 |
t.Error(message...) |
| 50 |
} |
| 51 |
t.FailNow() |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func AssertZero(t *testing.T, actual interface{}, message ...interface{}) { |
| 57 |
t.Helper() |
| 58 |
AssertEqual(t, 0, actual) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
func AssertNotZero(t *testing.T, actual interface{}, message ...interface{}) { |
| 63 |
t.Helper() |
| 64 |
AssertNotEqual(t, 0, actual) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func AssertTrue(t *testing.T, arg bool, message ...interface{}) { |
| 69 |
t.Helper() |
| 70 |
if !arg { |
| 71 |
t.Errorf("assertion failed; expected actual to be true") |
| 72 |
if len(message) > 0 { |
| 73 |
t.Error(message...) |
| 74 |
} |
| 75 |
t.FailNow() |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func AssertFalse(t *testing.T, arg bool, message ...interface{}) { |
| 81 |
t.Helper() |
| 82 |
if arg { |
| 83 |
t.Errorf("assertion failed; expected actual to be false") |
| 84 |
if len(message) > 0 { |
| 85 |
t.Error(message...) |
| 86 |
} |
| 87 |
t.FailNow() |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func AssertInDelta(t *testing.T, from, to, delta float64, message ...interface{}) { |
| 95 |
t.Helper() |
| 96 |
if diff := math.Abs(from - to); diff > delta { |
| 97 |
t.Errorf("assertion failed; expected absolute difference of %f and %f to be %f", from, to, delta) |
| 98 |
if len(message) > 0 { |
| 99 |
t.Error(message...) |
| 100 |
} |
| 101 |
t.FailNow() |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func AssertEmpty(t *testing.T, arg interface{}, message ...interface{}) { |
| 107 |
t.Helper() |
| 108 |
if getLength(arg) != 0 { |
| 109 |
t.Errorf("assertion failed; expected actual to be empty") |
| 110 |
if len(message) > 0 { |
| 111 |
t.Error(message...) |
| 112 |
} |
| 113 |
t.FailNow() |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func AssertNotEmpty(t *testing.T, arg interface{}, message ...interface{}) { |
| 119 |
t.Helper() |
| 120 |
if getLength(arg) == 0 { |
| 121 |
t.Errorf("assertion failed; expected actual to not be empty") |
| 122 |
if len(message) > 0 { |
| 123 |
t.Error(message...) |
| 124 |
} |
| 125 |
t.FailNow() |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func AssertLen(t *testing.T, arg interface{}, length int, message ...interface{}) { |
| 131 |
t.Helper() |
| 132 |
if getLength(arg) != length { |
| 133 |
t.Errorf("assertion failed; expected actual to have length %d", length) |
| 134 |
if len(message) > 0 { |
| 135 |
t.Error(message...) |
| 136 |
} |
| 137 |
t.FailNow() |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func AssertContains(t *testing.T, s, substr string, message ...interface{}) { |
| 143 |
t.Helper() |
| 144 |
if !strings.Contains(s, substr) { |
| 145 |
t.Errorf("assertion failed; expected actual to contain %q", substr) |
| 146 |
if len(message) > 0 { |
| 147 |
t.Error(message...) |
| 148 |
} |
| 149 |
t.FailNow() |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func AssertNotContains(t *testing.T, s, substr string, message ...interface{}) { |
| 155 |
t.Helper() |
| 156 |
if strings.Contains(s, substr) { |
| 157 |
t.Errorf("assertion failed; expected actual to not contain %q", substr) |
| 158 |
if len(message) > 0 { |
| 159 |
t.Error(message...) |
| 160 |
} |
| 161 |
t.FailNow() |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func equal(expected, actual interface{}) bool { |
| 166 |
if expected == nil && actual == nil { |
| 167 |
return true |
| 168 |
} |
| 169 |
if (expected == nil && actual != nil) || (expected != nil && actual == nil) { |
| 170 |
return false |
| 171 |
} |
| 172 |
|
| 173 |
actualType := reflect.TypeOf(actual) |
| 174 |
if actualType == nil { |
| 175 |
return false |
| 176 |
} |
| 177 |
expectedValue := reflect.ValueOf(expected) |
| 178 |
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) { |
| 179 |
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual) |
| 180 |
} |
| 181 |
|
| 182 |
return reflect.DeepEqual(expected, actual) |
| 183 |
} |
| 184 |
|
| 185 |
func isNil(object interface{}) bool { |
| 186 |
if object == nil { |
| 187 |
return true |
| 188 |
} |
| 189 |
|
| 190 |
value := reflect.ValueOf(object) |
| 191 |
kind := value.Kind() |
| 192 |
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() { |
| 193 |
return true |
| 194 |
} |
| 195 |
return false |
| 196 |
} |
| 197 |
|
| 198 |
func getLength(object interface{}) int { |
| 199 |
if object == nil { |
| 200 |
return 0 |
| 201 |
} else if object == "" { |
| 202 |
return 0 |
| 203 |
} |
| 204 |
|
| 205 |
objValue := reflect.ValueOf(object) |
| 206 |
|
| 207 |
switch objValue.Kind() { |
| 208 |
case reflect.Map: |
| 209 |
fallthrough |
| 210 |
case reflect.Slice, reflect.Chan, reflect.String: |
| 211 |
{ |
| 212 |
return objValue.Len() |
| 213 |
} |
| 214 |
} |
| 215 |
return 0 |
| 216 |
} |