From 7474489b9c32193b1e18ea188b20bf497e4e198e Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 28 Apr 2017 16:37:36 +0300 Subject: [PATCH] tests progress formatter output production --- .travis.yml | 3 +- fmt_junit_test.go | 2 +- fmt_progress.go | 3 +- fmt_progress_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 fmt_progress_test.go diff --git a/.travis.yml b/.travis.yml index a87782d..3a6d8c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,10 +10,9 @@ go_import_path: github.com/DATA-DOG/godog install: go install github.com/DATA-DOG/godog/cmd/godog script: - # run standard go tests - go vet ./... - go fmt ./... - - go test -race -coverprofile=coverage.txt -covermode=atomic + - go test -v -race -coverprofile=coverage.txt -covermode=atomic after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/fmt_junit_test.go b/fmt_junit_test.go index 6da1eaf..0a5fa5c 100644 --- a/fmt_junit_test.go +++ b/fmt_junit_test.go @@ -49,7 +49,7 @@ Feature: junit formatter | passing | undefined | ` -func TestJUnitSimpleFeature(t *testing.T) { +func TestJUnitFormatterOutput(t *testing.T) { feat, err := gherkin.ParseFeature(strings.NewReader(sampleGherkinFeature)) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/fmt_progress.go b/fmt_progress.go index f1af01c..6e1cc1a 100644 --- a/fmt_progress.go +++ b/fmt_progress.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "math" + "strings" "sync" "github.com/DATA-DOG/godog/gherkin" @@ -57,7 +58,7 @@ func (f *progress) Summary() { if len(f.failed) > 0 { fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n") for _, fail := range f.failed { - fmt.Fprintln(f.out, s(4)+red(fail.step.Keyword+" "+fail.step.Text)+black(" # "+fail.line())) + fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line())) fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n") } } diff --git a/fmt_progress_test.go b/fmt_progress_test.go new file mode 100644 index 0000000..5b48b97 --- /dev/null +++ b/fmt_progress_test.go @@ -0,0 +1,90 @@ +package godog + +import ( + "bytes" + "fmt" + "os" + "strings" + "testing" + "time" + + "github.com/DATA-DOG/godog/colors" + "github.com/DATA-DOG/godog/gherkin" +) + +func TestProgressFormatterOutput(t *testing.T) { + feat, err := gherkin.ParseFeature(strings.NewReader(sampleGherkinFeature)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + var buf bytes.Buffer + w := colors.Uncolored(&buf) + s := &Suite{ + fmt: progressFunc("progress", w), + features: []*feature{&feature{ + Path: "any.feature", + Feature: feat, + Content: []byte(sampleGherkinFeature), + }}, + } + + s.Step(`^passing$`, func() error { return nil }) + s.Step(`^failing$`, func() error { return fmt.Errorf("errored") }) + s.Step(`^pending$`, func() error { return ErrPending }) + + // var zeroDuration time.Duration + expected := ` +...F-.P-.UU.....F..P..U 23 + + +--- Failed steps: + + When failing # any.feature:11 + Error: errored + + When failing # any.feature:24 + Error: errored + + +8 scenarios (2 passed, 2 failed, 2 pending, 2 undefined) +23 steps (14 passed, 2 failed, 2 pending, 3 undefined, 2 skipped) +%s + +Randomized with seed: %s + +You can implement step definitions for undefined steps with these snippets: + +func undefined() error { + return godog.ErrPending +} + +func nextUndefined() error { + return godog.ErrPending +} + +func FeatureContext(s *godog.Suite) { + s.Step(` + "`^undefined$`" + `, undefined) + s.Step(` + "`^next undefined$`" + `, nextUndefined) +}` + + var zeroDuration time.Duration + expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED")) + expected = trimAllLines(expected) + + s.run() + s.fmt.Summary() + + actual := trimAllLines(buf.String()) + if actual != expected { + t.Fatalf("expected output does not match: %s", actual) + } +} + +func trimAllLines(s string) string { + var lines []string + for _, ln := range strings.Split(strings.TrimSpace(s), "\n") { + lines = append(lines, strings.TrimSpace(ln)) + } + return strings.Join(lines, "\n") +}