Этот коммит содержится в:
gedi 2015-06-22 22:42:22 +03:00
родитель ca847ac1d5
коммит 6e65757f89
2 изменённых файлов: 9 добавлений и 38 удалений

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

@ -35,4 +35,4 @@ Godog was inspired by Behat and the above description is taken from it's documen
package godog package godog
// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/ // Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
const Version = "v0.1.0-alpha" const Version = "v0.1.0"

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

@ -16,15 +16,10 @@ import (
// it can be either a string or a *regexp.Regexp // it can be either a string or a *regexp.Regexp
type Regexp interface{} type Regexp interface{}
// Handler is an unified type for a StepHandler // StepHandler is a function contract for
// interface satisfaction. It may be a function // step handler
// or a step handler
type Handler interface{}
// Objects implementing the StepHandler interface can be
// registered as step definitions in godog
// //
// HandleStep method receives all arguments which // It receives all arguments which
// will be matched according to the regular expression // will be matched according to the regular expression
// which is passed with a step registration. // which is passed with a step registration.
// The error in return - represents a reason of failure. // The error in return - represents a reason of failure.
@ -32,20 +27,7 @@ type Handler interface{}
// Returning signals that the step has finished // Returning signals that the step has finished
// and that the feature runner can move on to the next // and that the feature runner can move on to the next
// step. // step.
type StepHandler interface { type StepHandler func(...*Arg) error
HandleStep(args ...*Arg) error
}
// StepHandlerFunc type is an adapter to allow the use of
// ordinary functions as Step handlers. If f is a function
// with the appropriate signature, StepHandlerFunc(f) is a
// StepHandler object that calls f.
type StepHandlerFunc func(...*Arg) error
// HandleStep calls f(step_arguments...).
func (f StepHandlerFunc) HandleStep(args ...*Arg) error {
return f(args...)
}
// ErrUndefined is returned in case if step definition was not found // ErrUndefined is returned in case if step definition was not found
var ErrUndefined = fmt.Errorf("step is undefined") var ErrUndefined = fmt.Errorf("step is undefined")
@ -63,7 +45,7 @@ type StepDef struct {
// Suite is an interface which allows various contexts // Suite is an interface which allows various contexts
// to register step definitions and event handlers // to register step definitions and event handlers
type Suite interface { type Suite interface {
Step(expr Regexp, h Handler) Step(expr Regexp, h StepHandler)
// suite events // suite events
BeforeSuite(f func()) BeforeSuite(f func())
BeforeScenario(f func(*gherkin.Scenario)) BeforeScenario(f func(*gherkin.Scenario))
@ -109,8 +91,7 @@ func New() *suite {
// //
// If none of the StepHandlers are matched, then a pending // If none of the StepHandlers are matched, then a pending
// step error will be raised. // step error will be raised.
func (s *suite) Step(expr Regexp, h Handler) { func (s *suite) Step(expr Regexp, h StepHandler) {
var handler StepHandler
var regex *regexp.Regexp var regex *regexp.Regexp
switch t := expr.(type) { switch t := expr.(type) {
@ -124,18 +105,8 @@ func (s *suite) Step(expr Regexp, h Handler) {
panic(fmt.Sprintf("expecting expr to be a *regexp.Regexp or a string, got type: %T", expr)) panic(fmt.Sprintf("expecting expr to be a *regexp.Regexp or a string, got type: %T", expr))
} }
switch t := h.(type) {
case StepHandlerFunc:
handler = t
case StepHandler:
handler = t
case func(...*Arg) error:
handler = StepHandlerFunc(t)
default:
panic(fmt.Sprintf("expecting handler to satisfy StepHandler interface, got type: %T", h))
}
s.stepHandlers = append(s.stepHandlers, &StepDef{ s.stepHandlers = append(s.stepHandlers, &StepDef{
Handler: handler, Handler: h,
Expr: regex, Expr: regex,
}) })
} }
@ -260,7 +231,7 @@ func (s *suite) runStep(step *gherkin.Step) (err error) {
} }
}() }()
if err = match.Handler.HandleStep(match.Args...); err != nil { if err = match.Handler(match.Args...); err != nil {
s.fmt.Failed(step, match, err) s.fmt.Failed(step, match, err)
} else { } else {
s.fmt.Passed(step, match) s.fmt.Passed(step, match)