Skip to content

Commit

Permalink
add back missing helper calls
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Jul 7, 2024
1 parent d71a38f commit dac4da0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 53 deletions.
12 changes: 6 additions & 6 deletions DSL.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ import (
// in order to avoid repetitive test cases in the `Then` I often define a `onSuccess` variable,
// with a function that takes `testcase#variables` as well and test error return value there with `testcase#variables.T()`.
func (spec *Spec) Describe(subjectTopic string, specification sBlock, opts ...SpecOption) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
opts = append([]SpecOption{Group(subjectTopic)}, opts...)
spec.Context(fmt.Sprintf(`%s %s`, `describe`, subjectTopic), specification, opts...)
}

// When is an alias for testcase#Spec.Context
// When is used usually to represent `if` based decision reasons about your testing subject.
func (spec *Spec) When(desc string, testContextBlock sBlock, opts ...SpecOption) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.Context(fmt.Sprintf(`%s %s`, `when`, desc), testContextBlock, opts...)
}

// And is an alias for testcase#Spec.Context
// And is used to represent additional requirement for reaching a certain testing runtime contexts.
func (spec *Spec) And(desc string, testContextBlock sBlock, opts ...SpecOption) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.Context(fmt.Sprintf(`%s %s`, `and`, desc), testContextBlock, opts...)
}

