| 1 |
package expect |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
type JSON string |
| 12 |
|
| 13 |
var NotNil = struct{}{} |
| 14 |
|
| 15 |
type Expectation struct { |
| 16 |
actual interface{} |
| 17 |
others []interface{} |
| 18 |
Greater *ThanAssertion |
| 19 |
GreaterOrEqual *ToAssertion |
| 20 |
Less *ThanAssertion |
| 21 |
LessOrEqual *ToAssertion |
| 22 |
To *ToExpectation |
| 23 |
Not *InvertedExpectation |
| 24 |
} |
| 25 |
|
| 26 |
func (e *Expectation) ToEqual(expected interface{}, others ...interface{}) PostHandler { |
| 27 |
return e.To.Equal(expected, others...) |
| 28 |
} |
| 29 |
|
| 30 |
func (e *Expectation) ToEql(expected interface{}, others ...interface{}) PostHandler { |
| 31 |
return e.To.Eql(expected, others...) |
| 32 |
} |
| 33 |
|
| 34 |
func (e *Expectation) GreaterThan(expected interface{}) PostHandler { |
| 35 |
return e.Greater.Than(expected) |
| 36 |
} |
| 37 |
|
| 38 |
func (e *Expectation) GreaterOrEqualTo(expected interface{}) PostHandler { |
| 39 |
return e.GreaterOrEqual.To(expected) |
| 40 |
} |
| 41 |
|
| 42 |
func (e *Expectation) LessThan(expected interface{}) PostHandler { |
| 43 |
return e.Less.Than(expected) |
| 44 |
} |
| 45 |
|
| 46 |
func (e *Expectation) LessOrEqualTo(expected interface{}) PostHandler { |
| 47 |
return e.LessOrEqual.To(expected) |
| 48 |
} |
| 49 |
|
| 50 |
type InvertedExpectation struct { |
| 51 |
*Expectation |
| 52 |
} |
| 53 |
|
| 54 |
var Errorf = func(format string, args ...interface{}) { |
| 55 |
runner.Errorf(format, args...) |
| 56 |
} |
| 57 |
|
| 58 |
func Expect(actual interface{}, others ...interface{}) *Expectation { |
| 59 |
return expect(actual, others, true) |
| 60 |
} |
| 61 |
|
| 62 |
func Fail(format string, args ...interface{}) { |
| 63 |
Errorf(format, args...) |
| 64 |
panic(endTestErr) |
| 65 |
} |
| 66 |
|
| 67 |
func Skip(format string, args ...interface{}) { |
| 68 |
runner.Skip(format, args...) |
| 69 |
panic(endTestErr) |
| 70 |
} |
| 71 |
|
| 72 |
func expect(actual interface{}, others []interface{}, includeNot bool) *Expectation { |
| 73 |
e := &Expectation{actual: actual, others: others} |
| 74 |
e.Greater = newThanAssertion(actual, GreaterThanComparitor, "to be greater than", "greater than") |
| 75 |
e.GreaterOrEqual = newToAssertion(actual, GreaterOrEqualToComparitor, "to be greater or equal to") |
| 76 |
e.Less = newThanAssertion(actual, LessThanComparitor, "to be less than", "less than") |
| 77 |
e.LessOrEqual = newToAssertion(actual, LessThanOrEqualToComparitor, "to be less or equal to") |
| 78 |
e.To = &ToExpectation{ |
| 79 |
actual: actual, |
| 80 |
others: others, |
| 81 |
} |
| 82 |
if includeNot { |
| 83 |
e.Not = NotExpect(actual, others...) |
| 84 |
} |
| 85 |
return e |
| 86 |
} |
| 87 |
|
| 88 |
func NotExpect(actual interface{}, others ...interface{}) *InvertedExpectation { |
| 89 |
e := &InvertedExpectation{expect(actual, others, false)} |
| 90 |
e.Greater.invert = true |
| 91 |
e.GreaterOrEqual.invert = true |
| 92 |
e.Less.invert = true |
| 93 |
e.LessOrEqual.invert = true |
| 94 |
e.To.invert = true |
| 95 |
return e |
| 96 |
} |
| 97 |
|
| 98 |
type ToExpectation struct { |
| 99 |
invert bool |
| 100 |
actual interface{} |
| 101 |
others []interface{} |
| 102 |
} |
| 103 |
|
| 104 |
func (e *ToExpectation) Equal(expected interface{}, others ...interface{}) PostHandler { |
| 105 |
display := "to be equal to" |
| 106 |
if e.invert { |
| 107 |
display = "to equal" |
| 108 |
} |
| 109 |
assertion := newToAssertion(e.actual, EqualsComparitor, display) |
| 110 |
assertion.invert = e.invert |
| 111 |
failed := !equal(assertion, e.actual, expected) |
| 112 |
|
| 113 |
if len(others) != len(e.others) { |
| 114 |
Errorf("mismatch number of values and expectations %d != %d", len(e.others)+1, len(others)+1) |
| 115 |
failed = true |
| 116 |
} else { |
| 117 |
for i := 0; i < len(others); i++ { |
| 118 |
if equal(assertion, e.others[i], others[i]) == false { |
| 119 |
failed = true |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if failed { |
| 125 |
return NewFailureHandler(expected, e.actual) |
| 126 |
} |
| 127 |
return SuccessHandler |
| 128 |
} |
| 129 |
|
| 130 |
func (e *ToExpectation) Eql(expected interface{}, others ...interface{}) PostHandler { |
| 131 |
if len(others) == len(e.others) { |
| 132 |
expected = coerce(e.actual, expected) |
| 133 |
for i := 0; i < len(others); i++ { |
| 134 |
others[i] = coerce(e.others[i], others[i]) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return e.Equal(expected, others...) |
| 139 |
} |
| 140 |
|
| 141 |
func equal(assertion *ToAssertion, a, b interface{}) bool { |
| 142 |
aIsNil := IsNil(a) |
| 143 |
if b == NotNil { |
| 144 |
if aIsNil { |
| 145 |
showError(a, "", false, "to be nil", false) |
| 146 |
return false |
| 147 |
} else { |
| 148 |
return true |
| 149 |
} |
| 150 |
} |
| 151 |
bIsNil := IsNil(b) |
| 152 |
if aIsNil || bIsNil { |
| 153 |
if (aIsNil == bIsNil) == assertion.invert { |
| 154 |
showError(a, b, assertion.invert, assertion.display, false) |
| 155 |
return false |
| 156 |
} |
| 157 |
return true |
| 158 |
} |
| 159 |
assertion.actual = a |
| 160 |
return assertion.To(b) == SuccessHandler |
| 161 |
} |
| 162 |
|
| 163 |
func (e *ToExpectation) Contain(expected interface{}) PostHandler { |
| 164 |
c := contains(e.actual, expected) |
| 165 |
if e.invert == false && c == false { |
| 166 |
Errorf("%v does not contain %v", e.actual, expected) |
| 167 |
return NewFailureHandler(expected, e.actual) |
| 168 |
} |
| 169 |
if e.invert == true && c == true { |
| 170 |
Errorf("%v contains %v", e.actual, expected) |
| 171 |
return NewFailureHandler(expected, e.actual) |
| 172 |
} |
| 173 |
return SuccessHandler |
| 174 |
} |
| 175 |
|
| 176 |
type ToAssertion struct { |
| 177 |
actual interface{} |
| 178 |
comparitor comparitor |
| 179 |
display string |
| 180 |
invert bool |
| 181 |
} |
| 182 |
|
| 183 |
func newToAssertion(a interface{}, c comparitor, display string) *ToAssertion { |
| 184 |
return &ToAssertion{ |
| 185 |
actual: a, |
| 186 |
comparitor: c, |
| 187 |
display: display, |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func (a *ToAssertion) To(expected interface{}) PostHandler { |
| 192 |
original, actual := a.actual, a.actual |
| 193 |
|
| 194 |
isJson := false |
| 195 |
if j, ok := expected.(JSON); ok { |
| 196 |
var a []byte |
| 197 |
switch t := actual.(type) { |
| 198 |
case []byte: |
| 199 |
a = t |
| 200 |
case string: |
| 201 |
a = []byte(t) |
| 202 |
default: |
| 203 |
Errorf("JSON() helper can only be used with a string or []byte actual") |
| 204 |
return NewFailureHandler(expected, original) |
| 205 |
} |
| 206 |
if actual, ok = convertToJson(a); ok == false { |
| 207 |
Errorf("invalid json %v", string(a)) |
| 208 |
return NewFailureHandler(expected, original) |
| 209 |
} |
| 210 |
if expected, ok = convertToJson([]byte(j)); ok == false { |
| 211 |
Errorf("invalid json %v", string(j)) |
| 212 |
return NewFailureHandler(expected, original) |
| 213 |
} |
| 214 |
isJson = true |
| 215 |
} |
| 216 |
|
| 217 |
kind, ok := SameKind(actual, expected) |
| 218 |
if ok == false { |
| 219 |
Errorf("expected %v %s %v - type mismatch %s != %s", actual, a.display, expected, reflect.ValueOf(actual).Kind(), reflect.ValueOf(expected).Kind()) |
| 220 |
return NewFailureHandler(expected, a.actual) |
| 221 |
} |
| 222 |
if IsInt(actual) { |
| 223 |
actual, expected = ToInt64(actual, expected) |
| 224 |
kind = reflect.Int64 |
| 225 |
} else if IsUint(actual) { |
| 226 |
actual, expected = ToUint64(actual, expected) |
| 227 |
kind = reflect.Uint64 |
| 228 |
} else if kind == reflect.Slice && IsString(expected) { |
| 229 |
actual = ToString(actual) |
| 230 |
} else if kind == reflect.String && IsSlice(expected) { |
| 231 |
expected = ToString(expected) |
| 232 |
} |
| 233 |
if a.comparitor(kind, actual, expected) == a.invert { |
| 234 |
if isJson { |
| 235 |
actual, expected = convertFromJson(actual), convertFromJson(expected) |
| 236 |
} |
| 237 |
showError(actual, expected, a.invert, a.display, !isJson) |
| 238 |
return NewFailureHandler(expected, original) |
| 239 |
} |
| 240 |
return SuccessHandler |
| 241 |
} |
| 242 |
|
| 243 |
func showError(actual, expected interface{}, invert bool, display string, escape bool) { |
| 244 |
var inversion string |
| 245 |
if invert { |
| 246 |
inversion = "not " |
| 247 |
} |
| 248 |
if _, ok := actual.(string); ok { |
| 249 |
if escape == true { |
| 250 |
Errorf("expected %q %s%s %q", actual, inversion, display, expected) |
| 251 |
} else { |
| 252 |
Errorf("expected %s %s%s %s", actual, inversion, display, expected) |
| 253 |
} |
| 254 |
} else { |
| 255 |
Errorf("expected %v %s%s %v", actual, inversion, display, expected) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
type ThanAssertion struct { |
| 260 |
to *ToAssertion |
| 261 |
display string |
| 262 |
invert bool |
| 263 |
} |
| 264 |
|
| 265 |
func newThanAssertion(actual interface{}, c comparitor, toDisplay, thanDisplay string) *ThanAssertion { |
| 266 |
return &ThanAssertion{ |
| 267 |
to: newToAssertion(actual, c, toDisplay), |
| 268 |
display: thanDisplay, |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func (a *ThanAssertion) Than(expected interface{}) PostHandler { |
| 273 |
actual := a.to.actual |
| 274 |
a.to.invert = a.invert |
| 275 |
|
| 276 |
if _, ok := actual.(time.Time); ok { |
| 277 |
return a.to.To(expected) |
| 278 |
} |
| 279 |
|
| 280 |
if IsNumeric(actual) == false { |
| 281 |
Errorf("cannot use %s for type %s", a.display, reflect.ValueOf(actual).Kind()) |
| 282 |
return NewFailureHandler(expected, actual) |
| 283 |
} |
| 284 |
if IsNumeric(expected) == false { |
| 285 |
Errorf("cannot use %s for type %s", a.display, reflect.ValueOf(expected).Kind()) |
| 286 |
NewFailureHandler(expected, actual) |
| 287 |
} |
| 288 |
return a.to.To(expected) |
| 289 |
} |
| 290 |
|
| 291 |
func contains(actual, expected interface{}) bool { |
| 292 |
actualValue, expectedValue := reflect.ValueOf(actual), reflect.ValueOf(expected) |
| 293 |
actualKind, expectedKind := actualValue.Kind(), expectedValue.Kind() |
| 294 |
if actualKind == reflect.String { |
| 295 |
if expectedKind == reflect.String && strings.Contains(actual.(string), expected.(string)) { |
| 296 |
return true |
| 297 |
} |
| 298 |
return false |
| 299 |
} |
| 300 |
if actualKind == reflect.Slice || actualKind == reflect.Array { |
| 301 |
for i, l := 0, actualValue.Len(); i < l; i++ { |
| 302 |
if reflect.DeepEqual(actualValue.Index(i).Interface(), expected) { |
| 303 |
return true |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
if actualKind == reflect.Map { |
| 308 |
return actualValue.MapIndex(expectedValue).Kind() != reflect.Invalid |
| 309 |
} |
| 310 |
if actualBytes, ok := actual.([]byte); ok { |
| 311 |
if expectedBytes, ok := expected.([]byte); ok { |
| 312 |
return bytes.Contains(actualBytes, expectedBytes) |
| 313 |
} |
| 314 |
} |
| 315 |
return false |
| 316 |
} |
| 317 |
|
| 318 |
func convertToJson(bytes []byte) (interface{}, bool) { |
| 319 |
var m interface{} |
| 320 |
if err := json.Unmarshal(bytes, &m); err != nil { |
| 321 |
return bytes, false |
| 322 |
} |
| 323 |
return m, true |
| 324 |
} |
| 325 |
|
| 326 |
func convertFromJson(value interface{}) string { |
| 327 |
bytes, _ := json.MarshalIndent(value, "", " ") |
| 328 |
return string(bytes) |
| 329 |
} |
| 330 |
|
| 331 |
func coerce(actual interface{}, expected interface{}) interface{} { |
| 332 |
at := reflect.TypeOf(actual) |
| 333 |
et := reflect.TypeOf(expected) |
| 334 |
if et.ConvertibleTo(at) { |
| 335 |
return reflect.ValueOf(expected).Convert(at).Interface() |
| 336 |
} |
| 337 |
return expected |
| 338 |
} |