| 1 | package printer_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/mmcloughlin/avo/build" |
| 8 | "github.com/mmcloughlin/avo/printer" |
| 9 | ) |
| 10 | |
| 11 | func AssertPrintsLines(t *testing.T, ctx *build.Context, pb printer.Builder, expect []string) { |
| 12 | t.Helper() |
| 13 | |
| 14 | output := Print(t, ctx, pb) |
| 15 | lines := strings.Split(output, "\n") |
| 16 | |
| 17 | if len(expect) != len(lines) { |
| 18 | t.Logf("output:\n%s", output) |
| 19 | t.Fatalf("have %d lines of output; expected %d", len(lines), len(expect)) |
| 20 | } |
| 21 | |
| 22 | for i := range expect { |
| 23 | if expect[i] != lines[i] { |
| 24 | t.Errorf("mismatch on line %d:\n\tgot\t%s\n\texpect\t%s\n", i, lines[i], expect[i]) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func Print(t *testing.T, ctx *build.Context, pb printer.Builder) string { |
| 30 | t.Helper() |
| 31 | |
| 32 | f, errs := ctx.Result() |
| 33 | if errs != nil { |
| 34 | t.Fatal(errs) |
| 35 | } |
| 36 | |
| 37 | p := pb(printer.NewDefaultConfig()) |
| 38 | b, err := p.Print(f) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | |
| 43 | return string(b) |
| 44 | } |