Skip to content

Commit

Permalink
introduce TODO syntax sugar for Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Jul 28, 2024
1 parent 1549674 commit 06fa756
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
9 changes: 9 additions & 0 deletions DSL.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (spec *Spec) Then(desc string, test tBlock, opts ...SpecOption) {
spec.Test(desc, test, opts...)
}

// TODO allows you to leave notes for a given specification's context,
// which will be visible when the test specification is executed.
func (spec *Spec) TODO(task string) {
helper(spec.testingTB).Helper()
desc := fmt.Sprintf(`TODO: %s`, task)
skip := func(t *T) { t.Skip() }
spec.Test(desc, skip)
}

// NoSideEffect gives a hint to the reader of the current test that during the test execution,
// no side effect outside from the test specification scope is expected to be observable.
// It is important to note that this flag primary meant to represent the side effect possibility
Expand Down
34 changes: 34 additions & 0 deletions DSL_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package testcase_test

import (
"testing"

"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal"
"go.llib.dev/testcase/internal/doubles"
)

func TestSpec_TODO(t *testing.T) {
dtb := &doubles.TB{}
internal.StubVerbose(t, true)

todos := []string{"abc", "bcd", "cde"}
s := testcase.NewSpec(dtb)
for _, todo := range todos {
s.TODO(todo)
}
s.Finish()
dtb.Finish()

assert.False(t, dtb.IsFailed)
assert.False(t, dtb.IsSkipped)

for _, todo := range todos {
assert.OneOf(t, dtb.Tests, func(t assert.It, got *doubles.TB) {
assert.Contain(t, got.Name(), "TODO: "+todo)
assert.False(t, got.IsFailed)
assert.True(t, got.IsSkipped)
})
}
}
7 changes: 6 additions & 1 deletion clock/Clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,15 @@ func TestNewTicker(t *testing.T) {
timecop.SetSpeed(t, 1000) // 100x times faster
time.Sleep(time.Second/4 + time.Microsecond)
runtime.Gosched()

// TODO: flaky assertion
//
// FLAKY*
expectedTickCount += 100 / 4 * 1000 * failureRateMultiplier
t.Log("exp:", expectedTickCount, "got:", atomic.LoadInt64(&ticks))
assert.True(t, expectedTickCount <= atomic.LoadInt64(&ticks))
}) // TODO: FLAKY test
// *FLAKY
})

t.Run("race", func(t *testing.T) {
ticker := clock.NewTicker(time.Minute)
Expand Down
10 changes: 5 additions & 5 deletions internal/doubles/TB.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type TB struct {
td teardown.Teardown
mutex sync.Mutex

RunTBs []*TB
Tests []*TB
}

func (m *TB) Finish() {
Expand Down Expand Up @@ -143,10 +143,10 @@ func (m *TB) Setenv(key, value string) {

func (m *TB) Run(name string, blk func(tb testing.TB)) bool {
if name == "" {
name = fmt.Sprintf("%d", time.Now().UnixNano())
name = fmt.Sprintf("%d", len(m.Tests))
}
dtb := &TB{TB: m.TB, StubName: m.Name() + "/" + name}
m.RunTBs = append(m.RunTBs, dtb)
m.Tests = append(m.Tests, dtb)
sandbox.Run(func() { blk(dtb) })
if dtb.IsFailed {
m.Error(dtb.Logs.String())
Expand All @@ -155,10 +155,10 @@ func (m *TB) Run(name string, blk func(tb testing.TB)) bool {
}

func (m *TB) LastRunTB() (*TB, bool) {
if len(m.RunTBs) == 0 {
if len(m.Tests) == 0 {
return nil, false
}
return m.RunTBs[len(m.RunTBs)-1], true
return m.Tests[len(m.Tests)-1], true
}

func (m *TB) LastTB() *TB {
Expand Down
2 changes: 1 addition & 1 deletion internal/doubles/TB_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"go.llib.dev/testcase/internal/doubles"
)

func TestStubTB(t *testing.T) {
func TestTB(t *testing.T) {
s := testcase.NewSpec(t)

var stub = testcase.Let(s, func(t *testcase.T) *doubles.TB {
Expand Down
9 changes: 7 additions & 2 deletions internal/verbose.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ func Verbose() bool {

var verbose = testing.Verbose

func StubVerbose(tb testing.TB, fn func() bool) {
func StubVerbose[T bool | func() bool](tb testing.TB, v T) {
tb.Cleanup(func() { verbose = testing.Verbose })
verbose = fn
switch v := any(v).(type) {
case bool:
verbose = func() bool { return v }
case func() bool:
verbose = v
}
}

0 comments on commit 06fa756

Please sign in to comment.