First

func First(filter, expectedValue any) TestDeep

First 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 first 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.First(td.Gt(0), 1))                                    // succeeds
td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, -2))       // succeeds
td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, td.Lt(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.First(td.Gt(0), td.Gt(0)))      // fails
td.Cmp(t, []int{}, td.First(td.Gt(0), td.Gt(0)))  // fails
td.Cmp(t, [0]int{}, td.First(td.Gt(0), td.Gt(0))) // fails

See also Last and Grep.

See also First godoc.

Examples

Classic example
Empty example
Struct example
Json example

CmpFirst shortcut

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

CmpFirst is a shortcut for:

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

Examples

Classic example
Empty example
Struct example

T.First shortcut

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

First is a shortcut for:

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

Examples

Classic example
Empty example
Struct example