tests printing step definitions and stacktrace formatting

Этот коммит содержится в:
gedi 2017-04-28 09:46:17 +03:00
родитель 83675453b7
коммит d5a7e38a81
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
4 изменённых файлов: 95 добавлений и 4 удалений

2
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
}

52
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)
}
}

39
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)
}
}

Просмотреть файл

@ -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..")
}
}