Skip to content

Commit 130f1eb

Browse files
committed
Merge remote-tracking branch 'wking/directives'
2 parents 92ea411 + 323dc3d commit 130f1eb

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
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 known failing 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: 28 additions & 5 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,6 +125,21 @@ 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+
135+
// Skip indicates that a test has been skipped.
136+
func (t *T) Skip(count int, description string) {
137+
for i := 0; i < count; i++ {
138+
t.printf("ok %d # SKIP %s\n", *t.nextTestNumber, description)
139+
(*t.nextTestNumber)++
140+
}
141+
}
142+
120143
// Diagnostic generates a diagnostic from the message,
121144
// which may span multiple lines.
122145
func (t *T) Diagnostic(message string) {

util/tap/test/skip/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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(4)
18+
t.Skip(1, "insufficient flogiston pressure")
19+
t.Skip(2, "no /sys directory")
20+
21+
got := buf.String()
22+
t.Ok(got == expected, "skip gave expected output")
23+
}
24+
25+
const expected = `TAP version 13
26+
1..4
27+
ok 1 # SKIP insufficient flogiston pressure
28+
ok 2 # SKIP no /sys directory
29+
ok 3 # SKIP no /sys directory
30+
`

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)