forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
59 lines (52 loc) · 1.54 KB
/
checker.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
// Package checker provide Docker specific implementations of the go-check.Checker interface.
package checker
import (
"fmt"
"strings"
"github.com/go-check/check"
)
// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
// to think about check.X versus checker.X.
var (
DeepEquals = check.DeepEquals
Equals = check.Equals
ErrorMatches = check.ErrorMatches
FitsTypeOf = check.FitsTypeOf
HasLen = check.HasLen
Implements = check.Implements
IsNil = check.IsNil
Matches = check.Matches
Not = check.Not
NotNil = check.NotNil
PanicMatches = check.PanicMatches
Panics = check.Panics
)
// Contains checker verifies that string value contains a substring.
var Contains check.Checker = &containsChecker{
&check.CheckerInfo{
Name: "Contains",
Params: []string{"value", "substring"},
},
}
type containsChecker struct {
*check.CheckerInfo
}
func (checker *containsChecker) Check(params []interface{}, names []string) (bool, string) {
return contains(params[0], params[1])
}
func contains(value, substring interface{}) (bool, string) {
substringStr, ok := substring.(string)
if !ok {
return false, "Substring must be a string"
}
valueStr, valueIsStr := value.(string)
if !valueIsStr {
if valueWithStr, valueHasStr := value.(fmt.Stringer); valueHasStr {
valueStr, valueIsStr = valueWithStr.String(), true
}
}
if valueIsStr {
return strings.Contains(valueStr, substringStr), ""
}
return false, "Obtained value is not a string and has no .String()"
}