SuperMapOf operator compares the contents of a map against the non-zero
values of model (if any) and the values of expectedEntries.
model must be the same type as compared data.
expectedEntries can be omitted, if no TestDeep operators are
involved. If expectedEntries contains more than one item, all items
are merged before their use, from left to right.
During a match, each expected entry should match in the compared
map. But some entries in the compared map may not be expected.
got:=map[string]string{"foo":"test","bar":"wizz","zip":"buzz",}td.Cmp(t,got,td.SuperMapOf(map[string]string{"foo":"test",},td.MapEntries{"zip":td.HasSuffix("zz"),}),)// succeedstd.Cmp(t,got,td.SuperMapOf(map[string]string{"foo":"test",},td.MapEntries{"biz":td.HasSuffix("zz"),}),)// fails, missing {"biz": …} in got
t:=&testing.T{}got:=map[string]int{"foo":12,"bar":42,"zip":89}ok:=td.Cmp(t,got,td.SuperMapOf(map[string]int{"bar":42},td.MapEntries{"foo":td.Lt(15)}),"checks map %v contains at least all expected keys/values",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.SuperMapOf(MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)}),"checks typed map %v contains at least all expected keys/values",got)fmt.Println(ok)ok=td.Cmp(t,&got,td.SuperMapOf(&MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)}),"checks pointed typed map %v contains at least all expected keys/values",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.CmpSuperMapOf(t,got,map[string]int{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks map %v contains at least all expected keys/values",got)fmt.Println(ok)// Output:// true
TypedMap example
t:=&testing.T{}typeMyMapmap[string]intgot:=MyMap{"foo":12,"bar":42,"zip":89}ok:=td.CmpSuperMapOf(t,got,MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks typed map %v contains at least all expected keys/values",got)fmt.Println(ok)ok=td.CmpSuperMapOf(t,&got,&MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks pointed typed map %v contains at least all expected keys/values",got)fmt.Println(ok)// Output:// true// true
SuperMapOf optional parameter expectedEntries is here mandatory.
nil value should be passed to mimic its absence in
original SuperMapOf call.
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.SuperMapOf(got,map[string]int{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks map %v contains at least all expected keys/values",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.SuperMapOf(got,MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks typed map %v contains at least all expected keys/values",got)fmt.Println(ok)ok=t.SuperMapOf(&got,&MyMap{"bar":42},td.MapEntries{"foo":td.Lt(15)},"checks pointed typed map %v contains at least all expected keys/values",got)fmt.Println(ok)// Output:// true// true