Skip to content

Commit 323dc3d

Browse files
committed
Add T.TODO and t.Todo() to drive the TODO directive
The spec [1] suggests "not ok 13 # TODO ...", but if you attempt this with: t.Fail("# TODO foo") you get an extra hyphen ("not ok 13 - # TODO foo") which is parsed as a failed test (and not a TODO test). I'd initially written up an alternative to Ok: T.TODO(test bool, description string) but Michael pointed out that future work may add additional test methods (e.g. EqOk) and wanted to support those as well with something like [2]: t.TODO = true t.Ok(Foo(), "foo") t.EqOk(Bar(), Baz(), "bar == baz") t.TODO = false // could be done via defer too or [2]: t.TODO( func () { t.Ok(Foo(), "foo") t.EqOk(Bar(), Baz(), "bar == baz") }) This commit adds a .TODO boolean following Michael's first proposal. It also adds a Todo() method returning a copy of the test-state (but setting TODO), which you can use to avoid altering the original test-state's TODO value. This can be useful when you don't want to bother with a defer, or your TODO tests all live in an existing branch of a function, or you want to chain methods for a one-off TODO test: t.TODO().Ok(Foo(), "foo") To keep the count synchronized between sibling test states, nextTestNumber is now a pointer. We don't do anything to keep Writer synchronized, because it's already part of the public API as a non-pointer, and Michael is ok leaving this up to the caller [3]. [1]: http://testanything.org/tap-version-13-specification.html#todo-tests [2]: mndrix/tap-go#6 (comment) [3]: mndrix/tap-go#6 (comment)
1 parent 81d3a42 commit 323dc3d

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

util/tap/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TESTS = auto check diagnostic failing known skip writer
1+
TESTS = auto check diagnostic failing known skip todo writer
22
GOPATH = $(CURDIR)/gopath
33

44
.PHONY: $(TESTS)

util/tap/tap.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ import "testing/quick"
3131
// T is a type to encapsulate test state. Methods on this type generate TAP
3232
// output.
3333
type T struct {
34-
nextTestNumber int
34+
nextTestNumber *int
35+
36+
// TODO toggles the TODO directive for Ok, Fail, Pass, and similar.
37+
TODO bool
3538

3639
// Writer indicates where TAP output should be sent. The default is os.Stdout.
3740
Writer io.Writer
3841
}
3942

4043
// New creates a new Tap value
4144
func New() *T {
45+
nextTestNumber := 1
4246
return &T{
43-
nextTestNumber: 1,
47+
nextTestNumber: &nextTestNumber,
4448
}
4549
}
4650

@@ -74,8 +78,12 @@ func (t *T) Ok(test bool, description string) {
7478
ok = "not ok"
7579
}
7680

77-
t.printf("%s %d - %s\n", ok, t.nextTestNumber, description)
78-
t.nextTestNumber++
81+
if t.TODO {
82+
t.printf("%s %d # TODO %s\n", ok, *t.nextTestNumber, description)
83+
} else {
84+
t.printf("%s %d - %s\n", ok, *t.nextTestNumber, description)
85+
}
86+
(*t.nextTestNumber)++
7987
}
8088

8189
// Fail indicates that a test has failed. This is typically only used when the
@@ -105,7 +113,7 @@ func (t *T) Check(function interface{}, description string) {
105113

106114
// Count returns the number of tests completed so far.
107115
func (t *T) Count() int {
108-
return t.nextTestNumber - 1
116+
return *t.nextTestNumber - 1
109117
}
110118

111119
// AutoPlan generates a test plan based on the number of tests that were run.
@@ -117,11 +125,18 @@ func escapeNewlines(s string) string {
117125
return strings.Replace(strings.TrimRight(s, "\n"), "\n", "\n# ", -1)
118126
}
119127

128+
// Todo returns copy of the test-state with TODO set.
129+
func (t *T) Todo() *T {
130+
newT := *t
131+
newT.TODO = true
132+
return &newT
133+
}
134+
120135
// Skip indicates that a test has been skipped.
121136
func (t *T) Skip(count int, description string) {
122137
for i := 0; i < count; i++ {
123-
t.printf("ok %d # SKIP %s\n", t.nextTestNumber, description)
124-
t.nextTestNumber++
138+
t.printf("ok %d # SKIP %s\n", *t.nextTestNumber, description)
139+
(*t.nextTestNumber)++
125140
}
126141
}
127142

util/tap/test/todo/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
8+
tap "github.com/mndrix/tap-go"
9+
)
10+
11+
func main() {
12+
// collect output for comparison later
13+
buf := new(bytes.Buffer)
14+
t := tap.New()
15+
t.Writer = io.MultiWriter(os.Stdout, buf)
16+
17+
t.Header(6)
18+
t.TODO = true
19+
t.Ok(false, "using Ok(false, ...) in TODO mode")
20+
t.Fail("using Fail(...) in TODO mode")
21+
t.TODO = false
22+
t.Ok(true, "using Ok(false, ...) after leaving TODO mode")
23+
24+
t.Todo().Fail("using Fail(...) in TODO mode with method chaining")
25+
t.Pass("using Pass(...) after Todo method chaining")
26+
27+
got := buf.String()
28+
t.Ok(got == expected, "TODO gave expected output")
29+
}
30+
31+
const expected = `TAP version 13
32+
1..6
33+
not ok 1 # TODO using Ok(false, ...) in TODO mode
34+
not ok 2 # TODO using Fail(...) in TODO mode
35+
ok 3 - using Ok(false, ...) after leaving TODO mode
36+
not ok 4 # TODO using Fail(...) in TODO mode with method chaining
37+
ok 5 - using Pass(...) after Todo method chaining
38+
`

0 commit comments

Comments
 (0)