Shallow operator compares pointers only, not their contents. It
applies on channels, functions (with some restrictions), maps,
pointers, slices and strings.
During a match, the compared data must be the same as expectedPtr
to succeed.
a,b:=123,123td.Cmp(t,&a,td.Shallow(&a))// succeedstd.Cmp(t,&a,td.Shallow(&b))// fails even if a == b as &a != &bback:="foobarfoobar"a,b:=back[:6],back[6:]// a == b but...td.Cmp(t,&a,td.Shallow(&b))// fails
Be careful for slices and strings! Shallow can succeed but the
slices/strings not be identical because of their different
lengths. For example:
a:="foobar yes!"b:=a[:1]// aka "f"td.Cmp(t,&a,td.Shallow(&b))// succeeds as both strings point to the same area, even if len() differ
The same behavior occurs for slices:
a:=[]int{1,2,3,4,5,6}b:=a[:2]// aka []int{1, 2}td.Cmp(t,&a,td.Shallow(&b))// succeeds as both slices point to the same area, even if len() differ
t:=&testing.T{}typeMyStructstruct{Valueint}data:=MyStruct{Value:12}got:=&dataok:=td.Cmp(t,got,td.Shallow(&data),"checks pointers only, not contents")fmt.Println(ok)// Same contents, but not same pointerok=td.Cmp(t,got,td.Shallow(&MyStruct{Value:12}),"checks pointers only, not contents")fmt.Println(ok)// Output:// true// false
Slice example
t:=&testing.T{}back:=[]int{1,2,3,1,2,3}a:=back[:3]b:=back[3:]ok:=td.Cmp(t,a,td.Shallow(back))fmt.Println("are ≠ but share the same area:",ok)ok=td.Cmp(t,b,td.Shallow(back))fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false
String example
t:=&testing.T{}back:="foobarfoobar"a:=back[:6]b:=back[6:]ok:=td.Cmp(t,a,td.Shallow(back))fmt.Println("are ≠ but share the same area:",ok)ok=td.Cmp(t,b,td.Shallow(a))fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false
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{}typeMyStructstruct{Valueint}data:=MyStruct{Value:12}got:=&dataok:=td.CmpShallow(t,got,&data,"checks pointers only, not contents")fmt.Println(ok)// Same contents, but not same pointerok=td.CmpShallow(t,got,&MyStruct{Value:12},"checks pointers only, not contents")fmt.Println(ok)// Output:// true// false
Slice example
t:=&testing.T{}back:=[]int{1,2,3,1,2,3}a:=back[:3]b:=back[3:]ok:=td.CmpShallow(t,a,back)fmt.Println("are ≠ but share the same area:",ok)ok=td.CmpShallow(t,b,back)fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false
String example
t:=&testing.T{}back:="foobarfoobar"a:=back[:6]b:=back[6:]ok:=td.CmpShallow(t,a,back)fmt.Println("are ≠ but share the same area:",ok)ok=td.CmpShallow(t,b,a)fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false
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{})typeMyStructstruct{Valueint}data:=MyStruct{Value:12}got:=&dataok:=t.Shallow(got,&data,"checks pointers only, not contents")fmt.Println(ok)// Same contents, but not same pointerok=t.Shallow(got,&MyStruct{Value:12},"checks pointers only, not contents")fmt.Println(ok)// Output:// true// false
Slice example
t:=td.NewT(&testing.T{})back:=[]int{1,2,3,1,2,3}a:=back[:3]b:=back[3:]ok:=t.Shallow(a,back)fmt.Println("are ≠ but share the same area:",ok)ok=t.Shallow(b,back)fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false
String example
t:=td.NewT(&testing.T{})back:="foobarfoobar"a:=back[:6]b:=back[6:]ok:=t.Shallow(a,back)fmt.Println("are ≠ but share the same area:",ok)ok=t.Shallow(b,a)fmt.Println("are = but do not point to same area:",ok)// Output:// are ≠ but share the same area: true// are = but do not point to same area: false