Debian Codegraph @

td/t_anchor_127.go

raw
1 // Copyright (c) 2026, 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 //go:build go1.27
8 // +build go1.27
9
10 package td
11
12 // AnchorT returns a typed value allowing to anchor the TestDeep
13 // operator operator in a go classic literal like a struct, slice,
14 // array or map value.
15 //
16 // X type must be compatible with operator, so if the TypeBehind
17 // method of operator returns a non-nil type, it has to match X.
18 //
19 // AnchorT returns a typed value ready to be embed in a go data
20 // structure to be compared using [T.Cmp] or [T.CmpLax]:
21 //
22 // import (
23 // "testing"
24 //
25 // "github.com/maxatome/go-testdeep/td"
26 // )
27 //
28 // func TestFunc(tt *testing.T) {
29 // got := Func()
30 //
31 // t := td.NewT(tt)
32 // t.Cmp(got, &MyStruct{
33 // Name: "Bob",
34 // Details: &MyDetails{
35 // Nick: t.AnchorT[string](td.HasPrefix("Bobby")),
36 // Age: t.AnchorT[int](td.Between(40, 50)),
37 // },
38 // })
39 // }
40 //
41 // Without operator anchoring feature, the previous example would have
42 // been:
43 //
44 // import (
45 // "testing"
46 //
47 // "github.com/maxatome/go-testdeep/td"
48 // )
49 //
50 // func TestFunc(tt *testing.T) {
51 // got := Func()
52 //
53 // t := td.NewT(tt)
54 // t.Cmp(got, td.Struct(&MyStruct{Name: "Bob"},
55 // td.StructFields{
56 // "Details": td.Struct(&MyDetails{},
57 // td.StructFields{
58 // "Nick": td.HasPrefix("Bobby"),
59 // "Age": td.Between(40, 50),
60 // }),
61 // }))
62 // }
63 //
64 // using two times the [Struct] operator to work around the strict type
65 // checking of golang.
66 //
67 // AnchorT is a synonym of [T.AT].
68 //
69 // By default, the value returned by AnchorT can only be used in the
70 // next [T.Cmp] or [T.CmpLax] call. To make it persistent across calls,
71 // see [T.SetAnchorsPersist] and [T.AnchorsPersistTemporarily] methods.
72 //
73 // See also [T.AnchorsPersistTemporarily], [T.DoAnchorsPersist],
74 // [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType].
75 func (t *T) AnchorT[X any](operator TestDeep) X {
76 t.Helper()
77 return Anchor[X](t, operator)
78 }
79
80 // AT returns a typed value allowing to anchor the TestDeep
81 // operator operator in a go classic literal like a struct, slice,
82 // array or map value.
83 //
84 // X type must be compatible with operator, so if the TypeBehind
85 // method of operator returns a non-nil type, it has to match X.
86 //
87 // AT returns a typed value ready to be embed in a go data
88 // structure to be compared using [T.Cmp] or [T.CmpLax]:
89 //
90 // import (
91 // "testing"
92 //
93 // "github.com/maxatome/go-testdeep/td"
94 // )
95 //
96 // func TestFunc(tt *testing.T) {
97 // got := Func()
98 //
99 // t := td.NewT(tt)
100 // t.Cmp(got, &MyStruct{
101 // Name: "Bob",
102 // Details: &MyDetails{
103 // Nick: t.AT[string](td.HasPrefix("Bobby")),
104 // Age: t.AT[int](td.Between(40, 50)),
105 // },
106 // })
107 // }
108 //
109 // Without operator anchoring feature, the previous example would have
110 // been:
111 //
112 // import (
113 // "testing"
114 //
115 // "github.com/maxatome/go-testdeep/td"
116 // )
117 //
118 // func TestFunc(tt *testing.T) {
119 // got := Func()
120 //
121 // t := td.NewT(tt)
122 // t.Cmp(got, td.Struct(&MyStruct{Name: "Bob"},
123 // td.StructFields{
124 // "Details": td.Struct(&MyDetails{},
125 // td.StructFields{
126 // "Nick": td.HasPrefix("Bobby"),
127 // "Age": td.Between(40, 50),
128 // }),
129 // }))
130 // }
131 //
132 // using two times the [Struct] operator to work around the strict type
133 // checking of golang.
134 //
135 // AT is a shorter synonym of [T.AnchorT].
136 //
137 // By default, the value returned by AT can only be used in the
138 // next [T.Cmp] or [T.CmpLax] call. To make it persistent across calls,
139 // see [T.SetAnchorsPersist] and [T.AnchorsPersistTemporarily] methods.
140 //
141 // See also [T.AnchorsPersistTemporarily], [T.DoAnchorsPersist],
142 // [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType].
143 func (t *T) AT[X any](operator TestDeep) X {
144 t.Helper()
145 return A[X](t, operator)
146 }