Browse code

Add integration-cli/checker package

Add a `checker` package that adds some utility Checker implementation,
the first one being `checker.Contains`, as well as brining all go-check
provided Checker implementations in scope as a commodity.

Signed-off-by: Arnaud Porterie <arnaud.porterie@docker.com>

Arnaud Porterie authored on 2015/09/04 07:55:52
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+// Checker provide Docker specific implementations of the go-check.Checker interface.
1
+package checker
2
+
3
+import (
4
+	"fmt"
5
+	"strings"
6
+
7
+	"github.com/go-check/check"
8
+)
9
+
10
+// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
11
+// to think about check.X versus checker.X.
12
+var (
13
+	DeepEquals   = check.DeepEquals
14
+	Equals       = check.Equals
15
+	ErrorMatches = check.ErrorMatches
16
+	FitsTypeOf   = check.FitsTypeOf
17
+	HasLen       = check.HasLen
18
+	Implements   = check.Implements
19
+	IsNil        = check.IsNil
20
+	Matches      = check.Matches
21
+	Not          = check.Not
22
+	NotNil       = check.NotNil
23
+	PanicMatches = check.PanicMatches
24
+	Panics       = check.Panics
25
+)
26
+
27
+// Contains checker verifies that string value contains a substring.
28
+var Contains check.Checker = &containsChecker{
29
+	&check.CheckerInfo{
30
+		Name:   "Contains",
31
+		Params: []string{"value", "substring"},
32
+	},
33
+}
34
+
35
+type containsChecker struct {
36
+	*check.CheckerInfo
37
+}
38
+
39
+func (checker *containsChecker) Check(params []interface{}, names []string) (bool, string) {
40
+	return contains(params[0], params[1])
41
+}
42
+
43
+func contains(value, substring interface{}) (bool, string) {
44
+	substringStr, ok := substring.(string)
45
+	if !ok {
46
+		return false, "Substring must be a string"
47
+	}
48
+	valueStr, valueIsStr := value.(string)
49
+	if !valueIsStr {
50
+		if valueWithStr, valueHasStr := value.(fmt.Stringer); valueHasStr {
51
+			valueStr, valueIsStr = valueWithStr.String(), true
52
+		}
53
+	}
54
+	if valueIsStr {
55
+		return strings.Contains(valueStr, substringStr), ""
56
+	}
57
+	return false, "Obtained value is not a string and has no .String()"
58
+}