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
|
package expectate
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
// Fataler is satisfied by *testing.T but could be something else if needed
type Fataler interface {
Fatalf(format string, args ...interface{})
}
// Expect constructs an ExpectorFunc object
func Expect(t Fataler) ExpectorFunc {
expector := new(Expector)
expector.t = t
return func(subject interface{}) *Expector {
expector.subject = subject
return expector
}
}
type ExpectorFunc func(subject interface{}) *Expector
type Expector struct {
t Fataler
subject interface{}
}
// ToBe checks simple equality, i.e. (x == y)
func (e Expector) ToBe(expected interface{}) {
if e.subject != expected {
e.t.Fatalf("%s is not %s", format(e.subject), format(expected))
}
}
// ToEqual checks strict equality, i.e. (cmp.Diff(x, y) == "")
func (e Expector) ToEqual(expected interface{}) {
diff := cmp.Diff(expected, e.subject)
if diff != "" {
e.t.Fatalf(diff)
}
}
// NotToBe checks simple inequality, i.e. (x != y)
func (e Expector) NotToBe(expected interface{}) {
if e.subject == expected {
e.t.Fatalf("%s is %s", format(e.subject), format(expected))
}
}
// NotToEqual checks strict inequality, i.e. (!cmp.Equal(x, y))
func (e Expector) NotToEqual(expected interface{}) {
if cmp.Equal(e.subject, expected) {
e.t.Fatalf("%s equals %s", format(e.subject), format(expected))
}
}
func format(v interface{}) string {
str, ok := v.(string)
if ok {
return fmt.Sprintf("'%s'", str)
}
return fmt.Sprint(v)
}
|