Skip to content

Commit

Permalink
refactor doubles to enable importing double TB without testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Jul 25, 2022
1 parent af51326 commit 4101749
Show file tree
Hide file tree
Showing 24 changed files with 167 additions and 122 deletions.
3 changes: 2 additions & 1 deletion Race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
"github.com/adamluzsi/testcase/internal/doubles"
)

func TestRace(t *testing.T) {
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestRace(t *testing.T) {
testcase.Race(func() {
fn1Finished = true
}, func() {
fakeTB := &testcase.StubTB{}
fakeTB := &doubles.TB{}
// this only meant to represent why goroutine exit needs to be propagated.
fakeTB.FailNow()
fn2Finished = true
Expand Down
3 changes: 2 additions & 1 deletion Sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (

"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal/doubles"
"github.com/adamluzsi/testcase/random"
"github.com/adamluzsi/testcase/sandbox"
)

func ExampleSandbox() {
stb := &testcase.StubTB{}
stb := &doubles.TB{}
outcome := testcase.Sandbox(func() {
// some test helper function calls fatal, which cause runtime.Goexit after marking the test failed.
stb.FailNow()
Expand Down
3 changes: 2 additions & 1 deletion Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
"github.com/adamluzsi/testcase/internal/caller"
"github.com/adamluzsi/testcase/internal/teardown"
)

// NewSpec create new Spec struct that is ready for usage.
Expand Down Expand Up @@ -468,7 +469,7 @@ func (spec *Spec) Finish() {
allHookOnce = append(allHookOnce, s.hooks.AroundAll...)
}))
spec.orderer.Order(tests)
td := &internal.Teardown{}
td := &teardown.Teardown{}
defer td.Finish()
for _, hook := range allHookOnce {
td.Defer(hook.Block())
Expand Down
7 changes: 4 additions & 3 deletions Spec_bc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
"github.com/adamluzsi/testcase/internal/doubles"
)

func TestSpec_FriendlyVarNotDefined(t *testing.T) {
stub := &StubTB{}
stub := &doubles.TB{}
s := NewSpec(stub)
willFatalWithMessage := willFatalWithMessageFn(stub)

Expand All @@ -30,7 +31,7 @@ func TestSpec_FriendlyVarNotDefined(t *testing.T) {
})
}

func isFatalFn(stub *StubTB) func(block func()) bool {
func isFatalFn(stub *doubles.TB) func(block func()) bool {
return func(block func()) bool {
stub.IsFailed = false
defer func() { stub.IsFailed = false }()
Expand All @@ -39,7 +40,7 @@ func isFatalFn(stub *StubTB) func(block func()) bool {
}
}

func willFatalWithMessageFn(stub *StubTB) func(tb testing.TB, blk func()) bytes.Buffer {
func willFatalWithMessageFn(stub *doubles.TB) func(tb testing.TB, blk func()) bytes.Buffer {
isFatal := isFatalFn(stub)
return func(tb testing.TB, blk func()) bytes.Buffer {
stub.Logs = bytes.Buffer{}
Expand Down
25 changes: 13 additions & 12 deletions Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
doubles2 "github.com/adamluzsi/testcase/internal/doubles"
"github.com/adamluzsi/testcase/internal/spechelper"
"github.com/adamluzsi/testcase/random"

Expand Down Expand Up @@ -257,7 +258,7 @@ func TestSpec_ParallelSafeVariableSupport(t *testing.T) {
}

func TestSpec_InvalidUsages(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(stub)
nest1Value := rand.Int()
nest2Value := rand.Int()
Expand Down Expand Up @@ -433,7 +434,7 @@ func hackCallParallel(tb testing.TB) {
switch tb := tb.(type) {
case *testing.T:
tb.Parallel()
case *internal.RecorderTB:
case *doubles2.RecorderTB:
hackCallParallel(tb.TB)
case *testcase.T:
hackCallParallel(tb.TB)
Expand Down Expand Up @@ -600,7 +601,7 @@ func TestSpec_LetValue_ValueDefinedAtDeclarationWithoutTheNeedOfFunctionCallback
}

func TestSpec_LetValue_mutableValuesAreNotAllowed(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(stub)

var finished bool
Expand Down Expand Up @@ -696,7 +697,7 @@ func (tb *UnknownTestingTB) Log(args ...interface{}) {
}

func TestSpec_Test_withSomethingThatImplementsTestcaseTB(t *testing.T) {
rtb := &internal.RecorderTB{TB: &testcase.StubTB{}}
rtb := &doubles2.RecorderTB{TB: &doubles2.TB{}}

var tb testcase.TBRunner = rtb // implements check
s := testcase.NewSpec(tb)
Expand Down Expand Up @@ -831,7 +832,7 @@ func TestSpec_panicDoNotLeakOutFromTestingScope(t *testing.T) {
var noPanic bool
func() {
defer recover()
rtb := &internal.RecorderTB{TB: &testcase.StubTB{}}
rtb := &doubles2.RecorderTB{TB: &doubles2.TB{}}
defer rtb.CleanupNow()
s := testcase.NewSpec(rtb)
s.Test(``, func(t *testcase.T) { panic(`die`) })
Expand Down Expand Up @@ -966,7 +967,7 @@ func BenchmarkTest_Spec_SkipBenchmark2(b *testing.B) {
}

type stubB struct {
*testcase.StubTB
*doubles2.TB
TestingB *testing.B
}

Expand All @@ -975,9 +976,9 @@ func (b *stubB) Run(name string, fn func(b *testing.B)) bool {
}

func BenchmarkTest_Spec_SkipBenchmark_invalidUse(b *testing.B) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
stb := &stubB{
StubTB: stub,
TB: stub,
TestingB: b,
}
s := testcase.NewSpec(stb)
Expand Down Expand Up @@ -1005,7 +1006,7 @@ func BenchmarkTest_Spec_Test_flaky(b *testing.B) {
}

func TestSpec_Test_FailNowWithCustom(t *testing.T) {
rtb := &internal.RecorderTB{TB: &testcase.StubTB{}}
rtb := &doubles2.RecorderTB{TB: &doubles2.TB{}}
s := testcase.NewSpec(rtb)

var failCount int
Expand All @@ -1020,7 +1021,7 @@ func TestSpec_Test_FailNowWithCustom(t *testing.T) {
}

func TestSpec_Test_flaky_withoutFlakyFlag_willFailAndNeverRunAgain(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(stub)
var total int
s.Test(``, func(t *testcase.T) { total++; t.FailNow() })
Expand Down Expand Up @@ -1174,7 +1175,7 @@ func TestSpec_Finish(t *testing.T) {
}

func TestSpec_Finish_finishedSpecIsImmutable(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(stub)
s.Before(func(t *testcase.T) {})
s.Finish()
Expand Down Expand Up @@ -1313,7 +1314,7 @@ func TestNewSpec_withTestingT_optionsPassed(t *testing.T) {
func TestNewSpec_withTestcaseT_optionsPassed(t *testing.T) {
testcase.NewSpec(t).Test(``, func(t *testcase.T) {
rnd := random.New(random.CryptoSeed{})
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(t, testcase.Flaky(time.Second))
s.Test(``, func(t *testcase.T) {
if rnd.Bool() {
Expand Down
3 changes: 2 additions & 1 deletion Suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal/doubles"
)

func TestRunSuite(t *testing.T) {
t.Run(`when TB is testing.TB`, func(t *testing.T) {
sT := &RunContractContract{}
var tb testing.TB = &testcase.StubTB{}
var tb testing.TB = &doubles.TB{}
tb = testcase.NewT(tb, testcase.NewSpec(tb))
testcase.RunSuite(tb, sT)
assert.Must(t).True(sT.SpecWasCalled)
Expand Down
7 changes: 3 additions & 4 deletions T.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"time"

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal/teardown"
"github.com/adamluzsi/testcase/random"

"github.com/adamluzsi/testcase/internal"
)

// NewT returns a *testcase.T prepared for the given testing.TB
Expand Down Expand Up @@ -37,7 +36,7 @@ func newT(tb testing.TB, spec *Spec) *T {
spec: spec,
vars: newVariables(),
tags: spec.getTagSet(),
teardown: &internal.Teardown{CallerOffset: 1},
teardown: &teardown.Teardown{CallerOffset: 1},
}
}

Expand Down Expand Up @@ -66,7 +65,7 @@ type T struct {
spec *Spec
vars *variables
tags map[string]struct{}
teardown *internal.Teardown
teardown *teardown.Teardown

// TODO: protect it against concurrency
timerPaused bool
Expand Down
23 changes: 11 additions & 12 deletions T_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/contracts"
doubles2 "github.com/adamluzsi/testcase/internal/doubles"
"github.com/adamluzsi/testcase/sandbox"

"github.com/adamluzsi/testcase/random"

"github.com/adamluzsi/testcase/internal"

"github.com/adamluzsi/testcase"
)

Expand All @@ -24,7 +23,7 @@ var _ testing.TB = &testcase.T{}
func TestT_implementsTestingTB(t *testing.T) {
testcase.RunSuite(t, contracts.TestingTB{
Subject: func(t *testcase.T) testing.TB {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
t.Cleanup(stub.Finish)
return testcase.NewT(stub, nil)
},
Expand Down Expand Up @@ -131,7 +130,7 @@ func TestT_Defer_failNowWillNotHang(t *testing.T) {
go func() {
defer wg.Done()
defer recover()
s := testcase.NewSpec(&internal.RecorderTB{})
s := testcase.NewSpec(&doubles2.RecorderTB{})

s.Before(func(t *testcase.T) {
t.Defer(func() { t.FailNow() })
Expand Down Expand Up @@ -356,7 +355,7 @@ func TestT_Random(t *testing.T) {

func TestT_Eventually(t *testing.T) {
t.Run(`with default eventually retry strategy`, func(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
s := testcase.NewSpec(stub)
s.HasSideEffect()
var eventuallyRan bool
Expand All @@ -373,7 +372,7 @@ func TestT_Eventually(t *testing.T) {
})

t.Run(`with config passed`, func(t *testing.T) {
stub := &testcase.StubTB{}
stub := &doubles2.TB{}
var strategyUsed bool
strategy := assert.RetryStrategyFunc(func(condition func() bool) {
strategyUsed = true
Expand Down Expand Up @@ -402,7 +401,7 @@ func TestNewT(t *testing.T) {
Init: func(t *testcase.T) int { return t.Random.Int() },
}
t.Run(`with *Spec`, func(t *testing.T) {
tb := &testcase.StubTB{}
tb := &doubles2.TB{}
t.Cleanup(tb.Finish)
s := testcase.NewSpec(tb)
expectedY := rnd.Int()
Expand All @@ -412,7 +411,7 @@ func TestNewT(t *testing.T) {
assert.Must(t).Equal(v.Get(subject), v.Get(subject), `has test variable cache`)
})
t.Run(`without *Spec`, func(t *testing.T) {
tb := &testcase.StubTB{}
tb := &doubles2.TB{}
t.Cleanup(tb.Finish)
expectedY := rnd.Int()
subject := testcase.NewT(tb, nil)
Expand All @@ -421,7 +420,7 @@ func TestNewT(t *testing.T) {
assert.Must(t).Equal(v.Get(subject), v.Get(subject), `has test variable cache`)
})
t.Run(`with *testcase.T, same returned`, func(t *testing.T) {
tb := &testcase.StubTB{}
tb := &doubles2.TB{}
t.Cleanup(tb.Finish)
tcT1 := testcase.NewT(tb, nil)
tcT2 := testcase.NewT(tcT1, nil)
Expand All @@ -431,7 +430,7 @@ func TestNewT(t *testing.T) {
assert.Must(t).Nil(testcase.NewT(nil, nil))
})
t.Run(`when NewT is retrieved multiple times, hooks executed only once`, func(t *testing.T) {
stb := &testcase.StubTB{}
stb := &doubles2.TB{}
s := testcase.NewSpec(stb)
var out []struct{}
s.Before(func(t *testcase.T) {
Expand Down Expand Up @@ -474,7 +473,7 @@ func TestT_SkipUntil(t *testing.T) {
rnd := random.New(rand.NewSource(time.Now().UnixNano()))
future := time.Now().AddDate(0, 0, 1)
t.Run("before SkipUntil deadline, test is skipped", func(t *testing.T) {
stubTB := &testcase.StubTB{}
stubTB := &doubles2.TB{}
s := testcase.NewSpec(stubTB)
var ran bool
s.Test("", func(t *testcase.T) {
Expand All @@ -488,7 +487,7 @@ func TestT_SkipUntil(t *testing.T) {
assert.Must(t).Contain(stubTB.Logs.String(), fmt.Sprintf(skipUntilFormat, future.Format(timeLayout)))
})
t.Run("at or after SkipUntil deadline, test is failed", func(t *testing.T) {
stubTB := &testcase.StubTB{}
stubTB := &doubles2.TB{}
s := testcase.NewSpec(stubTB)
today := time.Now().AddDate(0, 0, -1*rnd.IntN(3))
var ran bool
Expand Down
5 changes: 3 additions & 2 deletions Var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal/doubles"
"github.com/adamluzsi/testcase/random"

"github.com/adamluzsi/testcase"
Expand All @@ -25,7 +26,7 @@ func TestVar(t *testing.T) {
var testVar = testcase.Var[int]{ID: rnd.StringNWithCharset(5, "abcdefghijklmnopqrstuvwxyz")}
expected := rnd.Int()

stub := &testcase.StubTB{}
stub := &doubles.TB{}
willFatal := willFatalWithMessageFn(stub)
willFatalWithVariableNotFoundMessage := func(s *testcase.Spec, tb testing.TB, varName string, blk func(*testcase.T)) {
tct := testcase.NewT(stub, s)
Expand Down Expand Up @@ -779,7 +780,7 @@ func TestVar_Before(t *testing.T) {

func TestVar_missingID(t *testing.T) {
varWithoutID := testcase.Var[string]{}
stub := &testcase.StubTB{}
stub := &doubles.TB{}
tct := testcase.NewT(stub, nil)
assert.Panic(t, func() { _ = varWithoutID.Get(tct) })
assert.Contain(t, stub.Logs.String(), "ID for testcase.Var[string] is missing. Maybe it's uninitialized?")
Expand Down
8 changes: 8 additions & 0 deletions doubles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testcase

import (
"github.com/adamluzsi/testcase/internal/doubles"
)

// StubTB is a stub that implements testing.TB
type StubTB = doubles.TB
7 changes: 4 additions & 3 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/internal"
doubles2 "github.com/adamluzsi/testcase/internal/doubles"
)

func TestEnvVarHelpers(t *testing.T) {
s := testcase.NewSpec(t)
s.Describe(`#SetEnv`, func(s *testcase.Spec) {
var (
recTB = testcase.Let(s, func(t *testcase.T) *internal.RecorderTB {
return &internal.RecorderTB{TB: &testcase.StubTB{}}
recTB = testcase.Let(s, func(t *testcase.T) *doubles2.RecorderTB {
return &doubles2.RecorderTB{TB: &doubles2.TB{}}
})
tbCleanupNow = func(t *testcase.T) { recTB.Get(t).CleanupNow() }
key = testcase.Let(s, func(t *testcase.T) string {
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestEnvVarHelpers(t *testing.T) {

s.Describe(`#UnsetEnv`, func(s *testcase.Spec) {
var (
recTB = testcase.Let(s, func(t *testcase.T) *internal.RecorderTB { return &internal.RecorderTB{} })
recTB = testcase.Let(s, func(t *testcase.T) *doubles2.RecorderTB { return &doubles2.RecorderTB{} })
tbCleanupNow = func(t *testcase.T) { recTB.Get(t).CleanupNow() }
key = testcase.Let(s, func(t *testcase.T) string {
return `TESTING_DATA_` + t.Random.StringNWithCharset(5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand Down
Loading

0 comments on commit 4101749

Please sign in to comment.