/
checkers.go
87 lines (75 loc) · 2 KB
/
checkers.go
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
package util
import (
. "gopkg.in/check.v1"
"reflect"
)
// -----------------------------------------------------------------------
// IsTrue checker.
type isTrueChecker struct {
*CheckerInfo
}
// The IsTrue checker tests whether the obtained value is true.
//
// For example:
//
// c.Assert(err, IsTrue)
//
var IsTrue Checker = &isTrueChecker{
&CheckerInfo{Name: "IsTrue", Params: []string{"value"}},
}
func (checker *isTrueChecker) Check(params []interface{}, names []string) (result bool, error string) {
return isTrue(params[0]), ""
}
func isTrue(obtained interface{}) (result bool) {
if obtained != nil {
switch v := reflect.ValueOf(obtained); v.Kind() {
case reflect.Bool:
return v.Bool() == true
}
}
return false
}
// -----------------------------------------------------------------------
// IsFalse checker.
type isFalseChecker struct {
*CheckerInfo
}
// The IsFalse checker tests whether the obtained value is false.
// It's an alias for Not(IsTrue).
//
// For example:
//
// c.Assert(err, IsFalse)
//
var IsFalse Checker = &isFalseChecker{
&CheckerInfo{Name: "IsFalse", Params: []string{"value"}},
}
func (checker *isFalseChecker) Check(params []interface{}, names []string) (result bool, error string) {
return !isTrue(params[0]), ""
}
// -----------------------------------------------------------------------
// IsEmptyString checker.
type isEmptyStringChecker struct {
*CheckerInfo
}
// The IsEmptyString checker tests whether the obtained string value equals "".
//
// For example:
//
// c.Assert("", IsEmptyString)
//
var IsEmptyString Checker = &isEmptyStringChecker{
&CheckerInfo{Name: "IsEmptyString", Params: []string{"value"}},
}
func (checker *isEmptyStringChecker) Check(params []interface{}, names []string) (result bool, error string) {
return isEmptyString(params[0]), ""
}
func isEmptyString(obtained interface{}) (result bool) {
if obtained != nil {
switch v := reflect.ValueOf(obtained); v.Kind() {
case reflect.String:
return v.String() == ""
}
}
return false
}