mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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>
This commit is contained in:
parent
9e4addde76
commit
c87cbd3862
1 changed files with 59 additions and 0 deletions
59
integration-cli/checker/checker.go
Normal file
59
integration-cli/checker/checker.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
// 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()"
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue