~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/testing/checkers/bool.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package checkers
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "reflect"
 
9
 
 
10
        gc "gopkg.in/check.v1"
 
11
)
 
12
 
 
13
type isTrueChecker struct {
 
14
        *gc.CheckerInfo
 
15
}
 
16
 
 
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"}},
 
21
}
 
22
 
 
23
// IsTrue checks whether a value has an underlying
 
24
// boolean type and is false.
 
25
var IsFalse gc.Checker = gc.Not(IsTrue)
 
26
 
 
27
func (checker *isTrueChecker) Check(params []interface{}, names []string) (result bool, error string) {
 
28
 
 
29
        value := reflect.ValueOf(params[0])
 
30
        if !value.IsValid() {
 
31
                return false, fmt.Sprintf("expected type bool, received %s", value)
 
32
        }
 
33
        switch value.Kind() {
 
34
        case reflect.Bool:
 
35
                return value.Bool(), ""
 
36
        }
 
37
 
 
38
        return false, fmt.Sprintf("expected type bool, received type %s", value.Type())
 
39
}
 
40
 
 
41
type satisfiesChecker struct {
 
42
        *gc.CheckerInfo
 
43
}
 
44
 
 
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{
 
50
        &gc.CheckerInfo{
 
51
                Name:   "Satisfies",
 
52
                Params: []string{"obtained", "func(T) bool"},
 
53
        },
 
54
}
 
55
 
 
56
func (checker *satisfiesChecker) Check(params []interface{}, names []string) (result bool, error string) {
 
57
        f := reflect.ValueOf(params[1])
 
58
        ft := f.Type()
 
59
        if ft.Kind() != reflect.Func ||
 
60
                ft.NumIn() != 1 ||
 
61
                ft.NumOut() != 1 ||
 
62
                ft.Out(0) != reflect.TypeOf(true) {
 
63
                return false, fmt.Sprintf("expected func(T) bool, got %s", ft)
 
64
        }
 
65
        v := reflect.ValueOf(params[0])
 
66
        if !v.IsValid() {
 
67
                if !canBeNil(ft.In(0)) {
 
68
                        return false, fmt.Sprintf("cannot assign nil to argument %T", ft.In(0))
 
69
                }
 
70
                v = reflect.Zero(ft.In(0))
 
71
        }
 
72
        if !v.Type().AssignableTo(ft.In(0)) {
 
73
                return false, fmt.Sprintf("wrong argument type %s for %s", v.Type(), ft)
 
74
        }
 
75
        return f.Call([]reflect.Value{v})[0].Interface().(bool), ""
 
76
}
 
77
 
 
78
func canBeNil(t reflect.Type) bool {
 
79
        switch t.Kind() {
 
80
        case reflect.Chan,
 
81
                reflect.Func,
 
82
                reflect.Interface,
 
83
                reflect.Map,
 
84
                reflect.Ptr,
 
85
                reflect.Slice:
 
86
                return true
 
87
        }
 
88
        return false
 
89
}
 
90
 
 
91
type deepEqualsChecker struct {
 
92
        *gc.CheckerInfo
 
93
}
 
94
 
 
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
 
98
// the test).
 
99
//
 
100
// For example:
 
101
//
 
102
//     c.Assert(value, DeepEquals, 42)
 
103
//     c.Assert(array, DeepEquals, []string{"hi", "there"})
 
104
//
 
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"}},
 
110
}
 
111
 
 
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()
 
115
        }
 
116
        return true, ""
 
117
}