Shallow
func Shallow(expectedPtr any) TestDeep
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, 123
td.Cmp(t, &a, td.Shallow(&a)) // succeeds
td.Cmp(t, &a, td.Shallow(&b)) // fails even if a == b as &a != &b
back := "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
See also Ptr
.
See also Shallow godoc.
Examples
Base example
t := &testing.T{}
type MyStruct struct {
Value int
}
data := MyStruct{Value: 12}
got := &data
ok := td.Cmp(t, got, td.Shallow(&data),
"checks pointers only, not contents")
fmt.Println(ok)
// Same contents, but not same pointer
ok = 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
CmpShallow shortcut
func CmpShallow(t TestingT, got, expectedPtr any, args ...any) bool
CmpShallow is a shortcut for:
td.Cmp(t, got, td.Shallow(expectedPtr), args...)
See above for details.
Returns true if the test is OK, false if it fails.
If t is a *T
then its Config field is inherited.
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.
See also CmpShallow godoc.
Examples
Base example
t := &testing.T{}
type MyStruct struct {
Value int
}
data := MyStruct{Value: 12}
got := &data
ok := td.CmpShallow(t, got, &data,
"checks pointers only, not contents")
fmt.Println(ok)
// Same contents, but not same pointer
ok = 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
T.Shallow shortcut
func (t *T) Shallow(got, expectedPtr any, args ...any) bool
Shallow is a shortcut for:
t.Cmp(got, td.Shallow(expectedPtr), 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.
See also T.Shallow godoc.
Examples
Base example
t := td.NewT(&testing.T{})
type MyStruct struct {
Value int
}
data := MyStruct{Value: 12}
got := &data
ok := t.Shallow(got, &data,
"checks pointers only, not contents")
fmt.Println(ok)
// Same contents, but not same pointer
ok = 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