From d5a7e38a812ff525a3ca4d5612d71338df8d7428 Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 28 Apr 2017 09:46:17 +0300 Subject: [PATCH] tests printing step definitions and stacktrace formatting --- run.go | 2 +- run_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ stacktrace_test.go | 39 ++++++++++++++++++++++++++++++++++ suite.go | 6 +++--- 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 run_test.go create mode 100644 stacktrace_test.go diff --git a/run.go b/run.go index 9761ecb..965906b 100644 --- a/run.go +++ b/run.go @@ -90,7 +90,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt if opt.ShowStepDefinitions { s := &Suite{} contextInitializer(s) - s.printStepDefinitions() + s.printStepDefinitions(output) return 0 } diff --git a/run_test.go b/run_test.go new file mode 100644 index 0000000..06d898c --- /dev/null +++ b/run_test.go @@ -0,0 +1,52 @@ +package godog + +import ( + "bytes" + "strings" + "testing" + + "github.com/DATA-DOG/godog/colors" +) + +func okStep() error { + return nil +} + +func TestPrintsStepDefinitions(t *testing.T) { + var buf bytes.Buffer + w := colors.Uncolored(&buf) + s := &Suite{} + + steps := []string{ + "^passing step$", + `^with name "([^"])"`, + } + + for _, step := range steps { + s.Step(step, okStep) + } + s.printStepDefinitions(w) + + out := buf.String() + ref := `github.com/DATA-DOG/godog.okStep` + for i, def := range strings.Split(strings.TrimSpace(out), "\n") { + if idx := strings.Index(def, steps[i]); idx == -1 { + t.Fatalf(`step "%s" was not found in output`, steps[i]) + } + if idx := strings.Index(def, ref); idx == -1 { + t.Fatalf(`step definition reference "%s" was not found in output: "%s"`, ref, def) + } + } +} + +func TestPrintsNoStepDefinitionsIfNoneFound(t *testing.T) { + var buf bytes.Buffer + w := colors.Uncolored(&buf) + s := &Suite{} + s.printStepDefinitions(w) + + out := strings.TrimSpace(buf.String()) + if out != "there were no contexts registered, could not find any step definition.." { + t.Fatalf("expected output does not match to: %s", out) + } +} diff --git a/stacktrace_test.go b/stacktrace_test.go new file mode 100644 index 0000000..516cdfe --- /dev/null +++ b/stacktrace_test.go @@ -0,0 +1,39 @@ +package godog + +import ( + "fmt" + "strings" + "testing" +) + +func trimLineSpaces(s string) string { + var res []string + for _, ln := range strings.Split(s, "\n") { + res = append(res, strings.TrimSpace(ln)) + } + return strings.Join(res, "\n") +} + +func TestStacktrace(t *testing.T) { + err := &traceError{ + msg: "err msg", + stack: callStack(), + } + + expect := "err msg" + actual := fmt.Sprintf("%s", err) + if expect != actual { + t.Fatalf("expected formatted trace error message to be: %s, but got %s", expect, actual) + } + + expect = trimLineSpaces(`err msg +testing.tRunner + /usr/lib/go/src/testing/testing.go:657 +runtime.goexit + /usr/lib/go/src/runtime/asm_amd64.s:2197`) + + actual = trimLineSpaces(fmt.Sprintf("%+v", err)) + if expect != actual { + t.Fatalf("detaily formatted actual: %s", actual) + } +} diff --git a/suite.go b/suite.go index d3b6cbe..19a70ae 100644 --- a/suite.go +++ b/suite.go @@ -389,7 +389,7 @@ func (s *Suite) runScenario(scenario *gherkin.Scenario, b *gherkin.Background) ( return } -func (s *Suite) printStepDefinitions() { +func (s *Suite) printStepDefinitions(w io.Writer) { var longest int for _, def := range s.steps { n := utf8.RuneCountInString(def.Expr.String()) @@ -401,10 +401,10 @@ func (s *Suite) printStepDefinitions() { n := utf8.RuneCountInString(def.Expr.String()) location := def.definitionID() spaces := strings.Repeat(" ", longest-n) - fmt.Println(yellow(def.Expr.String())+spaces, black("# "+location)) + fmt.Fprintln(w, yellow(def.Expr.String())+spaces, black("# "+location)) } if len(s.steps) == 0 { - fmt.Println("there were no contexts registered, could not find any step definition..") + fmt.Fprintln(w, "there were no contexts registered, could not find any step definition..") } }