MapEach operator has to be applied on maps. It compares each value
of data map against expectedValue. During a match, all values have
to match to succeed.
got:=map[string]string{"test":"foo","buzz":"bar"}td.Cmp(t,got,td.MapEach("bar"))// fails, coz "foo" ≠ "bar"td.Cmp(t,got,td.MapEach(td.Len(3)))// succeeds as values are 3 chars long
t:=&testing.T{}got:=map[string]int{"foo":12,"bar":42,"zip":89}ok:=td.Cmp(t,got,td.MapEach(td.Between(10,90)),"checks each value of map %v is in [10 .. 90]",got)fmt.Println(ok)// Output:// true
TypedMap example
t:=&testing.T{}typeMyMapmap[string]intgot:=MyMap{"foo":12,"bar":42,"zip":89}ok:=td.Cmp(t,got,td.MapEach(td.Between(10,90)),"checks each value of typed map %v is in [10 .. 90]",got)fmt.Println(ok)ok=td.Cmp(t,&got,td.MapEach(td.Between(10,90)),"checks each value of typed map pointer %v is in [10 .. 90]",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:=map[string]int{"foo":12,"bar":42,"zip":89}ok:=td.CmpMapEach(t,got,td.Between(10,90),"checks each value of map %v is in [10 .. 90]",got)fmt.Println(ok)// Output:// true
TypedMap example
t:=&testing.T{}typeMyMapmap[string]intgot:=MyMap{"foo":12,"bar":42,"zip":89}ok:=td.CmpMapEach(t,got,td.Between(10,90),"checks each value of typed map %v is in [10 .. 90]",got)fmt.Println(ok)ok=td.CmpMapEach(t,&got,td.Between(10,90),"checks each value of typed map pointer %v is in [10 .. 90]",got)fmt.Println(ok)// Output:// true// 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:=map[string]int{"foo":12,"bar":42,"zip":89}ok:=t.MapEach(got,td.Between(10,90),"checks each value of map %v is in [10 .. 90]",got)fmt.Println(ok)// Output:// true
TypedMap example
t:=td.NewT(&testing.T{})typeMyMapmap[string]intgot:=MyMap{"foo":12,"bar":42,"zip":89}ok:=t.MapEach(got,td.Between(10,90),"checks each value of typed map %v is in [10 .. 90]",got)fmt.Println(ok)ok=t.MapEach(&got,td.Between(10,90),"checks each value of typed map pointer %v is in [10 .. 90]",got)fmt.Println(ok)// Output:// true// true