HasSuffix
func HasSuffix(expected string) TestDeep
HasSuffix operator allows to compare the suffix 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.HasSuffix("bar")) // succeeds
type Foobar string
td.Cmp(t, Foobar("foobar"), td.HasSuffix("bar")) // succeeds
err := errors.New("error!")
td.Cmp(t, err, td.HasSuffix("!")) // succeeds
bstr := bytes.NewBufferString("fmt.Stringer!")
td.Cmp(t, bstr, td.HasSuffix("!")) // succeeds
See also Contains
, HasPrefix
, Re
, ReAll
and String
.
See also HasSuffix godoc.
Examples
Base example
t := &testing.T{}
got := "foobar"
ok := td.Cmp(t, got, td.HasSuffix("bar"), "checks %s", got)
fmt.Println("using string:", ok)
ok = td.Cmp(t, []byte(got), td.HasSuffix("bar"), "checks %s", got)
fmt.Println("using []byte:", ok)
// Output:
// using string: true
// using []byte: true
Stringer example
t := &testing.T{}
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := td.Cmp(t, got, td.HasSuffix("bar"), "checks %s", got)
fmt.Println(ok)
// Output:
// true
Error example
t := &testing.T{}
got := errors.New("foobar")
ok := td.Cmp(t, got, td.HasSuffix("bar"), "checks %s", got)
fmt.Println(ok)
// Output:
// true
CmpHasSuffix shortcut
func CmpHasSuffix(t TestingT, got any, expected string, args ...any) bool
CmpHasSuffix is a shortcut for:
td.Cmp(t, got, td.HasSuffix(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 CmpHasSuffix godoc.
Examples
Base example
t := &testing.T{}
got := "foobar"
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got)
fmt.Println("using string:", ok)
ok = td.Cmp(t, []byte(got), td.HasSuffix("bar"), "checks %s", got)
fmt.Println("using []byte:", ok)
// Output:
// using string: true
// using []byte: true
Stringer example
t := &testing.T{}
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got)
fmt.Println(ok)
// Output:
// true
Error example
t := &testing.T{}
got := errors.New("foobar")
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got)
fmt.Println(ok)
// Output:
// true
T.HasSuffix shortcut
func (t *T) HasSuffix(got any, expected string, args ...any) bool
HasSuffix is a shortcut for:
t.Cmp(got, td.HasSuffix(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.HasSuffix godoc.
Examples
Base example
t := td.NewT(&testing.T{})
got := "foobar"
ok := t.HasSuffix(got, "bar", "checks %s", got)
fmt.Println("using string:", ok)
ok = t.Cmp([]byte(got), td.HasSuffix("bar"), "checks %s", got)
fmt.Println("using []byte:", ok)
// Output:
// using string: true
// using []byte: true
Stringer example
t := td.NewT(&testing.T{})
// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")
ok := t.HasSuffix(got, "bar", "checks %s", got)
fmt.Println(ok)
// Output:
// true
Error example
t := td.NewT(&testing.T{})
got := errors.New("foobar")
ok := t.HasSuffix(got, "bar", "checks %s", got)
fmt.Println(ok)
// Output:
// true