Grep 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, and produces a
slice consisting of those items for which the filter matched and
compares it to expectedValue. The filter matches when it is a:
t := &testing.T{}
got := []int{-3, -2, -1, 0, 1, 2, 3}
ok := td.Cmp(t, got, td.Grep(td.Gt(0), []int{1, 2, 3}))
fmt.Println("check positive numbers:", ok)
isEven := func(x int) bool { return x%2 == 0 }
ok = td.Cmp(t, got, td.Grep(isEven, []int{-2, 0, 2}))
fmt.Println("even numbers are -2, 0 and 2:", ok)
ok = td.Cmp(t, got, td.Grep(isEven, td.Set(0, 2, -2)))
fmt.Println("even numbers are also 0, 2 and -2:", ok)
ok = td.Cmp(t, got, td.Grep(isEven, td.ArrayEach(td.Code(isEven))))
fmt.Println("even numbers are each even:", ok)
// Output:// check positive numbers: true// even numbers are -2, 0 and 2: true// even numbers are also 0, 2 and -2: true// even numbers are each even: true
Nil example
t := &testing.T{}
var got []int ok := td.Cmp(t, got, td.Grep(td.Gt(0), ([]int)(nil)))
fmt.Println("typed []int nil:", ok)
ok = td.Cmp(t, got, td.Grep(td.Gt(0), ([]string)(nil)))
fmt.Println("typed []string nil:", ok)
ok = td.Cmp(t, got, td.Grep(td.Gt(0), td.Nil()))
fmt.Println("td.Nil:", ok)
ok = td.Cmp(t, got, td.Grep(td.Gt(0), []int{}))
fmt.Println("empty non-nil slice:", ok)
// Output:// typed []int nil: true// typed []string nil: false// td.Nil: true// empty non-nil slice: false
Struct example
t := &testing.T{}
type Person struct {
Fullname string`json:"fullname"` Age int`json:"age"` }
got := []*Person{
{
Fullname: "Bob Foobar",
Age: 42,
},
{
Fullname: "Alice Bingo",
Age: 27,
},
}
ok := td.Cmp(t, got, td.Grep(
td.Smuggle("Age", td.Gt(30)),
td.All(
td.Len(1),
td.ArrayEach(td.Smuggle("Fullname", "Bob Foobar")),
)))
fmt.Println("person.Age > 30 β only Bob:", ok)
ok = td.Cmp(t, got, td.Grep(
td.JSONPointer("/age", td.Gt(30)),
td.JSON(`[ SuperMapOf({"fullname":"Bob Foobar"}) ]`)))
fmt.Println("person.Age > 30 β only Bob, using JSON:", ok)
// Output:// person.Age > 30 β only Bob: true// person.Age > 30 β only Bob, using JSON: true
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.
t := &testing.T{}
got := []int{-3, -2, -1, 0, 1, 2, 3}
ok := td.CmpGrep(t, got, td.Gt(0), []int{1, 2, 3})
fmt.Println("check positive numbers:", ok)
isEven := func(x int) bool { return x%2 == 0 }
ok = td.CmpGrep(t, got, isEven, []int{-2, 0, 2})
fmt.Println("even numbers are -2, 0 and 2:", ok)
ok = td.CmpGrep(t, got, isEven, td.Set(0, 2, -2))
fmt.Println("even numbers are also 0, 2 and -2:", ok)
ok = td.CmpGrep(t, got, isEven, td.ArrayEach(td.Code(isEven)))
fmt.Println("even numbers are each even:", ok)
// Output:// check positive numbers: true// even numbers are -2, 0 and 2: true// even numbers are also 0, 2 and -2: true// even numbers are each even: true
Nil example
t := &testing.T{}
var got []int ok := td.CmpGrep(t, got, td.Gt(0), ([]int)(nil))
fmt.Println("typed []int nil:", ok)
ok = td.CmpGrep(t, got, td.Gt(0), ([]string)(nil))
fmt.Println("typed []string nil:", ok)
ok = td.CmpGrep(t, got, td.Gt(0), td.Nil())
fmt.Println("td.Nil:", ok)
ok = td.CmpGrep(t, got, td.Gt(0), []int{})
fmt.Println("empty non-nil slice:", ok)
// Output:// typed []int nil: true// typed []string nil: false// td.Nil: true// empty non-nil slice: false
Struct example
t := &testing.T{}
type Person struct {
Fullname string`json:"fullname"` Age int`json:"age"` }
got := []*Person{
{
Fullname: "Bob Foobar",
Age: 42,
},
{
Fullname: "Alice Bingo",
Age: 27,
},
}
ok := td.CmpGrep(t, got, td.Smuggle("Age", td.Gt(30)), td.All(
td.Len(1),
td.ArrayEach(td.Smuggle("Fullname", "Bob Foobar")),
))
fmt.Println("person.Age > 30 β only Bob:", ok)
ok = td.CmpGrep(t, got, td.JSONPointer("/age", td.Gt(30)), td.JSON(`[ SuperMapOf({"fullname":"Bob Foobar"}) ]`))
fmt.Println("person.Age > 30 β only Bob, using JSON:", ok)
// Output:// person.Age > 30 β only Bob: true// person.Age > 30 β only Bob, using JSON: true
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.
t := td.NewT(&testing.T{})
got := []int{-3, -2, -1, 0, 1, 2, 3}
ok := t.Grep(got, td.Gt(0), []int{1, 2, 3})
fmt.Println("check positive numbers:", ok)
isEven := func(x int) bool { return x%2 == 0 }
ok = t.Grep(got, isEven, []int{-2, 0, 2})
fmt.Println("even numbers are -2, 0 and 2:", ok)
ok = t.Grep(got, isEven, td.Set(0, 2, -2))
fmt.Println("even numbers are also 0, 2 and -2:", ok)
ok = t.Grep(got, isEven, td.ArrayEach(td.Code(isEven)))
fmt.Println("even numbers are each even:", ok)
// Output:// check positive numbers: true// even numbers are -2, 0 and 2: true// even numbers are also 0, 2 and -2: true// even numbers are each even: true
Nil example
t := td.NewT(&testing.T{})
var got []int ok := t.Grep(got, td.Gt(0), ([]int)(nil))
fmt.Println("typed []int nil:", ok)
ok = t.Grep(got, td.Gt(0), ([]string)(nil))
fmt.Println("typed []string nil:", ok)
ok = t.Grep(got, td.Gt(0), td.Nil())
fmt.Println("td.Nil:", ok)
ok = t.Grep(got, td.Gt(0), []int{})
fmt.Println("empty non-nil slice:", ok)
// Output:// typed []int nil: true// typed []string nil: false// td.Nil: true// empty non-nil slice: false
Struct example
t := td.NewT(&testing.T{})
type Person struct {
Fullname string`json:"fullname"` Age int`json:"age"` }
got := []*Person{
{
Fullname: "Bob Foobar",
Age: 42,
},
{
Fullname: "Alice Bingo",
Age: 27,
},
}
ok := t.Grep(got, td.Smuggle("Age", td.Gt(30)), td.All(
td.Len(1),
td.ArrayEach(td.Smuggle("Fullname", "Bob Foobar")),
))
fmt.Println("person.Age > 30 β only Bob:", ok)
ok = t.Grep(got, td.JSONPointer("/age", td.Gt(30)), td.JSON(`[ SuperMapOf({"fullname":"Bob Foobar"}) ]`))
fmt.Println("person.Age > 30 β only Bob, using JSON:", ok)
// Output:// person.Age > 30 β only Bob: true// person.Age > 30 β only Bob, using JSON: true