Debian Codegraph @

td/doc.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 (from [go-testdeep]) allows extremely flexible deep
8 // comparison, it is built for testing.
9 //
10 // It is a go rewrite and adaptation of wonderful [Test::Deep] perl
11 // module.
12 //
13 // In golang, comparing data structure is usually done using
14 // [reflect.DeepEqual] or using a package that uses this function
15 // behind the scenes.
16 //
17 // This function works very well, but it is not flexible. Both
18 // compared structures must match exactly.
19 //
20 // The purpose of td package is to do its best to introduce this
21 // missing flexibility using ["operators"] when the expected value (or
22 // one of its component) cannot be matched exactly.
23 //
24 // See [go-testdeep] for details.
25 //
26 // For easy HTTP API testing, see the [tdhttp] helper.
27 //
28 // For tests suites also just as easy, see [tdsuite] helper.
29 //
30 // # Example of use
31 //
32 // Imagine a function returning a struct containing a newly created
33 // database record. The Id and the CreatedAt fields are set by the
34 // database layer:
35 //
36 // type Record struct {
37 // Id uint64
38 // Name string
39 // Age int
40 // CreatedAt time.Time
41 // }
42 //
43 // func CreateRecord(name string, age int) (*Record, error) {
44 // // Do INSERT INTO … and return newly created record or error if it failed
45 // }
46 //
47 // # Using standard testing package
48 //
49 // To check the freshly created record contents using standard testing
50 // package, we have to do something like that:
51 //
52 // import (
53 // "testing"
54 // "time"
55 // )
56 //
57 // func TestCreateRecord(t *testing.T) {
58 // before := time.Now().Truncate(time.Second)
59 // record, err := CreateRecord()
60 //
61 // if err != nil {
62 // t.Errorf("An error occurred: %s", err)
63 // } else {
64 // expected := Record{Name: "Bob", Age: 23}
65 //
66 // if record.Id == 0 {
67 // t.Error("Id probably not initialized")
68 // }
69 // if before.After(record.CreatedAt) ||
70 // time.Now().Before(record.CreatedAt) {
71 // t.Errorf("CreatedAt field not expected: %s", record.CreatedAt)
72 // }
73 // if record.Name != expected.Name {
74 // t.Errorf("Name field differs, got=%s, expected=%s",
75 // record.Name, expected.Name)
76 // }
77 // if record.Age != expected.Age {
78 // t.Errorf("Age field differs, got=%s, expected=%s",
79 // record.Age, expected.Age)
80 // }
81 // }
82 // }
83 //
84 // # Using basic go-testdeep approach
85 //
86 // td package, via its Cmp* functions, handles the tests and all the
87 // error message boiler plate. Let's do it:
88 //
89 // import (
90 // "testing"
91 // "time"
92 //
93 // "github.com/maxatome/go-testdeep/td"
94 // )
95 //
96 // func TestCreateRecord(t *testing.T) {
97 // before := time.Now().Truncate(time.Second)
98 // record, err := CreateRecord()
99 //
100 // if td.CmpNoError(t, err) {
101 // td.Cmp(t, record.Id, td.NotZero(), "Id initialized")
102 // td.Cmp(t, record.Name, "Bob")
103 // td.Cmp(t, record.Age, 23)
104 // td.Cmp(t, record.CreatedAt, td.Between(before, time.Now()))
105 // }
106 // }
107 //
108 // As we cannot guess the Id field value before its creation, we use
109 // the [NotZero] operator to check it is set by CreateRecord()
110 // call. The same it true for the creation date field
111 // CreatedAt. Thanks to the [Between] operator we can check it is set
112 // with a value included between the date before CreateRecord() call
113 // and the date just after.
114 //
115 // Note that if Id and CreatedAt could be known in advance, we could
116 // simply do:
117 //
118 // import (
119 // "testing"
120 // "time"
121 //
122 // "github.com/maxatome/go-testdeep/td"
123 // )
124 //
125 // func TestCreateRecord(t *testing.T) {
126 // before := time.Now().Truncate(time.Second)
127 // record, err := CreateRecord()
128 //
129 // if td.CmpNoError(t, err) {
130 // td.Cmp(t, record, &Record{
131 // Id: 1234,
132 // Name: "Bob",
133 // Age: 23,
134 // CreatedAt: time.Date(2019, time.May, 1, 12, 13, 14, 0, time.UTC),
135 // })
136 // }
137 // }
138 //
139 // But unfortunately, it is common not to know exactly the value of some
140 // fields…
141 //
142 // # Using advanced go-testdeep technique
143 //
144 // Of course we can test struct fields one by one, but with go-testdeep,
145 // the whole struct can be compared with one [Cmp] call.
146 //
147 // import (
148 // "testing"
149 // "time"
150 //
151 // "github.com/maxatome/go-testdeep/td"
152 // )
153 //
154 // func TestCreateRecord(t *testing.T) {
155 // before := time.Now().Truncate(time.Second)
156 // record, err := CreateRecord()
157 //
158 // if td.CmpNoError(t, err) {
159 // td.Cmp(t, record,
160 // td.Struct(
161 // &Record{
162 // Name: "Bob",
163 // Age: 23,
164 // },
165 // td.StructFields{
166 // "Id": td.NotZero(),
167 // "CreatedAt": td.Between(before, time.Now()),
168 // }),
169 // "Newly created record")
170 // }
171 // }
172 //
173 // See the use of the [Struct] operator. It is needed here to overcome
174 // the go static typing system and so use other go-testdeep operators
175 // for some fields, here [NotZero] and [Between].
176 //
177 // Not only structs can be compared. A lot of ["operators"] can be found
178 // below to cover most (all?) needed tests. See [TestDeep].
179 //
180 // # Using go-testdeep Cmp shortcuts
181 //
182 // The [Cmp] function is the keystone of this package, but to make
183 // the writing of tests even easier, the family of Cmp* functions are
184 // provided and act as shortcuts. Using [CmpStruct] function, the
185 // previous example can be written as:
186 //
187 // import (
188 // "testing"
189 // "time"
190 //
191 // "github.com/maxatome/go-testdeep/td"
192 // )
193 //
194 // func TestCreateRecord(t *testing.T) {
195 // before := time.Now().Truncate(time.Second)
196 // record, err := CreateRecord()
197 //
198 // if td.CmpNoError(t, err) {
199 // td.CmpStruct(t, record,
200 // &Record{
201 // Name: "Bob",
202 // Age: 23,
203 // },
204 // td.StructFields{
205 // "Id": td.NotZero(),
206 // "CreatedAt": td.Between(before, time.Now()),
207 // },
208 // "Newly created record")
209 // }
210 // }
211 //
212 // # Using T type
213 //
214 // [testing.T] can be encapsulated in [T] type, simplifying again the
215 // test:
216 //
217 // import (
218 // "testing"
219 // "time"
220 //
221 // "github.com/maxatome/go-testdeep/td"
222 // )
223 //
224 // func TestCreateRecord(tt *testing.T) {
225 // t := td.NewT(tt)
226 //
227 // before := time.Now().Truncate(time.Second)
228 // record, err := CreateRecord()
229 //
230 // if t.CmpNoError(err) {
231 // t.RootName("RECORD").Struct(record,
232 // &Record{
233 // Name: "Bob",
234 // Age: 23,
235 // },
236 // td.StructFields{
237 // "Id": td.NotZero(),
238 // "CreatedAt": td.Between(before, time.Now()),
239 // },
240 // "Newly created record")
241 // }
242 // }
243 //
244 // Note the use of [T.RootName] method, it allows to name what we are
245 // going to test, instead of the default "DATA".
246 //
247 // # A step further with operator anchoring
248 //
249 // Overcome the go static typing system using the [Struct] operator is
250 // sometimes heavy. Especially when structs are nested, as the [Struct]
251 // operator needs to be used for each level surrounding the level in
252 // which an operator is involved. Operator anchoring feature has been
253 // designed to avoid this heaviness:
254 //
255 // import (
256 // "testing"
257 // "time"
258 //
259 // "github.com/maxatome/go-testdeep/td"
260 // )
261 //
262 // func TestCreateRecord(tt *testing.T) {
263 // before := time.Now().Truncate(time.Second)
264 // record, err := CreateRecord()
265 //
266 // t := td.NewT(tt) // operator anchoring needs a *td.T instance
267 //
268 // if t.CmpNoError(err) {
269 // t.Cmp(record,
270 // &Record{
271 // Name: "Bob",
272 // Age: 23,
273 // ID: t.AT[uint64](td.NotZero()),
274 // CreatedAt: t.AT[time.Time](td.Between(before, time.Now())),
275 // // or if using go<1.27
276 // ID: td.A[uint64](t, td.NotZero()),
277 // CreatedAt: td.A[time.Time](t, td.Between(before, time.Now())),
278 // },
279 // "Newly created record")
280 // }
281 // }
282 //
283 // See the [T.AT] method (or its full name alias [T.AnchorT])
284 // documentation for details (if go<1.27, see [A] & [Anchor] functions
285 // instead.)
286 //
287 // [go-testdeep]: https://go-testdeep.zetta.rocks/
288 // [Test::Deep]: https://metacpan.org/pod/Test::Deep
289 // ["operators"]: https://go-testdeep.zetta.rocks/operators/
290 // [tdhttp]: https://pkg.go.dev/github.com/maxatome/go-testdeep/helpers/tdhttp
291 // [tdsuite]: https://pkg.go.dev/github.com/maxatome/go-testdeep/helpers/tdsuite
292 package td // import "github.com/maxatome/go-testdeep/td"