Debian Codegraph @

td/td_shallow.go

raw
1 // Copyright (c) 2018, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td
8
9 import (
10 "fmt"
11 "reflect"
12 "unsafe"
13
14 "github.com/maxatome/go-testdeep/internal/ctxerr"
15 "github.com/maxatome/go-testdeep/internal/types"
16 )
17
18 type tdShallow struct {
19 base
20 expectedKind reflect.Kind
21 expectedPointer uintptr
22 expectedStr string // in reflect.String case, to avoid contents GC
23 }
24
25 var _ TestDeep = &tdShallow{}
26
27 func stringPointer(s string) uintptr {
28 return (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
29 }
30
31 // summary(Shallow): compares pointers only, not their contents
32 // input(Shallow): nil,str,slice,map,ptr,chan,func
33
34 // Shallow operator compares pointers only, not their contents. It
35 // applies on channels, functions (with some restrictions), maps,
36 // pointers, slices and strings.
37 //
38 // During a match, the compared data must be the same as expectedPtr
39 // to succeed.
40 //
41 // a, b := 123, 123
42 // td.Cmp(t, &a, td.Shallow(&a)) // succeeds
43 // td.Cmp(t, &a, td.Shallow(&b)) // fails even if a == b as &a != &b
44 //
45 // back := "foobarfoobar"
46 // a, b := back[:6], back[6:]
47 // // a == b but...
48 // td.Cmp(t, &a, td.Shallow(&b)) // fails
49 //
50 // Be careful for slices and strings! Shallow can succeed but the
51 // slices/strings not be identical because of their different
52 // lengths. For example:
53 //
54 // a := "foobar yes!"
55 // b := a[:1] // aka "f"
56 // td.Cmp(t, &a, td.Shallow(&b)) // succeeds as both strings point to the same area, even if len() differ
57 //
58 // The same behavior occurs for slices:
59 //
60 // a := []int{1, 2, 3, 4, 5, 6}
61 // b := a[:2] // aka []int{1, 2}
62 // td.Cmp(t, &a, td.Shallow(&b)) // succeeds as both slices point to the same area, even if len() differ
63 //
64 // See also [Ptr].
65 func Shallow(expectedPtr any) TestDeep {
66 vptr := reflect.ValueOf(expectedPtr)
67
68 shallow := tdShallow{
69 base: newBase(3),
70 expectedKind: vptr.Kind(),
71 }
72
73 // Note from reflect documentation:
74 // If v's Kind is Func, the returned pointer is an underlying code
75 // pointer, but not necessarily enough to identify a single function
76 // uniquely. The only guarantee is that the result is zero if and
77 // only if v is a nil func Value.
78
79 switch shallow.expectedKind {
80 case reflect.Chan,
81 reflect.Func,
82 reflect.Map,
83 reflect.Ptr,
84 reflect.Slice,
85 reflect.UnsafePointer:
86 shallow.expectedPointer = vptr.Pointer()
87
88 case reflect.String:
89 shallow.expectedStr = vptr.String()
90 shallow.expectedPointer = stringPointer(shallow.expectedStr)
91
92 default:
93 shallow.err = ctxerr.OpBadUsage(
94 "Shallow", "(CHANNEL|FUNC|MAP|PTR|SLICE|UNSAFE_PTR|STRING)",
95 expectedPtr, 1, true)
96 }
97
98 return &shallow
99 }
100
101 func (s *tdShallow) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
102 if s.err != nil {
103 return ctx.CollectError(s.err)
104 }
105
106 if got.Kind() != s.expectedKind {
107 if ctx.BooleanError {
108 return ctxerr.BooleanError
109 }
110 return ctx.CollectError(ctxerr.BadKind(got, s.expectedKind.String()))
111 }
112
113 var ptr uintptr
114
115 // Special case for strings
116 if s.expectedKind == reflect.String {
117 ptr = stringPointer(got.String())
118 } else {
119 ptr = got.Pointer()
120 }
121
122 if ptr != s.expectedPointer {
123 if ctx.BooleanError {
124 return ctxerr.BooleanError
125 }
126 return ctx.CollectError(&ctxerr.Error{
127 Message: fmt.Sprintf("%s pointer mismatch", s.expectedKind),
128 Got: types.RawString(fmt.Sprintf("0x%x", ptr)),
129 Expected: types.RawString(fmt.Sprintf("0x%x", s.expectedPointer)),
130 })
131 }
132 return nil
133 }
134
135 func (s *tdShallow) String() string {
136 if s.err != nil {
137 return s.stringError()
138 }
139
140 return fmt.Sprintf("(%s) 0x%x", s.expectedKind, s.expectedPointer)
141 }