func HasPrefix(expected string) TestDeep
HasPrefix
operator allows to compare the prefix of a string
(or
convertible), error
or fmt.Stringer
interface (error
interface is
tested before fmt.Stringer
.)
type Foobar string
Cmp(t, Foobar("foobar"), HasPrefix("foo")) // succeeds
err := errors.New("error!")
Cmp(t, err, HasPrefix("err")) // succeeds
bstr := bytes.NewBufferString("fmt.Stringer!")
Cmp(t, bstr, HasPrefix("fmt")) // succeeds
See also HasPrefix godoc.
t := &testing.T{}
got := "foobar"
ok := Cmp(t, got, HasPrefix("foo"), "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := &testing.T{}
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := Cmp(t, got, HasPrefix("foo"), "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := &testing.T{}
got := errors.New("foobar")
ok := Cmp(t, got, HasPrefix("foo"), "checks %s", got)
fmt.Println(ok)
// Output:
// true
func CmpHasPrefix(t TestingT, got interface{}, expected string, args ...interface{}) bool
CmpHasPrefix is a shortcut for:
Cmp(t, got, HasPrefix(expected), args...)
See above for details.
Returns true if the test is OK, false if it fails.
args… are optional and allow to name the test. This name is
used in case of failure to qualify the test. If len(args) > 1
and
the first item of args is a string
and contains a ‘%’ rune
then
fmt.Fprintf
is used to compose the name, else args are passed to
fmt.Fprint
. Do not forget it is the name of the test, not the
reason of a potential failure.
See also CmpHasPrefix godoc.
t := &testing.T{}
got := "foobar"
ok := CmpHasPrefix(t, got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := &testing.T{}
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := CmpHasPrefix(t, got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := &testing.T{}
got := errors.New("foobar")
ok := CmpHasPrefix(t, got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true
func (t *T) HasPrefix(got interface{}, expected string, args ...interface{}) bool
HasPrefix
is a shortcut for:
t.Cmp(got, HasPrefix(expected), args...)
See above for details.
Returns true if the test is OK, false if it fails.
args… are optional and allow to name the test. This name is
used in case of failure to qualify the test. If len(args) > 1
and
the first item of args is a string
and contains a ‘%’ rune
then
fmt.Fprintf
is used to compose the name, else args are passed to
fmt.Fprint
. Do not forget it is the name of the test, not the
reason of a potential failure.
See also T.HasPrefix godoc.
t := NewT(&testing.T{})
got := "foobar"
ok := t.HasPrefix(got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := NewT(&testing.T{})
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := t.HasPrefix(got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true
t := NewT(&testing.T{})
got := errors.New("foobar")
ok := t.HasPrefix(got, "foo", "checks %s", got)
fmt.Println(ok)
// Output:
// true