From d423e0216281d1458b701f2d49445cf29eee8717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Soul=C3=A9?= Date: Mon, 7 Sep 2026 13:56:44 +0200 Subject: [PATCH] feat: td.A & td.Anchor are go-fixable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime Soulé --- td/t_anchor_118.go | 4 ++-- td/t_anchor_118_test.go | 4 ++-- td/t_anchor_127.go | 24 ++++++++++++++++++++++-- td/t_anchor_127_test.go | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/td/t_anchor_118.go b/td/t_anchor_118.go index 313c6b81..c0614054 100644 --- a/td/t_anchor_118.go +++ b/td/t_anchor_118.go @@ -4,8 +4,8 @@ // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. -//go:build go1.18 -// +build go1.18 +//go:build go1.18 && !go1.27 +// +build go1.18,!go1.27 package td diff --git a/td/t_anchor_118_test.go b/td/t_anchor_118_test.go index 99db74d9..7b9fcfdc 100644 --- a/td/t_anchor_118_test.go +++ b/td/t_anchor_118_test.go @@ -4,8 +4,8 @@ // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. -//go:build go1.18 -// +build go1.18 +//go:build go1.18 && !go1.27 +// +build go1.18,!go1.27 package td_test diff --git a/td/t_anchor_127.go b/td/t_anchor_127.go index 0176793d..a9301798 100644 --- a/td/t_anchor_127.go +++ b/td/t_anchor_127.go @@ -74,7 +74,8 @@ package td // [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType]. func (t *T) AnchorT[X any](operator TestDeep) X { t.Helper() - return Anchor[X](t, operator) + var model X + return t.Anchor(operator, model).(X) } // AT returns a typed value allowing to anchor the TestDeep @@ -142,5 +143,24 @@ func (t *T) AnchorT[X any](operator TestDeep) X { // [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType]. func (t *T) AT[X any](operator TestDeep) X { t.Helper() - return A[X](t, operator) + var model X + return t.A(operator, model).(X) +} + +// Anchor is a generic shortcut to [T.Anchor]. +// +// See also [T.AnchorT] method for a generic variant (go1.27 required). +// +//go:fix inline +func Anchor[X any](t *T, operator TestDeep) X { + return t.AnchorT[X](operator) +} + +// A is a generic shortcut to [T.A]. +// +// See also [T.AT] method for a generic variant (go1.27 required). +// +//go:fix inline +func A[X any](t *T, operator TestDeep) X { + return t.AT[X](operator) } diff --git a/td/t_anchor_127_test.go b/td/t_anchor_127_test.go index 2416c845..e06c6533 100644 --- a/td/t_anchor_127_test.go +++ b/td/t_anchor_127_test.go @@ -51,4 +51,21 @@ func TestT_AnchorT(tt *testing.T) { Str: t.AT[string](td.HasPrefix("Pipo")), Time: t.AT[time.Time](td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)), })) + + // Pre go1.27 versions + td.CmpTrue(tt, + t.Cmp(got, MyStruct{ + PNum: td.Anchor[*int](t, td.Ptr(td.Between(40, 45))), + Num: td.Anchor[int64](t, td.Between(int64(135), int64(137))), + Str: td.Anchor[string](t, td.HasPrefix("Pipo")), + Time: td.Anchor[time.Time](t, td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)), + })) + + td.CmpTrue(tt, + t.Cmp(got, MyStruct{ + PNum: td.A[*int](t, td.Ptr(td.Between(40, 45))), + Num: td.A[int64](t, td.Between(int64(135), int64(137))), + Str: td.A[string](t, td.HasPrefix("Pipo")), + Time: td.A[time.Time](t, td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)), + })) }