1
// Copyright 2011 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
10
gc "gopkg.in/check.v1"
13
type isTrueChecker struct {
17
// IsTrue checks whether a value has an underlying
18
// boolean type and is true.
19
var IsTrue gc.Checker = &isTrueChecker{
20
&gc.CheckerInfo{Name: "IsTrue", Params: []string{"obtained"}},
23
// IsTrue checks whether a value has an underlying
24
// boolean type and is false.
25
var IsFalse gc.Checker = gc.Not(IsTrue)
27
func (checker *isTrueChecker) Check(params []interface{}, names []string) (result bool, error string) {
29
value := reflect.ValueOf(params[0])
31
return false, fmt.Sprintf("expected type bool, received %s", value)
35
return value.Bool(), ""
38
return false, fmt.Sprintf("expected type bool, received type %s", value.Type())
41
type satisfiesChecker struct {
45
// Satisfies checks whether a value causes the argument
46
// function to return true. The function must be of
47
// type func(T) bool where the value being checked
48
// is assignable to T.
49
var Satisfies gc.Checker = &satisfiesChecker{
52
Params: []string{"obtained", "func(T) bool"},
56
func (checker *satisfiesChecker) Check(params []interface{}, names []string) (result bool, error string) {
57
f := reflect.ValueOf(params[1])
59
if ft.Kind() != reflect.Func ||
62
ft.Out(0) != reflect.TypeOf(true) {
63
return false, fmt.Sprintf("expected func(T) bool, got %s", ft)
65
v := reflect.ValueOf(params[0])
67
if !canBeNil(ft.In(0)) {
68
return false, fmt.Sprintf("cannot assign nil to argument %T", ft.In(0))
70
v = reflect.Zero(ft.In(0))
72
if !v.Type().AssignableTo(ft.In(0)) {
73
return false, fmt.Sprintf("wrong argument type %s for %s", v.Type(), ft)
75
return f.Call([]reflect.Value{v})[0].Interface().(bool), ""
78
func canBeNil(t reflect.Type) bool {
91
type deepEqualsChecker struct {
95
// The DeepEquals checker verifies that the obtained value is deep-equal to
96
// the expected value. The check will work correctly even when facing
97
// slices, interfaces, and values of different types (which always fail
102
// c.Assert(value, DeepEquals, 42)
103
// c.Assert(array, DeepEquals, []string{"hi", "there"})
105
// This checker differs from gocheck.DeepEquals in that
106
// it will compare a nil slice equal to an empty slice,
107
// and a nil map equal to an empty map.
108
var DeepEquals gc.Checker = &deepEqualsChecker{
109
&gc.CheckerInfo{Name: "DeepEquals", Params: []string{"obtained", "expected"}},
112
func (checker *deepEqualsChecker) Check(params []interface{}, names []string) (result bool, error string) {
113
if ok, err := DeepEqual(params[0], params[1]); !ok {
114
return false, err.Error()