Skip to content

Commit

Permalink
deprecate AfterAll and AroundAll
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Mar 16, 2023
1 parent 10d1f6e commit 2ec5962
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 136 deletions.
1 change: 0 additions & 1 deletion Global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestGlobal_Before(t *testing.T) {
for i := 0; i < 42; i++ {
s := NewSpec(t)
s.Test("", func(t *T) { t.Must.Equal(n1+n2, v.Get(t)) })
s.Finish()
}
}

Expand Down
8 changes: 2 additions & 6 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Spec struct {

hooks struct {
Around []hook
AroundAll []hookOnce
BeforeAll []hookOnce
}

immutable bool
Expand Down Expand Up @@ -449,22 +449,18 @@ func (spec *Spec) acceptVisitor(v visitor) {
func (spec *Spec) Finish() {
spec.testingTB.Helper()
var tests []func()
var allHookOnce []hookOnce
spec.acceptVisitor(visitorFunc(func(s *Spec) {
if s.finished {
return
}
s.finished = true
s.immutable = true
tests = append(tests, s.tests...)
allHookOnce = append(allHookOnce, s.hooks.AroundAll...)
}))

spec.orderer.Order(tests)
td := &teardown.Teardown{}
defer td.Finish()
for _, hook := range allHookOnce {
td.Defer(hook.Block())
}
for _, tc := range tests {
tc()
}
Expand Down
6 changes: 6 additions & 0 deletions T.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (t *T) setUp() func() {
t.vars.merge(c.vars)
}

for _, c := range contexts {
for _, hook := range c.hooks.BeforeAll {
hook.Block()
}
}

for _, c := range contexts {
for _, hook := range c.hooks.Around {
t.teardown.Defer(hook.Block(t))
Expand Down
4 changes: 2 additions & 2 deletions contracts/TestingTB.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ func (c TestingTB) Spec(s *testcase.Spec) {
s.Describe(`#Cleanup`, func(s *testcase.Spec) {
s.HasSideEffect()
var cleanups []int
s.AfterAll(func(tb testing.TB) {
assert.Equal(tb, []int{4, 2}, cleanups)
s.Before(func(t *testcase.T) {
t.Cleanup(func() { t.Must.Equal([]int{4, 2}, cleanups) })
})
s.Test(``, func(t *testcase.T) {
testingTB.Get(t).Cleanup(func() { cleanups = append(cleanups, 2) })
Expand Down
21 changes: 0 additions & 21 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,27 +977,6 @@ func ExampleSpec_BeforeAll() {
})
}

func ExampleSpec_AfterAll() {
var t *testing.T
s := testcase.NewSpec(t)

s.AfterAll(func(tb testing.TB) {
// this will run once all the test case already ran.
})
}

func ExampleSpec_AroundAll() {
var t *testing.T
s := testcase.NewSpec(t)

s.AroundAll(func(tb testing.TB) func() {
// this will run once before all the test case.
return func() {
// this will run once after all the test case already ran.
}
})
}

func ExampleSpec_Describe() {
var t *testing.T
s := testcase.NewSpec(t)
Expand Down
33 changes: 6 additions & 27 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcase

import (
"runtime"
"sync"
"testing"

"github.com/adamluzsi/testcase/internal/caller"
Expand All @@ -18,7 +19,7 @@ type hook struct {
}

type hookOnce struct {
Block func() func()
Block func()
Frame runtime.Frame
}

Expand Down Expand Up @@ -72,37 +73,15 @@ func (spec *Spec) Around(block hookBlock) {
// BeforeAll give you the ability to create a hook
// that runs only once before the test cases.
func (spec *Spec) BeforeAll(blk func(tb testing.TB)) {
spec.testingTB.Helper()
spec.AroundAll(func(tb testing.TB) func() {
blk(tb)
return func() {}
})
}

// AfterAll give you the ability to create a hook
// that runs only once after all the test cases already ran.
//
// DEPRECATED: use Spec.BeforeAll with testing.TB#Cleanup
func (spec *Spec) AfterAll(blk func(tb testing.TB)) {
spec.testingTB.Helper()
spec.AroundAll(func(tb testing.TB) func() {
return func() { blk(tb) }
})
}

// AroundAll give you the ability to create a hook
// that first run before all test,
// then the returned lambda will run after the test cases.
//
// DEPRECATED: use Spec.BeforeAll with testing.TB#Cleanup
func (spec *Spec) AroundAll(blk func(tb testing.TB) func()) {
spec.testingTB.Helper()
if spec.immutable {
spec.testingTB.Fatal(hookWarning)
}
frame, _ := caller.GetFrame()
spec.hooks.AroundAll = append(spec.hooks.AroundAll, hookOnce{
Block: func() func() { return blk(spec.testingTB) },
var once sync.Once
spec.hooks.BeforeAll = append(spec.hooks.BeforeAll, hookOnce{
Block: func() { once.Do(func() { blk(spec.testingTB) }) },
Frame: frame,
})

}
79 changes: 0 additions & 79 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,51 +80,6 @@ func TestSpec_BeforeAll_blkRunsOnlyOnce(t *testing.T) {
assert.Must(t).Equal(1, counter)
}

func TestSpec_AfterAll_blkRunsOnlyOnce(t *testing.T) {
s := testcase.NewSpec(t)

var counter int
blk := func(t *testcase.T) { assert.Must(t).Equal(0, counter) }
s.AfterAll(func(tb testing.TB) { counter++ })
s.Test(``, blk)
s.Test(``, blk)
s.Test(``, blk)
s.Context(``, func(s *testcase.Spec) {
s.Test(``, blk)
s.Test(``, blk)
s.Test(``, blk)
})
s.Finish()

assert.Must(t).Equal(1, counter)
}

func TestSpec_AroundAll_blkRunsOnlyOnce(t *testing.T) {
s := testcase.NewSpec(t)

var before, after int
blk := func(t *testcase.T) {
assert.Must(t).Equal(1, before)
assert.Must(t).Equal(0, after)
}
s.AroundAll(func(tb testing.TB) func() {
before++
return func() { after++ }
})
s.Test(``, blk)
s.Test(``, blk)
s.Test(``, blk)
s.Context(``, func(s *testcase.Spec) {
s.Test(``, blk)
s.Test(``, blk)
s.Test(``, blk)
})
s.Finish()

assert.Must(t).Equal(1, before)
assert.Must(t).Equal(1, after)
}

func TestSpec_BeforeAll_failIfDefinedAfterTestCases(t *testing.T) {
var isAnyOfTheTestCaseRan bool
blk := func(t *testcase.T) { isAnyOfTheTestCaseRan = true }
Expand All @@ -141,37 +96,3 @@ func TestSpec_BeforeAll_failIfDefinedAfterTestCases(t *testing.T) {
assert.Must(t).True(stub.IsFailed)
assert.Must(t).True(!isAnyOfTheTestCaseRan)
}

func TestSpec_AfterAll_failIfDefinedAfterTestCases(t *testing.T) {
var isAnyOfTheTestCaseRan bool
blk := func(t *testcase.T) { isAnyOfTheTestCaseRan = true }
stub := &doubles.TB{}

sandbox.Run(func() {
s := testcase.NewSpec(stub)
s.Test(``, blk)
s.AfterAll(func(tb testing.TB) {})
s.Test(``, blk)
s.Finish()
})

assert.Must(t).True(stub.IsFailed)
assert.Must(t).True(!isAnyOfTheTestCaseRan)
}

func TestSpec_AroundAll_failIfDefinedAfterTestCases(t *testing.T) {
var isAnyOfTheTestCaseRan bool
blk := func(t *testcase.T) { isAnyOfTheTestCaseRan = true }
stub := &doubles.TB{}

sandbox.Run(func() {
s := testcase.NewSpec(stub)
s.Test(``, blk)
s.AroundAll(func(tb testing.TB) func() { return func() {} })
s.Test(``, blk)
s.Finish()
})

assert.Must(t).True(stub.IsFailed)
assert.Must(t).True(!isAnyOfTheTestCaseRan)
}

0 comments on commit 2ec5962

Please sign in to comment.