From d6fd834d203606426684a4f03c29f6e14e3703a1 Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Sun, 17 Nov 2024 22:06:30 +0100 Subject: [PATCH] make testing.TB#Helper call safer for suite mode --- Suite_test.go | 17 +++++++++++++++++ T.go | 2 +- Var.go | 12 ++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Suite_test.go b/Suite_test.go index b148f96..e43dc8d 100644 --- a/Suite_test.go +++ b/Suite_test.go @@ -200,6 +200,23 @@ func TestSpec_AsSuite_merge(t *testing.T) { // TODO: cover further } +func TestSpecSuite_VarLet(t *testing.T) { + s := testcase.NewSpec(nil) + v := testcase.Var[string]{ + ID: "42", + Init: func(t *testcase.T) string { + return t.Random.String() + }, + } + v = v.Let(s, func(t *testcase.T) string { + return "42" + }) + s.Test("", func(t *testcase.T) { + assert.Equal(t, v.Get(t), "42") + }) + s.AsSuite("").Test(t) +} + type SampleContractType interface { testcase.Suite testcase.OpenSuite diff --git a/T.go b/T.go index c9e09ef..9f1fa68 100644 --- a/T.go +++ b/T.go @@ -179,7 +179,7 @@ func (v Var[V]) initDeps(t *T) { } func (v Var[V]) letDeps(s *Spec) { - s.testingTB.Helper() + helper(s.testingTB).Helper() for _, dep := range v.Deps { dep.bind(s) } diff --git a/Var.go b/Var.go index 9f10ee9..9200eef 100644 --- a/Var.go +++ b/Var.go @@ -122,13 +122,13 @@ func (v Var[V]) Set(t *T, value V) { // Let allow you to set the variable value to a given spec func (v Var[V]) Let(s *Spec, blk VarInit[V]) Var[V] { - s.testingTB.Helper() + helper(s.testingTB).Helper() v.onLet(s) return let(s, v.ID, blk) } func (v Var[V]) onLet(s *Spec) { - s.testingTB.Helper() + helper(s.testingTB).Helper() if v.OnLet != nil { v.OnLet(s, v) s.vars.addOnLetHookSetup(v.ID) @@ -148,7 +148,7 @@ func (v Var[V]) execBefore(t *T) { // LetValue set the value of the variable to a given block func (v Var[V]) LetValue(s *Spec, value V) Var[V] { - s.testingTB.Helper() + helper(s.testingTB).Helper() v.onLet(s) return letValue[V](s, v.ID, value) } @@ -156,7 +156,7 @@ func (v Var[V]) LetValue(s *Spec, value V) Var[V] { // Bind is a syntax sugar shorthand for Var.Let(*Spec, nil), // where skipping providing a block meant to be explicitly expressed. func (v Var[V]) Bind(s *Spec) Var[V] { - s.testingTB.Helper() + helper(s.testingTB).Helper() for _, s := range s.specsFromCurrent() { if s.vars.Knows(v.ID) { return v @@ -166,7 +166,7 @@ func (v Var[V]) Bind(s *Spec) Var[V] { } func (v Var[V]) bind(s *Spec) { - s.testingTB.Helper() + helper(s.testingTB).Helper() _ = v.Bind(s) } @@ -177,7 +177,7 @@ func (v Var[V]) bind(s *Spec) { // For example, you may persist the value in a storage as part of the initialization block, // and then when the testCase/then block is reached, the entity is already present in the resource. func (v Var[V]) EagerLoading(s *Spec) Var[V] { - s.testingTB.Helper() + helper(s.testingTB).Helper() s.Before(func(t *T) { _ = v.Get(t) }) return v }