From 031cb25ca9064c801321d53d5d16c7eb0d722fe4 Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Sun, 13 Oct 2024 15:14:05 +0200 Subject: [PATCH] make sure that tests in a spec suite are always grouped together This improves readability and reduces the need for repetitive code when integrating a suite into a top-level spec. --- Spec.go | 7 +++++-- Spec_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Spec.go b/Spec.go index cd1cc78..1e23096 100644 --- a/Spec.go +++ b/Spec.go @@ -833,7 +833,10 @@ type SpecSuite struct { } func (suite SpecSuite) Name() string { return suite.N } -func (suite SpecSuite) Spec(s *Spec) { suite.S.Spec(s) } + +func (suite SpecSuite) Spec(s *Spec) { + s.Context(suite.N, suite.S.Spec, Group(suite.N)) +} func (suite SpecSuite) Test(t *testing.T) { suite.run(t) } func (suite SpecSuite) Benchmark(b *testing.B) { suite.run(b) } @@ -841,7 +844,7 @@ func (suite SpecSuite) Benchmark(b *testing.B) { suite.run(b) } func (suite SpecSuite) run(tb testing.TB) { s := NewSpec(tb) defer s.Finish() - s.Context(suite.N, suite.Spec, Group(suite.N)) + suite.Spec(s) } func helper(tb testingHelper) testingHelper { diff --git a/Spec_test.go b/Spec_test.go index d0d23c4..03eaae2 100644 --- a/Spec_test.go +++ b/Spec_test.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "testing" "time" @@ -1730,3 +1731,59 @@ func TestSpecSuite(t *testing.T) { assert.Equal(t, ran, ir+1) }) } + +func TestSpecSuite_name(t *testing.T) { + t.Run("when suite used directly", func(t *testing.T) { + var ran int32 + t.Run("begin", func(t *testing.T) { + suite := func() testcase.Suite { + s := testcase.NewSpec(nil) + s.Describe("#ok", func(s *testcase.Spec) { + s.Test("nok", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/suite1/#ok/nok") + }) + }) + return s.AsSuite("suite1") + }() + s := testcase.NewSpec(t) + s.Test("foo", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/foo") + }) + suite.Spec(s) + s.Test("bar", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/bar") + }) + }) + assert.Equal(t, atomic.LoadInt32(&ran), 3) + }) + + t.Run("when suite used as context block", func(t *testing.T) { + var ran int32 + t.Run("begin", func(t *testing.T) { + suite := func() testcase.Suite { + s := testcase.NewSpec(nil) + s.Describe("#ok", func(s *testcase.Spec) { + s.Test("nok", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/2suite/suite2/#ok/nok") + }) + }) + return s.AsSuite("suite2") + }() + s := testcase.NewSpec(t) + s.Test("foo", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/foo") + }) + s.Describe("2suite", suite.Spec) + s.Test("bar", func(t *testcase.T) { + atomic.AddInt32(&ran, 1) + assert.Contain(t, t.Name(), "/begin/bar") + }) + }) + assert.Equal(t, atomic.LoadInt32(&ran), 3) + }) +}