Skip to content

Commit

Permalink
Add ensure.RunParallel
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahWitt committed May 8, 2023
1 parent a9f02d7 commit de1717d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
20 changes: 19 additions & 1 deletion ensuring/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ensuring

import "github.com/JosiahWitt/ensure/internal/testctx"

// Run fn as a subtest called name.
// Run runs fn as a subtest called name, using [testing.T.Run].
func (e E) Run(name string, fn func(ensure E)) {
c := e(nil)
c.t.Helper()
Expand All @@ -15,3 +15,21 @@ func (e E) Run(name string, fn func(ensure E)) {
fn(ensure)
})
}

// RunParallel runs fn as a subtest called name, using [testing.T.Run].
// It calls [testing.T.Parallel] just before calling fn, causing fn to
// be run in parallel with (and only with) other parallel tests. See
// the [testing.T.Parallel] docs for more info.
func (e E) RunParallel(name string, fn func(ensure E)) {
c := e(nil)
c.t.Helper()
c.markRun()

c.ctx.Run(name, func(ctx testctx.Context) {
t := ctx.T()
t.Helper()
ensure := wrap(t)
t.Parallel()
fn(ensure)
})
}
21 changes: 20 additions & 1 deletion ensuring/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import (
)

func TestERun(t *testing.T) {
testERun(t, false, func(ensure ensuring.E) func(string, func(ensuring.E)) {
return ensure.Run
})
}

func TestERunParallel(t *testing.T) {
testERun(t, true, func(ensure ensuring.E) func(string, func(ensuring.E)) {
return ensure.RunParallel
})
}

type runBuilderFunc func(ensure ensuring.E) func(string, func(ensuring.E))

func testERun(t *testing.T, isParallel bool, runBuilder runBuilderFunc) {
ctrl := gomock.NewController(t)

outerMockT := setupMockTWithCleanupCheck(t)
Expand All @@ -23,6 +37,10 @@ func TestERun(t *testing.T) {
innerMockT := setupMockTWithCleanupCheck(t)
innerMockT.EXPECT().Helper().Times(2)

if isParallel {
innerMockT.EXPECT().Parallel()
}

innerMockCtx := mock_testctx.NewMockContext(ctrl)
innerMockCtx.EXPECT().T().Return(innerMockT)
testhelper.SetTestContext(t, innerMockT, innerMockCtx)
Expand All @@ -36,7 +54,8 @@ func TestERun(t *testing.T) {

var innerEnsure ensuring.E
outerEnsure := ensure.New(outerMockT)
outerEnsure.Run(name, func(ensure ensuring.E) {
run := runBuilder(outerEnsure)
run(name, func(ensure ensuring.E) {
innerEnsure = ensure
})

Expand Down
25 changes: 25 additions & 0 deletions internal/mocks/mock_testctx/mock_testctx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/testctx/testctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type T interface {
Run(name string, f func(t *testing.T)) bool
Helper()
Cleanup(func())
Parallel()
}

var _ T = &testing.T{}
Expand Down

0 comments on commit de1717d

Please sign in to comment.