Skip to content

Commit

Permalink
refact
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Feb 12, 2023
1 parent 50ec451 commit 85499b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
3 changes: 1 addition & 2 deletions let.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
// Let can enhance readability
// when used sparingly in any given example group,
// but that can quickly degrade with heavy overuse.
//
func Let[V any](spec *Spec, blk VarInit[V]) Var[V] {
spec.testingTB.Helper()
return let[V](spec, makeVarName(spec), blk)
Expand All @@ -59,7 +58,7 @@ func let[V any](spec *Spec, varName string, blk VarInit[V]) Var[V] {
spec.testingTB.Fatalf(warnEventOnImmutableFormat, `Let`)
}
if blk != nil {
spec.vars.sdefs[varName] = findCurrentDeclsFor(spec, varName)
spec.vars.defsSuper[varName] = findCurrentDeclsFor(spec, varName)
spec.vars.defs[varName] = func(t *T) any {
t.Helper()
return blk(t)
Expand Down
44 changes: 22 additions & 22 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import (

func newVariables() *variables {
return &variables{
defs: make(map[string]variablesInitBlock),
sdefs: make(map[string][]variablesInitBlock),
cache: make(map[string]interface{}),
scache: newVariablesSuperCache(),
onLet: make(map[string]struct{}),
locks: make(map[string]*sync.RWMutex),
before: make(map[string]struct{}),
defs: make(map[string]variablesInitBlock),
defsSuper: make(map[string][]variablesInitBlock),
cache: make(map[string]interface{}),
cacheSuper: newVariablesSuperCache(),
onLet: make(map[string]struct{}),
locks: make(map[string]*sync.RWMutex),
before: make(map[string]struct{}),
}
}

// variables represents an individual test case's runtime variables.
// Using the variables cache within the individual test cases are safe even with *testing#T.Parallel().
// Different test cases don't share they variables instance.
type variables struct {
mutex sync.RWMutex
locks map[string]*sync.RWMutex
defs map[string]variablesInitBlock
sdefs map[string][]variablesInitBlock
onLet map[string]struct{}
before map[string]struct{}
cache map[string]any
scache *variablesSuperCache
mutex sync.RWMutex
locks map[string]*sync.RWMutex
defs map[string]variablesInitBlock
defsSuper map[string][]variablesInitBlock
onLet map[string]struct{}
before map[string]struct{}
cache map[string]any
cacheSuper *variablesSuperCache
}

type variablesInitBlock func(t *T) any
Expand Down Expand Up @@ -101,7 +101,7 @@ func (v *variables) reset() {
v.mutex.Lock()
defer v.mutex.Unlock()
v.cache = make(map[string]interface{})
v.scache = newVariablesSuperCache()
v.cacheSuper = newVariablesSuperCache()
}

func (v *variables) fatalMessageFor(varName string) string {
Expand All @@ -122,8 +122,8 @@ func (v *variables) merge(oth *variables) {
for key, value := range oth.defs {
v.defs[key] = value
}
for key, value := range oth.sdefs {
v.sdefs[key] = value
for key, value := range oth.defsSuper {
v.defsSuper[key] = value
}
}

Expand Down Expand Up @@ -168,21 +168,21 @@ func (v *variables) getMutex(varName string) *sync.RWMutex {
//////////////////////////////////////////////////////// super /////////////////////////////////////////////////////////

func (v *variables) SetSuper(varName string, val any) {
v.scache.Set(varName, val)
v.cacheSuper.Set(varName, val)
}

func (v *variables) LookupSuper(t *T, varName string) (any, bool) {
if cv, ok := v.scache.Lookup(varName); ok {
if cv, ok := v.cacheSuper.Lookup(varName); ok {
return cv, ok
}
var declOfSuper func(*T) any
if decl, ok := v.scache.FindDecl(varName, v.sdefs[varName]); ok {
if decl, ok := v.cacheSuper.FindDecl(varName, v.defsSuper[varName]); ok {
declOfSuper = decl
}
if declOfSuper == nil {
return nil, false
}
stepOut := v.scache.StepIn(varName)
stepOut := v.cacheSuper.StepIn(varName)
val := declOfSuper(t)
stepOut()
v.SetSuper(varName, val)
Expand Down

0 comments on commit 85499b9

Please sign in to comment.