Skip to content

Commit

Permalink
extend support for Name usage with Spec based Suites
Browse files Browse the repository at this point in the history
The 'testcase.AsSuite' spec option allows you to provide a name to enhance convenience while using a Spec as a testing suite.
  • Loading branch information
adamluzsi committed May 26, 2023
1 parent 1c8dea8 commit 816f3e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ type Spec struct {
seed int64
isTest bool
sync bool
isSuite bool

isSuite bool
suiteName string
}

type (
Expand Down Expand Up @@ -647,6 +649,7 @@ func (spec *Spec) AsSuite() SpecSuite { return SpecSuite{S: spec} }

type SpecSuite struct{ S *Spec }

func (suite SpecSuite) Name() string { return suite.S.suiteName }
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) }
17 changes: 17 additions & 0 deletions Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1527,3 +1528,19 @@ func TestSpec_AsSuite(t *testing.T) {
assert.True(t, ran)
})
}

func TestRunSuite_spectAsSuite(t *testing.T) {
var name1, name2 string

suite1 := testcase.NewSpec(nil, testcase.AsSuite("Suite-namE-1"))
suite1.Test("tst1", func(t *testcase.T) { name1 = t.Name() })

suite2 := testcase.NewSpec(nil, testcase.AsSuite("Suite-namE-2"))
suite2.Test("tst2", func(t *testcase.T) { name2 = t.Name() })

dtb := &doubles.TB{}
testcase.RunSuite(dtb, suite1, suite2.AsSuite())

assert.True(t, strings.HasSuffix(name1, "Suite-namE-1/tst1"))
assert.True(t, strings.HasSuffix(name2, "Suite-namE-2/tst2"))
}
8 changes: 7 additions & 1 deletion Suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package testcase

import (
"fmt"
"strings"
"testing"

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

// AsSuite will flag the Spec as a Suite.
// Calling AsSuite will delay test until the Spec.Spec function is called
func AsSuite() SpecOption {
func AsSuite(name ...string) SpecOption {
return specOptionFunc(func(s *Spec) {
s.isSuite = true
if 0 < len(name) {
s.suiteName = strings.Join(name, " ")
}
})
}

Expand Down Expand Up @@ -89,6 +93,8 @@ func getSuiteName(c interface{}) (name string) {
switch c := c.(type) {
case interface{ Name() string }:
return c.Name()
case *Spec:
return c.suiteName
default:
return internal.SymbolicName(c)
}
Expand Down

0 comments on commit 816f3e6

Please sign in to comment.