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(xint)bool{returnx%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
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(xint)bool{returnx%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
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(xint)bool{returnx%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