Debian Codegraph @

td/types.go

raw
1 // Copyright (c) 2018-2021, 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 "strings"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/ctxerr"
15 "github.com/maxatome/go-testdeep/internal/location"
16 "github.com/maxatome/go-testdeep/internal/types"
17 )
18
19 const stringErrorParam = "(<ERROR>)"
20
21 var (
22 tType = reflect.TypeOf((*T)(nil))
23 testDeeper = reflect.TypeOf((*TestDeep)(nil)).Elem()
24 smuggledGotType = reflect.TypeOf(SmuggledGot{})
25 smuggledGotPtrType = reflect.TypeOf((*SmuggledGot)(nil))
26 recvKindType = reflect.TypeOf(RecvNothing)
27 )
28
29 // TestingT is the minimal interface used by [Cmp] to report errors. It
30 // is commonly implemented by [*testing.T] and [*testing.B].
31 type TestingT interface {
32 Error(args ...any)
33 Fatal(args ...any)
34 Helper()
35 }
36
37 // TestingFT is a deprecated alias of [testing.TB]. Use [testing.TB]
38 // directly in new code.
39 type TestingFT = testing.TB
40
41 // TestDeep is the representation of a [go-testdeep operator]. It is not
42 // intended to be used directly, but through Cmp* functions.
43 //
44 // [go-testdeep operator]: https://go-testdeep.zetta.rocks/operators/
45 type TestDeep interface {
46 types.TestDeepStringer
47 location.GetLocationer
48 // Match checks got against the operator. It returns nil if it matches.
49 Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error
50 setLocation(int)
51 replaceLocation(location.Location)
52 // HandleInvalid returns true if the operator is able to handle
53 // untyped nil value. Otherwise the untyped nil value is handled
54 // generically.
55 HandleInvalid() bool
56 // TypeBehind returns the type handled by the operator or nil if it
57 // is not known. tdhttp helper uses it to know how to unmarshal HTTP
58 // responses bodies before comparing them using the operator.
59 TypeBehind() reflect.Type
60 // Error returns nil if the operator is operational, the
61 // corresponding error otherwise.
62 Error() error
63 }
64
65 // base is a base type providing some methods needed by the TestDeep
66 // interface.
67 type base struct {
68 types.TestDeepStamp
69 location location.Location
70 err *ctxerr.Error
71 }
72
73 func pkgFunc(full string) (string, string) {
74 // the/package.Foo → "the/package", "Foo"
75 // the/package.(*T).Foo → "the/package", "(*T).Foo"
76 // the/package.glob..func1 → "the/package", "glob..func1"
77 sp := strings.LastIndexByte(full, '/')
78 if sp < 0 {
79 sp = 0 // std package without any '/' in name
80 }
81
82 dp := strings.IndexByte(full[sp:], '.')
83 if dp < 0 {
84 return full, ""
85 }
86 dp += sp
87 return full[:dp], full[dp+1:]
88 }
89
90 // setLocation sets location using the stack trace going callDepth levels up.
91 func (t *base) setLocation(callDepth int) {
92 if callDepth < 0 {
93 return
94 }
95 var ok bool
96 t.location, ok = location.New(callDepth)
97 if !ok {
98 t.location.File = "???"
99 t.location.Line = 0
100 return
101 }
102
103 // Here package is github.com/maxatome/go-testdeep, or its vendored
104 // counterpart
105 var pkg string
106 pkg, t.location.Func = pkgFunc(t.location.Func)
107
108 // Try to go one level upper, if we are still in go-testdeep package
109 cmpLoc, ok := location.New(callDepth + 1)
110 if ok {
111 cmpPkg, _ := pkgFunc(cmpLoc.Func)
112 if cmpPkg == pkg {
113 t.location.File = cmpLoc.File
114 t.location.Line = cmpLoc.Line
115 t.location.BehindCmp = true
116 }
117 }
118 }
119
120 // replaceLocation replaces the location by loc.
121 func (t *base) replaceLocation(loc location.Location) {
122 t.location = loc
123 }
124
125 // GetLocation returns a copy of the location.Location where the TestDeep
126 // operator has been created.
127 func (t *base) GetLocation() location.Location {
128 return t.location
129 }
130
131 // HandleInvalid tells go-testdeep internals that this operator does
132 // not handle nil values directly.
133 func (t base) HandleInvalid() bool {
134 return false
135 }
136
137 // TypeBehind returns the type handled by the operator. Only few operators
138 // knows the type they are handling. If they do not know, nil is
139 // returned.
140 func (t base) TypeBehind() reflect.Type {
141 return nil
142 }
143
144 // Error returns nil if the operator is operational, the corresponding
145 // error otherwise.
146 func (t base) Error() error {
147 if t.err == nil {
148 return nil
149 }
150 return t.err
151 }
152
153 // stringError is a convenience method to call in String()
154 // implementations when the operator is in error.
155 func (t base) stringError() string {
156 return t.GetLocation().Func + stringErrorParam
157 }
158
159 // MarshalJSON implements encoding/json.Marshaler only to returns an
160 // error, as a TestDeep operator should never be JSON marshaled. So
161 // it is better to tell the user he/she does a mistake.
162 func (t base) MarshalJSON() ([]byte, error) {
163 return nil, types.OperatorNotJSONMarshallableError(t.location.Func)
164 }
165
166 // newBase returns a new base struct with location.Location set to the
167 // callDepth depth.
168 func newBase(callDepth int) (b base) {
169 b.setLocation(callDepth)
170 return
171 }
172
173 // baseOKNil is a base type providing some methods needed by the TestDeep
174 // interface, for operators handling nil values.
175 type baseOKNil struct {
176 base
177 }
178
179 // HandleInvalid tells go-testdeep internals that this operator
180 // handles nil values directly.
181 func (t baseOKNil) HandleInvalid() bool {
182 return true
183 }
184
185 // newBaseOKNil returns a new baseOKNil struct with location.Location set to
186 // the callDepth depth.
187 func newBaseOKNil(callDepth int) (b baseOKNil) {
188 b.setLocation(callDepth)
189 return
190 }