Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit 1e8f72e

Browse files
committed
Add T.TODO() to return a child state
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") }) The child-state approach taken in this commit is closer to the latter, but allows for method chaining in the single-test case: t.TODO().Ok(Foo(), "foo") which I find easier to read. The root state is the only one which holds nextTestNumber. It might make more sense if it was *also* the only the state that held Writer (so internally t.root().printf(...) instead of t.printf(...)). However, Writer is public, and we can't keep folks from fiddling with it. Respecting local overrides makes as much sense as anything, although I doubt folks will want to override Writer in their TODO child state. [1]: http://testanything.org/tap-version-13-specification.html#todo-tests [2]: #6 (comment)
1 parent 2d799e5 commit 1e8f72e

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

Makefile

+1-1
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)

tap.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ 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+
parent *T
35+
nextTestNumber int // only tracked on the root T instance
36+
todo bool
3537

3638
// Writer indicates where TAP output should be sent. The default is os.Stdout.
3739
Writer io.Writer
@@ -44,6 +46,13 @@ func New() *T {
4446
}
4547
}
4648

49+
func (t *T) root() *T {
50+
if t.parent == nil {
51+
return t
52+
}
53+
return t.parent.root()
54+
}
55+
4756
func (t *T) w() io.Writer {
4857
if t.Writer == nil {
4958
return os.Stdout
@@ -74,8 +83,13 @@ func (t *T) Ok(test bool, description string) {
7483
ok = "not ok"
7584
}
7685

77-
t.printf("%s %d - %s\n", ok, t.nextTestNumber, description)
78-
t.nextTestNumber++
86+
root := t.root()
87+
if t.todo {
88+
t.printf("%s %d # TODO %s\n", ok, root.nextTestNumber, description)
89+
} else {
90+
t.printf("%s %d - %s\n", ok, root.nextTestNumber, description)
91+
}
92+
root.nextTestNumber++
7993
}
8094

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

106120
// Count returns the number of tests completed so far.
107121
func (t *T) Count() int {
108-
return t.nextTestNumber - 1
122+
return t.root().nextTestNumber - 1
109123
}
110124

111125
// AutoPlan generates a test plan based on the number of tests that were run.
@@ -117,11 +131,22 @@ func escapeNewlines(s string) string {
117131
return strings.Replace(strings.TrimRight(s, "\n"), "\n", "\n# ", -1)
118132
}
119133

134+
// TODO returns a test-state which uses a TODO directive for Ok, Fail,
135+
// Pass, and similar.
136+
func (t *T) TODO() *T {
137+
return &T{
138+
parent: t,
139+
todo: true,
140+
Writer: t.Writer,
141+
}
142+
}
143+
120144
// Skip indicates that a test has been skipped.
121145
func (t *T) Skip(count int, description string) {
146+
root := t.root()
122147
for i := 0; i < count; i++ {
123-
t.printf("ok %d # SKIP %s\n", t.nextTestNumber, description)
124-
t.nextTestNumber++
148+
t.printf("ok %d # SKIP %s\n", root.nextTestNumber, description)
149+
root.nextTestNumber++
125150
}
126151
}
127152

test/todo/main.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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(5)
18+
todo := t.TODO()
19+
todo.Ok(false, "bend space and time")
20+
todo.Fail("bend space and time further")
21+
22+
buf2 := new(bytes.Buffer)
23+
todo.Writer = io.MultiWriter(os.Stdout, buf2)
24+
todo.Fail("use another writer")
25+
26+
got := buf.String()
27+
t.Ok(got == expected, "TODO gave expected output via the root writer")
28+
29+
got = buf2.String()
30+
t.Ok(got == "not ok 3 # TODO use another writer\n", "TODO gave expected output via a child writer")
31+
}
32+
33+
const expected = `TAP version 13
34+
1..5
35+
not ok 1 # TODO bend space and time
36+
not ok 2 # TODO bend space and time further
37+
`

0 commit comments

Comments
 (0)