HasPrefix

func HasPrefix(expected string) TestDeep

HasPrefix operator allows to compare the prefix of a string (or convertible), []byte (or convertible), error or fmt.Stringer interface (error interface is tested before fmt.Stringer).

td.Cmp(t, []byte("foobar"), td.HasPrefix("foo")) // succeeds

type Foobar string
td.Cmp(t, Foobar("foobar"), td.HasPrefix("foo")) // succeeds

err := errors.New("error!")
td.Cmp(t, err, td.HasPrefix("err")) // succeeds

bstr := bytes.NewBufferString("fmt.Stringer!")
td.Cmp(t, bstr, td.HasPrefix("fmt")) // succeeds

See also Contains, HasSuffix, Re, ReAll and String.

See also HasPrefix godoc.

Examples

Base example
Stringer example
Error example

CmpHasPrefix shortcut

func CmpHasPrefix(t TestingT, got any, expected string, args ...any) bool

CmpHasPrefix is a shortcut for:

td.Cmp(t, got, td.HasPrefix(expected), args...)

See above for details.

Returns true if the test is OK, false if it fails.

If t is a *T then its Config field is inherited.

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.

Examples

Base example
Stringer example
Error example

T.HasPrefix shortcut

func (t *T) HasPrefix(got any, expected string, args ...any) bool

HasPrefix is a shortcut for:

t.Cmp(got, td.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.

Examples

Base example
Stringer example
Error example