Debian Codegraph @

td/td_ptr.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 "reflect"
11
12 "github.com/maxatome/go-testdeep/internal/ctxerr"
13 "github.com/maxatome/go-testdeep/internal/types"
14 )
15
16 type tdPtr struct {
17 tdSmugglerBase
18 }
19
20 var _ TestDeep = &tdPtr{}
21
22 // summary(Ptr): allows to easily test a pointer value
23 // input(Ptr): ptr
24
25 // Ptr is a smuggler operator. It takes the address of data and
26 // compares it to val.
27 //
28 // val depends on data type. For example, if the compared data is an
29 // *int, one can have:
30 //
31 // num := 12
32 // td.Cmp(t, &num, td.Ptr(12)) // succeeds
33 //
34 // as well as an other operator:
35 //
36 // num := 3
37 // td.Cmp(t, &num, td.Ptr(td.Between(3, 4)))
38 //
39 // TypeBehind method returns the [reflect.Type] of a pointer on val,
40 // except if val is a [TestDeep] operator. In this case, it delegates
41 // TypeBehind() to the operator and returns the [reflect.Type] of a
42 // pointer on the returned value (if non-nil of course).
43 //
44 // See also [PPtr] and [Shallow].
45 func Ptr(val any) TestDeep {
46 p := tdPtr{
47 tdSmugglerBase: newSmugglerBase(val),
48 }
49
50 vval := reflect.ValueOf(val)
51 if !vval.IsValid() {
52 p.err = ctxerr.OpBadUsage("Ptr", "(NON_NIL_VALUE)", val, 1, true)
53 return &p
54 }
55
56 if !p.isTestDeeper {
57 p.expectedValue = reflect.New(vval.Type())
58 p.expectedValue.Elem().Set(vval)
59 }
60 return &p
61 }
62
63 func (p *tdPtr) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
64 if p.err != nil {
65 return ctx.CollectError(p.err)
66 }
67
68 if got.Kind() != reflect.Ptr {
69 if ctx.BooleanError {
70 return ctxerr.BooleanError
71 }
72 return ctx.CollectError(&ctxerr.Error{
73 Message: "pointer type mismatch",
74 Got: types.RawString(got.Type().String()),
75 Expected: types.RawString(p.String()),
76 })
77 }
78
79 if p.isTestDeeper {
80 return deepValueEqual(ctx.AddPtr(1), got.Elem(), p.expectedValue)
81 }
82 return deepValueEqual(ctx, got, p.expectedValue)
83 }
84
85 func (p *tdPtr) String() string {
86 if p.err != nil {
87 return p.stringError()
88 }
89
90 if p.isTestDeeper {
91 return "*<something>"
92 }
93 return p.expectedValue.Type().String()
94 }
95
96 func (p *tdPtr) TypeBehind() reflect.Type {
97 if p.err != nil {
98 return nil
99 }
100
101 // If the expected value is a TestDeep operator, delegate TypeBehind to it
102 if p.isTestDeeper {
103 typ := p.expectedValue.Interface().(TestDeep).TypeBehind()
104 if typ == nil {
105 return nil
106 }
107 // Add a level of pointer
108 return reflect.New(typ).Type()
109 }
110 return p.expectedValue.Type()
111 }
112
113 type tdPPtr struct {
114 tdSmugglerBase
115 }
116
117 var _ TestDeep = &tdPPtr{}
118
119 // summary(PPtr): allows to easily test a pointer of pointer value
120 // input(PPtr): ptr
121
122 // PPtr is a smuggler operator. It takes the address of the address of
123 // data and compares it to val.
124 //
125 // val depends on data type. For example, if the compared data is an
126 // **int, one can have:
127 //
128 // num := 12
129 // pnum = &num
130 // td.Cmp(t, &pnum, td.PPtr(12)) // succeeds
131 //
132 // as well as an other operator:
133 //
134 // num := 3
135 // pnum = &num
136 // td.Cmp(t, &pnum, td.PPtr(td.Between(3, 4))) // succeeds
137 //
138 // It is more efficient and shorter to write than:
139 //
140 // td.Cmp(t, &pnum, td.Ptr(td.Ptr(val))) // succeeds too
141 //
142 // TypeBehind method returns the [reflect.Type] of a pointer on a
143 // pointer on val, except if val is a [TestDeep] operator. In this
144 // case, it delegates TypeBehind() to the operator and returns the
145 // [reflect.Type] of a pointer on a pointer on the returned value (if
146 // non-nil of course).
147 //
148 // See also [Ptr].
149 func PPtr(val any) TestDeep {
150 p := tdPPtr{
151 tdSmugglerBase: newSmugglerBase(val),
152 }
153
154 vval := reflect.ValueOf(val)
155 if !vval.IsValid() {
156 p.err = ctxerr.OpBadUsage("PPtr", "(NON_NIL_VALUE)", val, 1, true)
157 return &p
158 }
159
160 if !p.isTestDeeper {
161 pVval := reflect.New(vval.Type())
162 pVval.Elem().Set(vval)
163
164 p.expectedValue = reflect.New(pVval.Type())
165 p.expectedValue.Elem().Set(pVval)
166 }
167 return &p
168 }
169
170 func (p *tdPPtr) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
171 if p.err != nil {
172 return ctx.CollectError(p.err)
173 }
174
175 if got.Kind() != reflect.Ptr || got.Elem().Kind() != reflect.Ptr {
176 if ctx.BooleanError {
177 return ctxerr.BooleanError
178 }
179 return ctx.CollectError(&ctxerr.Error{
180 Message: "pointer type mismatch",
181 Got: types.RawString(got.Type().String()),
182 Expected: types.RawString(p.String()),
183 })
184 }
185
186 if p.isTestDeeper {
187 return deepValueEqual(ctx.AddPtr(2), got.Elem().Elem(), p.expectedValue)
188 }
189 return deepValueEqual(ctx, got, p.expectedValue)
190 }
191
192 func (p *tdPPtr) String() string {
193 if p.err != nil {
194 return p.stringError()
195 }
196
197 if p.isTestDeeper {
198 return "**<something>"
199 }
200 return p.expectedValue.Type().String()
201 }
202
203 func (p *tdPPtr) TypeBehind() reflect.Type {
204 if p.err != nil {
205 return nil
206 }
207
208 // If the expected value is a TestDeep operator, delegate TypeBehind to it
209 if p.isTestDeeper {
210 typ := p.expectedValue.Interface().(TestDeep).TypeBehind()
211 if typ == nil {
212 return nil
213 }
214 // Add 2 levels of pointer
215 return reflect.New(reflect.New(typ).Type()).Type()
216 }
217 return p.expectedValue.Type()
218 }