Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions datadriven.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func RunTest(t *testing.T, path string, f func(t *testing.T, d *TestData) string

// RunTestAny is like RunTest but works over a testing.TB.
func RunTestAny(t testing.TB, path string, f func(t testing.TB, d *TestData) string) {
t.Helper()

mode := os.O_RDONLY
if *rewriteTestFiles {
// We only open read-write if rewriting, so as to enable running
Expand Down Expand Up @@ -378,19 +380,14 @@ func runDirective(t testing.TB, r *testDataReader, f func(testing.TB, *TestData)
B: actualLines,
})
if err == nil {
t.Fatalf("\n%s:\n %s\noutput didn't match expected:\n%s", d.Pos, d.Input, diff)
t.Fatalf("\n%s:\n%soutput didn't match expected:\n%s", indentLines(d.Pos), indentLines(d.String()), indentLines(diff))
return
}
t.Logf("Failed to produce diff %v", err)
}
t.Fatalf("\n%s:\n %s\nexpected:\n%s\nfound:\n%s", d.Pos, d.Input, d.Expected, actual)
t.Fatalf("\n%s:\n%s\nexpected:\n%s\nfound:\n%s", d.Pos, indentLines(d.String()), indentLines(d.Expected), indentLines(actual))
} else if Verbose() {
input := d.Input
if input == "" {
input = "<no input to command>"
}
// TODO(tbg): it's awkward to reproduce the args, but it would be helpful.
t.Logf("\n%s:\n%s [%d args]\n%s\n----\n%s", d.Pos, d.Cmd, len(d.CmdArgs), input, actual)
t.Logf("\n%s:\n%s ----\n%s", d.Pos, indentLines(d.String()), indentLines(actual))
}
return
}
Expand Down Expand Up @@ -515,6 +512,24 @@ type TestData struct {
Rewrite bool
}

// FullCmd renders the command and the arguments.
func (td *TestData) FullCmd() string {
fields := make([]string, 0, len(td.CmdArgs)+1)
fields = append(fields, td.Cmd)
for i := range td.CmdArgs {
fields = append(fields, td.CmdArgs[i].String())
}
return strings.Join(fields, " ")
}

// String renders the entire testcase. The string ends in a newline.
func (td *TestData) String() string {
if td.Input == "" {
return td.FullCmd() + "\n"
}
return fmt.Sprintf("%s\n%s\n", td.FullCmd(), td.Input)
}

// HasArg checks whether the CmdArgs array contains an entry for the given key.
func (td *TestData) HasArg(key string) bool {
_, ok := td.Arg(key)
Expand Down Expand Up @@ -848,3 +863,20 @@ func hasBlankLine(s string) bool {
// want that final match to be included, so we force the end-of-line
// match using "\n" specifically.
var blankLineRe = regexp.MustCompile(`(?m)^[\t ]*\n`)

func indentLines(str string) string {
var b strings.Builder
if str == "" {
return ""
}
for i, l := range strings.Split(str, "\n") {
if i > 0 {
b.WriteString("\n")
}
if l != "" {
b.WriteString(" ")
b.WriteString(l)
}
}
return b.String()
}
6 changes: 6 additions & 0 deletions datadriven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ time.Duration vals=10.0m
})
}

func TestString(t *testing.T) {
RunTest(t, "testdata/string", func(t *testing.T, d *TestData) string {
return d.String()
})
}

func BenchmarkInput(b *testing.B) {
RunTestFromStringAny(b, `
foo
Expand Down
6 changes: 0 additions & 6 deletions testdata/multiline
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
small
----
----
just
two lines of output
----
----

large
----
----
more
than
five
lines
of
output
----
----
27 changes: 27 additions & 0 deletions testdata/string
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
simple
input-line-1
input-line-2
----
simple
input-line-1
input-line-2

with some args=2
input-line-1
input-line-2
----
with some args=2
input-line-1
input-line-2

args that=(1) render=(1,2) differently=(1,2,3)
input-line-1
input-line-2
----
args that=1 render=(1, 2) differently=(1, 2, 3)
input-line-1
input-line-2

noinput
----
noinput