Skip to content

Commit

Permalink
Var.Bind improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Feb 12, 2023
1 parent 85499b9 commit d2edad7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Var.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ func (v Var[V]) LetValue(s *Spec, value V) Var[V] {
// where skipping providing a block meant to be explicitly expressed.
func (v Var[V]) Bind(s *Spec) Var[V] {
s.testingTB.Helper()
if s.vars.Knows(v.ID) {
return v
for _, s := range s.specsFromCurrent() {
if s.vars.Knows(v.ID) {
return v
}
}
return v.Let(s, v.Init)
}
Expand Down
20 changes: 19 additions & 1 deletion Var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ func TestVar_Bind(t *testing.T) {
s.Finish()
assert.True(t, onLetRan)
})
t.Run("bind will not overrite a previous value assignment", func(t *testing.T) {
t.Run("bind will not overwrite a previous value assignment", func(t *testing.T) {
s := testcase.NewSpec(t)
expected := rnd.Int()
var onLetRan bool
Expand All @@ -952,6 +952,24 @@ func TestVar_Bind(t *testing.T) {
s.Finish()
assert.True(t, onLetRan)
})
t.Run("bind will not overwrite a previous value assignment in an outer scope", func(t *testing.T) {
s := testcase.NewSpec(t)
expected := rnd.Int()
var onLetRan bool
v := testcase.Var[int]{
ID: "variable", Init: func(t *testcase.T) int { return rnd.Int() },
OnLet: func(s *testcase.Spec, self testcase.Var[int]) { onLetRan = true },
}
v.Let(s, func(s *testcase.T) int { return expected })
s.Context("", func(s *testcase.Spec) {
v = v.Bind(s)
s.Test(``, func(t *testcase.T) {
assert.Must(t).Equal(expected, v.Get(t))
})
})
s.Finish()
assert.True(t, onLetRan)
})
}

func TestVar_Before(t *testing.T) {
Expand Down

0 comments on commit d2edad7

Please sign in to comment.