Skip to content

Commit

Permalink
make testing.TB#Helper call safer for suite mode
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Nov 17, 2024
1 parent ce7e5fa commit d6fd834
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
17 changes: 17 additions & 0 deletions Suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion T.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions Var.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -148,15 +148,15 @@ 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)
}

// 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
Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down

0 comments on commit d6fd834

Please sign in to comment.