Vendoring shakers library and update go-check
The shakers library defines a bunch of go-check checkers to ease writing tests. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
831c2e7a7b
commit
4538373791
2 changed files with 24 additions and 94 deletions
|
@ -2,17 +2,14 @@
|
|||
package checker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-check/check"
|
||||
"github.com/vdemeester/shakers"
|
||||
)
|
||||
|
||||
// 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
|
||||
|
@ -23,37 +20,27 @@ var (
|
|||
NotNil = check.NotNil
|
||||
PanicMatches = check.PanicMatches
|
||||
Panics = check.Panics
|
||||
|
||||
Contains = shakers.Contains
|
||||
ContainsAny = shakers.ContainsAny
|
||||
Count = shakers.Count
|
||||
Equals = shakers.Equals
|
||||
EqualFold = shakers.EqualFold
|
||||
False = shakers.False
|
||||
GreaterOrEqualThan = shakers.GreaterOrEqualThan
|
||||
GreaterThan = shakers.GreaterThan
|
||||
HasPrefix = shakers.HasPrefix
|
||||
HasSuffix = shakers.HasSuffix
|
||||
Index = shakers.Index
|
||||
IndexAny = shakers.IndexAny
|
||||
IsAfter = shakers.IsAfter
|
||||
IsBefore = shakers.IsBefore
|
||||
IsBetween = shakers.IsBetween
|
||||
IsLower = shakers.IsLower
|
||||
IsUpper = shakers.IsUpper
|
||||
LessOrEqualThan = shakers.LessOrEqualThan
|
||||
LessThan = shakers.LessThan
|
||||
TimeEquals = shakers.TimeEquals
|
||||
True = shakers.True
|
||||
TimeIgnore = shakers.TimeIgnore
|
||||
)
|
||||
|
||||
// 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()"
|
||||
}
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
package checker
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
check.TestingT(t)
|
||||
}
|
||||
|
||||
func init() {
|
||||
check.Suite(&CheckersS{})
|
||||
}
|
||||
|
||||
type CheckersS struct{}
|
||||
|
||||
var _ = check.Suite(&CheckersS{})
|
||||
|
||||
func testInfo(c *check.C, checker check.Checker, name string, paramNames []string) {
|
||||
info := checker.Info()
|
||||
if info.Name != name {
|
||||
c.Fatalf("Got name %s, expected %s", info.Name, name)
|
||||
}
|
||||
if !reflect.DeepEqual(info.Params, paramNames) {
|
||||
c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
|
||||
}
|
||||
}
|
||||
|
||||
func testCheck(c *check.C, checker check.Checker, expectedResult bool, expectedError string, params ...interface{}) ([]interface{}, []string) {
|
||||
info := checker.Info()
|
||||
if len(params) != len(info.Params) {
|
||||
c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
|
||||
}
|
||||
names := append([]string{}, info.Params...)
|
||||
result, error := checker.Check(params, names)
|
||||
if result != expectedResult || error != expectedError {
|
||||
c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
|
||||
info.Name, params, result, error, expectedResult, expectedError)
|
||||
}
|
||||
return params, names
|
||||
}
|
||||
|
||||
func (s *CheckersS) TestContains(c *check.C) {
|
||||
testInfo(c, Contains, "Contains", []string{"value", "substring"})
|
||||
|
||||
testCheck(c, Contains, true, "", "abcd", "bc")
|
||||
testCheck(c, Contains, false, "", "abcd", "efg")
|
||||
testCheck(c, Contains, false, "", "", "bc")
|
||||
testCheck(c, Contains, true, "", "abcd", "")
|
||||
testCheck(c, Contains, true, "", "", "")
|
||||
|
||||
testCheck(c, Contains, false, "Obtained value is not a string and has no .String()", 12, "1")
|
||||
testCheck(c, Contains, false, "Substring must be a string", "", 1)
|
||||
}
|
Loading…
Reference in a new issue