Len is a smuggler operator. It takes data, applies len() function
on it and compares its result to expectedLen. Of course, the
compared value must be an array, a channel, a map, a slice or a
string.
t:=&testing.T{}got:=[]int{11,22,33}ok:=td.Cmp(t,got,td.Len(3),"checks %v len is 3",got)fmt.Println(ok)ok=td.Cmp(t,got,td.Len(0),"checks %v len is 0",got)fmt.Println(ok)got=nilok=td.Cmp(t,got,td.Len(0),"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
Map example
t:=&testing.T{}got:=map[int]bool{11:true,22:false,33:false}ok:=td.Cmp(t,got,td.Len(3),"checks %v len is 3",got)fmt.Println(ok)ok=td.Cmp(t,got,td.Len(0),"checks %v len is 0",got)fmt.Println(ok)got=nilok=td.Cmp(t,got,td.Len(0),"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
OperatorSlice example
t:=&testing.T{}got:=[]int{11,22,33}ok:=td.Cmp(t,got,td.Len(td.Between(3,8)),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=td.Cmp(t,got,td.Len(td.Lt(5)),"checks %v len is < 5",got)fmt.Println(ok)// Output:// true// true
OperatorMap example
t:=&testing.T{}got:=map[int]bool{11:true,22:false,33:false}ok:=td.Cmp(t,got,td.Len(td.Between(3,8)),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=td.Cmp(t,got,td.Len(td.Gte(3)),"checks %v len is ≥ 3",got)fmt.Println(ok)// Output:// true// 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{11,22,33}ok:=td.CmpLen(t,got,3,"checks %v len is 3",got)fmt.Println(ok)ok=td.CmpLen(t,got,0,"checks %v len is 0",got)fmt.Println(ok)got=nilok=td.CmpLen(t,got,0,"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
Map example
t:=&testing.T{}got:=map[int]bool{11:true,22:false,33:false}ok:=td.CmpLen(t,got,3,"checks %v len is 3",got)fmt.Println(ok)ok=td.CmpLen(t,got,0,"checks %v len is 0",got)fmt.Println(ok)got=nilok=td.CmpLen(t,got,0,"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
OperatorSlice example
t:=&testing.T{}got:=[]int{11,22,33}ok:=td.CmpLen(t,got,td.Between(3,8),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=td.CmpLen(t,got,td.Lt(5),"checks %v len is < 5",got)fmt.Println(ok)// Output:// true// true
OperatorMap example
t:=&testing.T{}got:=map[int]bool{11:true,22:false,33:false}ok:=td.CmpLen(t,got,td.Between(3,8),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=td.CmpLen(t,got,td.Gte(3),"checks %v len is ≥ 3",got)fmt.Println(ok)// Output:// true// true
T.Len shortcut
func(t*T)Len(got,expectedLenany,args...any)bool
Len is a shortcut for:
t.Cmp(got,td.Len(expectedLen),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.
t:=td.NewT(&testing.T{})got:=[]int{11,22,33}ok:=t.Len(got,3,"checks %v len is 3",got)fmt.Println(ok)ok=t.Len(got,0,"checks %v len is 0",got)fmt.Println(ok)got=nilok=t.Len(got,0,"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
Map example
t:=td.NewT(&testing.T{})got:=map[int]bool{11:true,22:false,33:false}ok:=t.Len(got,3,"checks %v len is 3",got)fmt.Println(ok)ok=t.Len(got,0,"checks %v len is 0",got)fmt.Println(ok)got=nilok=t.Len(got,0,"checks %v len is 0",got)fmt.Println(ok)// Output:// true// false// true
OperatorSlice example
t:=td.NewT(&testing.T{})got:=[]int{11,22,33}ok:=t.Len(got,td.Between(3,8),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=t.Len(got,td.Lt(5),"checks %v len is < 5",got)fmt.Println(ok)// Output:// true// true
OperatorMap example
t:=td.NewT(&testing.T{})got:=map[int]bool{11:true,22:false,33:false}ok:=t.Len(got,td.Between(3,8),"checks %v len is in [3 .. 8]",got)fmt.Println(ok)ok=t.Len(got,td.Gte(3),"checks %v len is ≥ 3",got)fmt.Println(ok)// Output:// true// true