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
|
package expect
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"time"
)
type JSON string
var NotNil = struct{}{}
type Expectation struct {
actual interface{}
others []interface{}
Greater *ThanAssertion
GreaterOrEqual *ToAssertion
Less *ThanAssertion
LessOrEqual *ToAssertion
To *ToExpectation
Not *InvertedExpectation
}
func (e *Expectation) ToEqual(expected interface{}, others ...interface{}) PostHandler {
return e.To.Equal(expected, others...)
}
func (e *Expectation) ToEql(expected interface{}, others ...interface{}) PostHandler {
return e.To.Eql(expected, others...)
}
func (e *Expectation) GreaterThan(expected interface{}) PostHandler {
return e.Greater.Than(expected)
}
func (e *Expectation) GreaterOrEqualTo(expected interface{}) PostHandler {
return e.GreaterOrEqual.To(expected)
}
func (e *Expectation) LessThan(expected interface{}) PostHandler {
return e.Less.Than(expected)
}
func (e *Expectation) LessOrEqualTo(expected interface{}) PostHandler {
return e.LessOrEqual.To(expected)
}
type InvertedExpectation struct {
*Expectation
}
var Errorf = func(format string, args ...interface{}) {
runner.Errorf(format, args...)
}
func Expect(actual interface{}, others ...interface{}) *Expectation {
return expect(actual, others, true)
}
func Fail(format string, args ...interface{}) {
Errorf(format, args...)
panic(endTestErr)
}
func Skip(format string, args ...interface{}) {
runner.Skip(format, args...)
panic(endTestErr)
}
func expect(actual interface{}, others []interface{}, includeNot bool) *Expectation {
e := &Expectation{actual: actual, others: others}
e.Greater = newThanAssertion(actual, GreaterThanComparitor, "to be greater than", "greater than")
e.GreaterOrEqual = newToAssertion(actual, GreaterOrEqualToComparitor, "to be greater or equal to")
e.Less = newThanAssertion(actual, LessThanComparitor, "to be less than", "less than")
e.LessOrEqual = newToAssertion(actual, LessThanOrEqualToComparitor, "to be less or equal to")
e.To = &ToExpectation{
actual: actual,
others: others,
}
if includeNot {
e.Not = NotExpect(actual, others...)
}
return e
}
func NotExpect(actual interface{}, others ...interface{}) *InvertedExpectation {
e := &InvertedExpectation{expect(actual, others, false)}
e.Greater.invert = true
e.GreaterOrEqual.invert = true
e.Less.invert = true
e.LessOrEqual.invert = true
e.To.invert = true
return e
}
type ToExpectation struct {
invert bool
actual interface{}
others []interface{}
}
func (e *ToExpectation) Equal(expected interface{}, others ...interface{}) PostHandler {
display := "to be equal to"
if e.invert {
display = "to equal"
}
assertion := newToAssertion(e.actual, EqualsComparitor, display)
assertion.invert = e.invert
failed := !equal(assertion, e.actual, expected)
if len(others) != len(e.others) {
Errorf("mismatch number of values and expectations %d != %d", len(e.others)+1, len(others)+1)
failed = true
} else {
for i := 0; i < len(others); i++ {
if equal(assertion, e.others[i], others[i]) == false {
failed = true
}
}
}
if failed {
return NewFailureHandler(expected, e.actual)
}
return SuccessHandler
}
func (e *ToExpectation) Eql(expected interface{}, others ...interface{}) PostHandler {
if len(others) == len(e.others) {
expected = coerce(e.actual, expected)
for i := 0; i < len(others); i++ {
others[i] = coerce(e.others[i], others[i])
}
}
return e.Equal(expected, others...)
}
func equal(assertion *ToAssertion, a, b interface{}) bool {
aIsNil := IsNil(a)
if b == NotNil {
if aIsNil {
showError(a, "", false, "to be nil", false)
return false
} else {
return true
}
}
bIsNil := IsNil(b)
if aIsNil || bIsNil {
if (aIsNil == bIsNil) == assertion.invert {
showError(a, b, assertion.invert, assertion.display, false)
return false
}
return true
}
assertion.actual = a
return assertion.To(b) == SuccessHandler
}
func (e *ToExpectation) Contain(expected interface{}) PostHandler {
c := contains(e.actual, expected)
if e.invert == false && c == false {
Errorf("%v does not contain %v", e.actual, expected)
return NewFailureHandler(expected, e.actual)
}
if e.invert == true && c == true {
Errorf("%v contains %v", e.actual, expected)
return NewFailureHandler(expected, e.actual)
}
return SuccessHandler
}
type ToAssertion struct {
actual interface{}
comparitor comparitor
display string
invert bool
}
func newToAssertion(a interface{}, c comparitor, display string) *ToAssertion {
return &ToAssertion{
actual: a,
comparitor: c,
display: display,
}
}
func (a *ToAssertion) To(expected interface{}) PostHandler {
original, actual := a.actual, a.actual
isJson := false
if j, ok := expected.(JSON); ok {
var a []byte
switch t := actual.(type) {
case []byte:
a = t
case string:
a = []byte(t)
default:
Errorf("JSON() helper can only be used with a string or []byte actual")
return NewFailureHandler(expected, original)
}
if actual, ok = convertToJson(a); ok == false {
Errorf("invalid json %v", string(a))
return NewFailureHandler(expected, original)
}
if expected, ok = convertToJson([]byte(j)); ok == false {
Errorf("invalid json %v", string(j))
return NewFailureHandler(expected, original)
}
isJson = true
}
kind, ok := SameKind(actual, expected)
if ok == false {
Errorf("expected %v %s %v - type mismatch %s != %s", actual, a.display, expected, reflect.ValueOf(actual).Kind(), reflect.ValueOf(expected).Kind())
return NewFailureHandler(expected, a.actual)
}
if IsInt(actual) {
actual, expected = ToInt64(actual, expected)
kind = reflect.Int64
} else if IsUint(actual) {
actual, expected = ToUint64(actual, expected)
kind = reflect.Uint64
} else if kind == reflect.Slice && IsString(expected) {
actual = ToString(actual)
} else if kind == reflect.String && IsSlice(expected) {
expected = ToString(expected)
}
if a.comparitor(kind, actual, expected) == a.invert {
if isJson {
actual, expected = convertFromJson(actual), convertFromJson(expected)
}
showError(actual, expected, a.invert, a.display, !isJson)
return NewFailureHandler(expected, original)
}
return SuccessHandler
}
func showError(actual, expected interface{}, invert bool, display string, escape bool) {
var inversion string
if invert {
inversion = "not "
}
if _, ok := actual.(string); ok {
if escape == true {
Errorf("expected %q %s%s %q", actual, inversion, display, expected)
} else {
Errorf("expected %s %s%s %s", actual, inversion, display, expected)
}
} else {
Errorf("expected %v %s%s %v", actual, inversion, display, expected)
}
}
type ThanAssertion struct {
to *ToAssertion
display string
invert bool
}
func newThanAssertion(actual interface{}, c comparitor, toDisplay, thanDisplay string) *ThanAssertion {
return &ThanAssertion{
to: newToAssertion(actual, c, toDisplay),
display: thanDisplay,
}
}
func (a *ThanAssertion) Than(expected interface{}) PostHandler {
actual := a.to.actual
a.to.invert = a.invert
if _, ok := actual.(time.Time); ok {
return a.to.To(expected)
}
if IsNumeric(actual) == false {
Errorf("cannot use %s for type %s", a.display, reflect.ValueOf(actual).Kind())
return NewFailureHandler(expected, actual)
}
if IsNumeric(expected) == false {
Errorf("cannot use %s for type %s", a.display, reflect.ValueOf(expected).Kind())
NewFailureHandler(expected, actual)
}
return a.to.To(expected)
}
func contains(actual, expected interface{}) bool {
actualValue, expectedValue := reflect.ValueOf(actual), reflect.ValueOf(expected)
actualKind, expectedKind := actualValue.Kind(), expectedValue.Kind()
if actualKind == reflect.String {
if expectedKind == reflect.String && strings.Contains(actual.(string), expected.(string)) {
return true
}
return false
}
if actualKind == reflect.Slice || actualKind == reflect.Array {
for i, l := 0, actualValue.Len(); i < l; i++ {
if reflect.DeepEqual(actualValue.Index(i).Interface(), expected) {
return true
}
}
}
if actualKind == reflect.Map {
return actualValue.MapIndex(expectedValue).Kind() != reflect.Invalid
}
if actualBytes, ok := actual.([]byte); ok {
if expectedBytes, ok := expected.([]byte); ok {
return bytes.Contains(actualBytes, expectedBytes)
}
}
return false
}
func convertToJson(bytes []byte) (interface{}, bool) {
var m interface{}
if err := json.Unmarshal(bytes, &m); err != nil {
return bytes, false
}
return m, true
}
func convertFromJson(value interface{}) string {
bytes, _ := json.MarshalIndent(value, "", " ")
return string(bytes)
}
func coerce(actual interface{}, expected interface{}) interface{} {
at := reflect.TypeOf(actual)
et := reflect.TypeOf(expected)
if et.ConvertibleTo(at) {
return reflect.ValueOf(expected).Convert(at).Interface()
}
return expected
}
|