// Then is an alias for Test
func (spec *Spec) Then(desc string, test tBlock, opts ...SpecOption) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
desc = fmt.Sprintf(`%s %s`, `then`, desc)
spec.Test(desc, test, opts...)
}
Expand All @@ -60,7 +60,7 @@ func (spec *Spec) Then(desc string, test tBlock, opts ...SpecOption) {
// the test specification has side effects that would affect other test specification results,
// and, as such, must be executed sequentially.
func (spec *Spec) NoSideEffect() {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.Parallel()
}

Expand All @@ -73,7 +73,7 @@ func (spec *Spec) NoSideEffect() {
// This allows flexibility for the developers to use side effect free variant for local development that has quick feedback loop,
// and replace them with the production implementation during CI/CD pipeline which less time critical.
func (spec *Spec) HasSideEffect() {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.Sequential()
}

Expand Down
78 changes: 46 additions & 32 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// NewSpec create new Spec struct that is ready for usage.
func NewSpec(tb testing.TB, opts ...SpecOption) *Spec {
h(tb).Helper()
helper(tb).Helper()
// tb, opts = checkSuite(tb, opts)
var s *Spec
switch tb := tb.(type) {
Expand All @@ -38,7 +38,7 @@ func NewSpec(tb testing.TB, opts ...SpecOption) *Spec {
}

func newSpec(tb testing.TB, opts ...SpecOption) *Spec {
h(tb).Helper()
helper(tb).Helper()
s := &Spec{
testingTB: tb,
opts: opts,
Expand All @@ -53,7 +53,7 @@ func newSpec(tb testing.TB, opts ...SpecOption) *Spec {
}

func (spec *Spec) newSubSpec(desc string, opts ...SpecOption) *Spec {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.immutable = true
sub := newSpec(spec.testingTB, opts...)
sub.parent = spec
Expand Down Expand Up @@ -144,14 +144,16 @@ type (
// To verify easily your state-machine, you can count the `if`s in your implementation,
// and check that each `if` has 2 `When` block to represent the two possible path.
func (spec *Spec) Context(desc string, testContextBlock sBlock, opts ...SpecOption) {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
helper(spec.testingTB).Helper()

spec.defs = append(spec.defs, func(oth *Spec) {
oth.Context(desc, testContextBlock, opts...)
})
if spec.isSuite() {
return
}
h(spec.testingTB).Helper()
sub := spec.newSubSpec(desc, opts...)
if spec.sync {
defer sub.Finish()
Expand Down Expand Up @@ -203,14 +205,15 @@ func (spec *Spec) Context(desc string, testContextBlock sBlock, opts ...SpecOpti
// It should not contain anything that modify the test subject input.
// It should focus only on asserting the result of the subject.
func (spec *Spec) Test(desc string, test tBlock, opts ...SpecOption) {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
helper(spec.testingTB).Helper()
spec.defs = append(spec.defs, func(oth *Spec) {
oth.Test(desc, test, opts...)
})
if spec.isSuite() {
return
}
h(spec.testingTB).Helper()
s := spec.newSubSpec(desc, opts...)
s.isTest = !s.isBenchmark
s.hasRan = true
Expand All @@ -224,7 +227,9 @@ const panicMessageForRunningBenchmarkAfterTest = `when .Benchmark is defined, th
//
// Creating a Benchmark will signal the Spec that test and benchmark happens seperately, and a test should not double as a benchmark.
func (spec *Spec) Benchmark(desc string, test tBlock, opts ...SpecOption) {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
helper(spec.testingTB).Helper()
if spec.isTestRunner() {
return
}
Expand All @@ -248,8 +253,9 @@ const warnEventOnImmutableFormat = `you can't use .%s after you already used whe
// Using values from *vars when Parallel is safe.
// It is a shortcut for executing *testing.T#Parallel() for each test
func (spec *Spec) Parallel() {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
if spec.immutable {
spec.testingTB.Fatalf(warnEventOnImmutableFormat, `Parallel`)
}
Expand All @@ -260,8 +266,9 @@ func (spec *Spec) Parallel() {
// SkipBenchmark will flag the current Spec / Context to be skipped during Benchmark mode execution.
// If you wish to skip only a certain test, not the whole Spec / Context, use the SkipBenchmark SpecOption instead.
func (spec *Spec) SkipBenchmark() {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
if spec.immutable {
spec.testingTB.Fatalf(warnEventOnImmutableFormat, `SkipBenchmark`)
}
Expand All @@ -275,8 +282,9 @@ func (spec *Spec) SkipBenchmark() {
// This is useful when you want to create a spec helper package
// and there you want to manage if you want to use components side effects or not.
func (spec *Spec) Sequential() {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
if spec.immutable {
panic(fmt.Sprintf(warnEventOnImmutableFormat, `Sequential`))
}
Expand All @@ -300,14 +308,15 @@ func (spec *Spec) Sequential() {
// TESTCASE_TAG_EXCLUDE='E2E' go test ./...
// TESTCASE_TAG_INCLUDE='E2E' TESTCASE_TAG_EXCLUDE='list,of,excluded,tags' go test ./...
func (spec *Spec) Tag(tags ...string) {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.tags = append(spec.tags, tags...)
})
}

func (spec *Spec) isAllowedToRun() bool {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()

if spec.isTest && !spec.isTestAllowedToRun() {
return false
Expand Down Expand Up @@ -337,7 +346,7 @@ func (spec *Spec) isAllowedToRun() bool {
}

func (spec *Spec) isTestAllowedToRun() bool {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
for _, context := range spec.specsFromParent() {
if context.skipTest {
return false
Expand All @@ -347,7 +356,7 @@ func (spec *Spec) isTestAllowedToRun() bool {
}

func (spec *Spec) isBenchAllowedToRun() bool {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
for _, context := range spec.specsFromParent() {
if context.skipBenchmark {
return false
Expand All @@ -357,7 +366,7 @@ func (spec *Spec) isBenchAllowedToRun() bool {
}

func (spec *Spec) lookupRetryFlaky() (assert.Retry, bool) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
for _, context := range spec.specsFromParent() {
if context.flaky != nil {
return *context.flaky, true
Expand All @@ -367,7 +376,7 @@ func (spec *Spec) lookupRetryFlaky() (assert.Retry, bool) {
}

func (spec *Spec) lookupRetryEventually() (assert.Retry, bool) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
for _, context := range spec.specsFromParent() {
if context.eventually != nil {
return *context.eventually, true
Expand All @@ -377,7 +386,7 @@ func (spec *Spec) lookupRetryEventually() (assert.Retry, bool) {
}

func (spec *Spec) printDescription(tb testing.TB) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
tb.Helper()
var lines []interface{}

Expand Down Expand Up @@ -416,7 +425,7 @@ func (spec *Spec) name() string {
///////////////////////////////////////////////////////=- run -=////////////////////////////////////////////////////////

func (spec *Spec) run(blk func(*T)) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
if !spec.isAllowedToRun() {
return
}
Expand All @@ -427,7 +436,7 @@ func (spec *Spec) run(blk func(*T)) {
return
}
spec.addTest(func() {
if h, ok := tb.(helper); ok {
if h, ok := tb.(testingHelper); ok {
h.Helper()
}
tb.Run(name, func(t *testing.T) {
Expand All @@ -447,7 +456,7 @@ func (spec *Spec) run(blk func(*T)) {
})
case TBRunner:
spec.addTest(func() {
if h, ok := tb.(helper); ok {
if h, ok := tb.(testingHelper); ok {
h.Helper()
}
tb.Run(name, func(tb testing.TB) {
Expand All @@ -457,7 +466,7 @@ func (spec *Spec) run(blk func(*T)) {
})
default:
spec.addTest(func() {
if h, ok := tb.(helper); ok {
if h, ok := tb.(testingHelper); ok {
h.Helper()
}
spec.runTB(tb, blk)
Expand All @@ -479,6 +488,7 @@ func (spec *Spec) isTestRunner() bool {
func (spec *Spec) modify(blk func(spec *Spec)) {
spec.mods = append(spec.mods, blk)
if isValidTestingTB(spec.testingTB) {
helper(spec.testingTB).Helper()
blk(spec)
}
}
Expand All @@ -491,8 +501,9 @@ func (spec *Spec) getTestSeed(tb testing.TB) int64 {
}

func (spec *Spec) runTB(tb testing.TB, blk func(*T)) {
h(spec.testingTB).Helper()
tb.Helper()
helper(spec.testingTB).Helper()
helper(tb).Helper()

spec.hasRan = true
if tb, ok := tb.(interface{ Parallel() }); ok && spec.isParallel() {
tb.Parallel()
Expand Down Expand Up @@ -528,8 +539,9 @@ func (spec *Spec) runTB(tb testing.TB, blk func(*T)) {
}

func (spec *Spec) runB(b *testing.B, blk func(*T)) {
h(spec.testingTB).Helper()
b.Helper()
helper(spec.testingTB).Helper()
helper(b).Helper()

t := newT(b, spec)
if _, ok := spec.lookupRetryFlaky(); ok {
b.Skip(`skipping because flaky flag`)
Expand Down Expand Up @@ -561,6 +573,7 @@ type visitorFunc func(s *Spec)
func (fn visitorFunc) Visit(s *Spec) { fn(s) }

func (spec *Spec) acceptVisitor(v visitor) {
helper(spec.testingTB).Helper()
for _, child := range spec.children {
child.acceptVisitor(v)
}
Expand All @@ -573,8 +586,9 @@ func (spec *Spec) acceptVisitor(v visitor) {
// Such case can be when a resource leaked inside a testing scope
// and resource closed with a deferred function, but the spec is still not ran.
func (spec *Spec) Finish() {
helper(spec.testingTB).Helper()
spec.modify(func(spec *Spec) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
var tests []func()
spec.acceptVisitor(visitorFunc(func(s *Spec) {
if s.finished {
Expand Down Expand Up @@ -605,7 +619,7 @@ func (spec *Spec) documentResults() {
if spec.isSuite() || spec.isBenchmark {
return
}
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
spec.doc.once.Do(func() {
var collect func(*Spec) []doc.TestingCase
collect = func(spec *Spec) []doc.TestingCase {
Expand All @@ -630,7 +644,7 @@ func (spec *Spec) documentResults() {
}

func (spec *Spec) withFinishUsingTestingTB(tb testing.TB, blk func()) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
tb.Helper()
ogTB := spec.testingTB
defer func() { spec.testingTB = ogTB }()
Expand All @@ -640,7 +654,7 @@ func (spec *Spec) withFinishUsingTestingTB(tb testing.TB, blk func()) {
}

func (spec *Spec) isParallel() bool {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
var (
isParallel bool
isSequential bool
Expand Down Expand Up @@ -689,7 +703,7 @@ func (spec *Spec) specsFromCurrent() []*Spec {
}

func (spec *Spec) lookupParent() (*Spec, bool) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
for _, s := range spec.specsFromCurrent() {
if s.hasRan { // skip test
continue
Expand All @@ -703,7 +717,7 @@ func (spec *Spec) lookupParent() (*Spec, bool) {
}

func (spec *Spec) getTagSet() map[string]struct{} {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()
tagsSet := make(map[string]struct{})
for _, ctx := range spec.specsFromParent() {
for _, tag := range ctx.tags {
Expand All @@ -716,7 +730,7 @@ func (spec *Spec) getTagSet() map[string]struct{} {
// addTest registers a testing block to be executed as part of the Spec.
// the main purpose is to enable test execution order manipulation throught the TESTCASE_SEED.
func (spec *Spec) addTest(blk func()) {
h(spec.testingTB).Helper()
helper(spec.testingTB).Helper()

if p, ok := spec.lookupParent(); ok && p.sync {
blk()
Expand Down Expand Up @@ -744,7 +758,7 @@ To achieve this, the current "testcase.Spec" needs to be created as a suite by p
Once the "Spec" is converted into a suite, you can use "testcase.Spec#Spec" as the function block for another "testcase.Spec" "#Context" call.`

func (spec *Spec) Spec(oth *Spec) {
h(oth.testingTB).Helper()
helper(oth.testingTB).Helper()
if !spec.isSuite() {
panic(panicMessageSpecSpec)
}
Expand Down Expand Up @@ -803,7 +817,7 @@ func (suite SpecSuite) run(tb testing.TB) {
s.Context(suite.N, suite.Spec, Group(suite.N))
}

func h(tb helper) helper {
func helper(tb testingHelper) testingHelper {
if tb == nil {
return internal.NullTB{}
}
Expand Down
4 changes: 2 additions & 2 deletions Suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type OpenSuite interface {
// By using RunSuite, you don't have to distinguish between testing or benchmark execution mod.
// It supports *testing.T, *testing.B, *testcase.T, *testcase.Spec and CustomTB test runners.
func RunSuite[S Suite, TBS anyTBOrSpec](tb TBS, contracts ...S) {
if tb, ok := any(tb).(helper); ok {
if tb, ok := any(tb).(testingHelper); ok {
tb.Helper()
}
s := ToSpec(tb)
Expand All @@ -47,7 +47,7 @@ func RunSuite[S Suite, TBS anyTBOrSpec](tb TBS, contracts ...S) {
}

func RunOpenSuite[OS OpenSuite, TBS anyTBOrSpec](tb TBS, contracts ...OS) {
if tb, ok := any(tb).(helper); ok {
if tb, ok := any(tb).(testingHelper); ok {
tb.Helper()
}
s := ToSpec(tb)
Expand Down
Loading

0 comments on commit dac4da0

Please sign in to comment.