Skip to content

Commit 379080b

Browse files
committed
add TestData.String and use it for logging
`TestData.String()` renders the entire testcase and we use it for better error and logging output. We also improve the logging output using better indentation.
1 parent 77c18f3 commit 379080b

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

datadriven.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func RunTest(t *testing.T, path string, f func(t *testing.T, d *TestData) string
122122

123123
// RunTestAny is like RunTest but works over a testing.TB.
124124
func RunTestAny(t testing.TB, path string, f func(t testing.TB, d *TestData) string) {
125+
t.Helper()
126+
125127
mode := os.O_RDONLY
126128
if *rewriteTestFiles {
127129
// We only open read-write if rewriting, so as to enable running
@@ -378,19 +380,14 @@ func runDirective(t testing.TB, r *testDataReader, f func(testing.TB, *TestData)
378380
B: actualLines,
379381
})
380382
if err == nil {
381-
t.Fatalf("\n%s:\n %s\noutput didn't match expected:\n%s", d.Pos, d.Input, diff)
383+
t.Fatalf("\n%s:\n%soutput didn't match expected:\n%s", indentLines(d.Pos), indentLines(d.String()), indentLines(diff))
382384
return
383385
}
384386
t.Logf("Failed to produce diff %v", err)
385387
}
386-
t.Fatalf("\n%s:\n %s\nexpected:\n%s\nfound:\n%s", d.Pos, d.Input, d.Expected, actual)
388+
t.Fatalf("\n%s:\n%s\nexpected:\n%s\nfound:\n%s", d.Pos, indentLines(d.String()), indentLines(d.Expected), indentLines(actual))
387389
} else if Verbose() {
388-
input := d.Input
389-
if input == "" {
390-
input = "<no input to command>"
391-
}
392-
// TODO(tbg): it's awkward to reproduce the args, but it would be helpful.
393-
t.Logf("\n%s:\n%s [%d args]\n%s\n----\n%s", d.Pos, d.Cmd, len(d.CmdArgs), input, actual)
390+
t.Logf("\n%s:\n%s ----\n%s", d.Pos, indentLines(d.String()), indentLines(actual))
394391
}
395392
return
396393
}
@@ -515,6 +512,24 @@ type TestData struct {
515512
Rewrite bool
516513
}
517514

515+
// FullCmd renders the command and the arguments.
516+
func (td *TestData) FullCmd() string {
517+
fields := make([]string, 0, len(td.CmdArgs)+1)
518+
fields = append(fields, td.Cmd)
519+
for i := range td.CmdArgs {
520+
fields = append(fields, td.CmdArgs[i].String())
521+
}
522+
return strings.Join(fields, " ")
523+
}
524+
525+
// String renders the entire testcase. The string ends in a newline.
526+
func (td *TestData) String() string {
527+
if td.Input == "" {
528+
return td.FullCmd() + "\n"
529+
}
530+
return fmt.Sprintf("%s\n%s\n", td.FullCmd(), td.Input)
531+
}
532+
518533
// HasArg checks whether the CmdArgs array contains an entry for the given key.
519534
func (td *TestData) HasArg(key string) bool {
520535
_, ok := td.Arg(key)
@@ -848,3 +863,20 @@ func hasBlankLine(s string) bool {
848863
// want that final match to be included, so we force the end-of-line
849864
// match using "\n" specifically.
850865
var blankLineRe = regexp.MustCompile(`(?m)^[\t ]*\n`)
866+
867+
func indentLines(str string) string {
868+
var b strings.Builder
869+
if str == "" {
870+
return ""
871+
}
872+
for i, l := range strings.Split(str, "\n") {
873+
if i > 0 {
874+
b.WriteString("\n")
875+
}
876+
if l != "" {
877+
b.WriteString(" ")
878+
b.WriteString(l)
879+
}
880+
}
881+
return b.String()
882+
}

datadriven_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ time.Duration vals=10.0m
385385
})
386386
}
387387

388+
func TestString(t *testing.T) {
389+
RunTest(t, "testdata/string", func(t *testing.T, d *TestData) string {
390+
return d.String()
391+
})
392+
}
393+
388394
func BenchmarkInput(b *testing.B) {
389395
RunTestFromStringAny(b, `
390396
foo

testdata/multiline

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
small
22
----
3-
----
43
just
54
two lines of output
6-
----
7-
----
85

96
large
107
----
11-
----
128
more
139
than
1410
five
1511
lines
1612
of
1713
output
18-
----
19-
----

testdata/string

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
simple
2+
input-line-1
3+
input-line-2
4+
----
5+
simple
6+
input-line-1
7+
input-line-2
8+
9+
with some args=2
10+
input-line-1
11+
input-line-2
12+
----
13+
with some args=2
14+
input-line-1
15+
input-line-2
16+
17+
args that=(1) render=(1,2) differently=(1,2,3)
18+
input-line-1
19+
input-line-2
20+
----
21+
args that=1 render=(1, 2) differently=(1, 2, 3)
22+
input-line-1
23+
input-line-2
24+
25+
noinput
26+
----
27+
noinput

0 commit comments

Comments
 (0)