Skip to content

Commit 1c3ba86

Browse files
committed
Allow for embedded newlines in diagnostics.
1 parent 0fa2830 commit 1c3ba86

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

util/tap/tap.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"io"
2626
"os"
27+
"strings"
2728
)
2829
import "testing/quick"
2930

@@ -112,12 +113,18 @@ func (t *T) AutoPlan() {
112113
t.printf("1..%d\n", t.Count())
113114
}
114115

115-
// Diagnostic generates a diagnostic from the message.
116+
func escapeNewlines(s string) string {
117+
return strings.Replace(strings.TrimRight(s, "\n"), "\n", "\n# ", -1)
118+
}
119+
120+
// Diagnostic generates a diagnostic from the message,
121+
// which may span multiple lines.
116122
func (t *T) Diagnostic(message string) {
117-
t.printf("# %s\n", message)
123+
t.printf("# %s\n", escapeNewlines(message))
118124
}
119125

120-
// Diagnosticf generates a diagnostic from the format string and arguments.
126+
// Diagnosticf generates a diagnostic from the format string and arguments,
127+
// which may span multiple lines.
121128
func (t *T) Diagnosticf(format string, a ...interface{}) {
122-
t.printf("# "+format+"\n", a...)
129+
t.printf("# "+escapeNewlines(format)+"\n", a...)
123130
}

util/tap/test/diagnostic/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ func main() {
77
t.Header(1)
88
t.Diagnostic("expecting all to be well")
99
t.Diagnosticf("here's some perfectly magical output: %d %s 0x%X.", 6, "abracadabra", 28)
10+
t.Diagnostic("some\nmultiline\ntext\n")
11+
t.Diagnosticf("%d lines\n%s multiline\ntext", 3, "more")
1012
t.Pass("all good")
1113
}

0 commit comments

Comments
 (0)