Skip to content

Commit 6317226

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 d8e35a4 commit 6317226

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

util/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

0 commit comments

Comments
 (0)