Last

func Last(filter, expectedValue any) TestDeep

Last is a smuggler operator. It takes an array, a slice or a pointer on array/slice. For each item it applies filter, a TestDeep operator or a function returning a bool. It takes the last item for which the filter matched and compares it to expectedValue. The filter matches when it is a:

  • TestDeep operator and it matches for the item;
  • function receiving the item and it returns true.

expectedValue can of course be a TestDeep operator.

got := []int{-3, -2, -1, 0, 1, 2, 3}
td.Cmp(t, got, td.Last(td.Lt(0), -1))                                   // succeeds
td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, 2))        // succeeds
td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, td.Gt(0))) // succeeds

If the input is empty (and/or nil for a slice), an “item not found” error is raised before comparing to expectedValue.

var got []int
td.Cmp(t, got, td.Last(td.Gt(0), td.Gt(0)))      // fails
td.Cmp(t, []int{}, td.Last(td.Gt(0), td.Gt(0)))  // fails
td.Cmp(t, [0]int{}, td.Last(td.Gt(0), td.Gt(0))) // fails

See also First and Grep.

See also Last godoc.

Examples

Classic example
Empty example
Struct example
Json example

CmpLast shortcut

func CmpLast(t TestingT, got, filter , expectedValue any, args ...any) bool

CmpLast is a shortcut for:

td.Cmp(t, got, td.Last(filter, expectedValue), 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 CmpLast godoc.

Examples

Classic example
Empty example
Struct example

T.Last shortcut

func (t *T) Last(got, filter , expectedValue any, args ...any) bool

Last is a shortcut for:

t.Cmp(got, td.Last(filter, expectedValue), 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.Last godoc.

Examples

Classic example
Empty example
Struct example