Skip to content

Commit

Permalink
support OpenSuite interface with Spec to enable non-testcase specific…
Browse files Browse the repository at this point in the history
… suite generation
  • Loading branch information
adamluzsi committed May 19, 2023
1 parent e08faa8 commit 1c8dea8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
16 changes: 12 additions & 4 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// NewSpec create new Spec struct that is ready for usage.
func NewSpec(tb testing.TB, opts ...SpecOption) *Spec {
tb = ensureTB(tb)
tb, opts = checkSuite(tb, opts)
tb.Helper()
var s *Spec
switch tb := tb.(type) {
Expand Down Expand Up @@ -636,9 +636,17 @@ func (spec *Spec) getIsSuite() bool {
return false
}

func ensureTB(tb testing.TB) testing.TB {
func checkSuite(tb testing.TB, opts []SpecOption) (testing.TB, []SpecOption) {
if tb == nil {
return internal.SuiteNullTB{}
return internal.SuiteNullTB{}, append(opts, AsSuite())
}
return tb
return tb, opts
}

func (spec *Spec) AsSuite() SpecSuite { return SpecSuite{S: spec} }

type SpecSuite struct{ S *Spec }

func (suite SpecSuite) Test(t *testing.T) { suite.S.Spec(NewSpec(t)) }
func (suite SpecSuite) Benchmark(b *testing.B) { suite.S.Spec(NewSpec(b)) }
func (suite SpecSuite) Spec(s *Spec) { suite.S.Spec(s) }
54 changes: 53 additions & 1 deletion Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ func TestSpec_spike(t *testing.T) {

}

func TestAsSuite(t *testing.T) {
func TestSpec_Spec(t *testing.T) {
t.Run("runs only when Spec method is called", func(t *testing.T) {
s := testcase.NewSpec(nil, testcase.AsSuite())
s.Sequential()
Expand Down Expand Up @@ -1466,7 +1466,22 @@ func TestAsSuite(t *testing.T) {

assert.True(t, ran)
})
t.Run("Spec made with nil testing.TB is interpreted as a Suite", func(t *testing.T) {
var ran bool
s1 := testcase.NewSpec(nil)
s1.Test("", func(t *testcase.T) { ran = true })

s2 := testcase.NewSpec(nil)
s1.Spec(s2) // s1 merge into s2

assert.False(t, ran)

dtb := &doubles.TB{}
s3 := testcase.NewSpec(dtb)
s2.Spec(s3) // execute

assert.True(t, ran)
})
t.Run("when Spec.Spec is called on non Suite Spec", func(t *testing.T) {
dtb := &doubles.TB{}
s := testcase.NewSpec(dtb)
Expand All @@ -1475,3 +1490,40 @@ func TestAsSuite(t *testing.T) {
})
})
}

func TestSpec_AsSuite(t *testing.T) {
t.Run(".Suite", func(t *testing.T) {
var ran bool
s1 := testcase.NewSpec(nil)
s1.Test("", func(t *testcase.T) { ran = true })
assert.False(t, ran)

dtb := &doubles.TB{}
s2 := testcase.NewSpec(dtb)
s1.AsSuite().Spec(s2) // execute
assert.True(t, ran)
})
t.Run(".Test", func(t *testing.T) {
var ran bool
s1 := testcase.NewSpec(nil)
s1.Test("", func(t *testcase.T) { ran = true })
assert.False(t, ran)

s1.AsSuite().Test(t) // execute
assert.True(t, ran)
})
t.Run(".Benchmark", func(t *testing.T) {
var ran bool
s1 := testcase.NewSpec(nil)
s1.Test("", func(t *testcase.T) {
ran = true
t.Skip()
})
assert.False(t, ran)

testing.Benchmark(func(b *testing.B) {
s1.AsSuite().Benchmark(b) // execute
})
assert.True(t, ran)
})
}
26 changes: 22 additions & 4 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,12 @@ func Example_global_Before() {
})
}

func ExampleAsSuite() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Context("my example testing suite", exampleSuite().Spec)
}

func exampleSuite() testcase.Suite {
s := testcase.NewSpec(nil, testcase.AsSuite())
s.Test("foo", func(t *testcase.T) {
Expand All @@ -1520,8 +1526,20 @@ func exampleSuite() testcase.Suite {
return s
}

func ExampleAsSuite() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Context("my example testing suite", exampleSuite().Spec)
func ExampleSpec_AsSuite() {
suite := exampleOpenSuite()

var t *testing.T
suite.Test(t)

var b *testing.B
suite.Benchmark(b)
}

func exampleOpenSuite() testcase.OpenSuite {
s := testcase.NewSpec(nil, testcase.AsSuite())
s.Test("foo", func(t *testcase.T) {
// OK
})
return s.AsSuite()
}

0 comments on commit 1c8dea8

Please sign in to comment.