harness-drone/vendor/github.com/franela/goblin/assertions.go

60 lines
1.1 KiB
Go
Raw Normal View History

2016-03-07 19:23:49 +00:00
package goblin
import (
"fmt"
"reflect"
2016-03-07 19:51:06 +00:00
"strings"
2016-03-07 19:23:49 +00:00
)
type Assertion struct {
src interface{}
fail func(interface{})
}
func objectsAreEqual(a, b interface{}) bool {
if reflect.TypeOf(a) != reflect.TypeOf(b) {
return false
}
if reflect.DeepEqual(a, b) {
return true
}
if fmt.Sprintf("%#v", a) == fmt.Sprintf("%#v", b) {
return true
}
return false
}
2016-03-07 19:51:06 +00:00
func formatMessages(messages ...string) string {
if len(messages) > 0 {
return ", " + strings.Join(messages, " ")
}
return ""
}
2016-03-07 19:23:49 +00:00
func (a *Assertion) Eql(dst interface{}) {
a.Equal(dst)
}
func (a *Assertion) Equal(dst interface{}) {
if !objectsAreEqual(a.src, dst) {
2016-03-07 19:51:06 +00:00
a.fail(fmt.Sprintf("%#v %s %#v", a.src, "does not equal", dst))
2016-03-07 19:23:49 +00:00
}
}
2016-03-07 19:51:06 +00:00
func (a *Assertion) IsTrue(messages ...string) {
2016-03-07 19:23:49 +00:00
if !objectsAreEqual(a.src, true) {
2016-03-07 19:51:06 +00:00
message := fmt.Sprintf("%v %s%s", a.src, "expected false to be truthy", formatMessages(messages...))
a.fail(message)
2016-03-07 19:23:49 +00:00
}
}
2016-03-07 19:51:06 +00:00
func (a *Assertion) IsFalse(messages ...string) {
2016-03-07 19:23:49 +00:00
if !objectsAreEqual(a.src, false) {
2016-03-07 19:51:06 +00:00
message := fmt.Sprintf("%v %s%s", a.src, "expected true to be falsey", formatMessages(messages...))
a.fail(message)
2016-03-07 19:23:49 +00:00
}
}