From f292e412c6317ba4d6af8a7b2a3a098c1b17f564 Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 12 Jun 2015 17:35:00 +0300 Subject: [PATCH] move step handler type to suite --- builder.go | 37 ++++++++++++++++---------------- gherkin/example/ls_test.go | 16 ++++++++++++++ steps.go | 44 -------------------------------------- steps_test.go | 14 ------------ suite.go | 36 +++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 76 deletions(-) create mode 100644 gherkin/example/ls_test.go delete mode 100644 steps.go delete mode 100644 steps_test.go diff --git a/builder.go b/builder.go index c956f65..bc008fc 100644 --- a/builder.go +++ b/builder.go @@ -14,7 +14,18 @@ import ( "golang.org/x/tools/imports" ) -var mainTpl = `package main +type builder struct { + files map[string]*ast.File + fset *token.FileSet + Contexts []string + tpl *template.Template +} + +func newBuilder() *builder { + return &builder{ + files: make(map[string]*ast.File), + fset: token.NewFileSet(), + tpl: template.Must(template.New("main").Parse(`package main import ( "github.com/DATA-DOG/godog" @@ -26,19 +37,7 @@ func main() { {{$c}}(suite) {{end}} suite.Run() -} -` - -type builder struct { - files map[string]*ast.File - fset *token.FileSet - Contexts []string -} - -func newBuilder() *builder { - return &builder{ - files: make(map[string]*ast.File), - fset: token.NewFileSet(), +}`)), } } @@ -104,8 +103,7 @@ func (b *builder) registerSteps(f *ast.File) { func (b *builder) merge() (*ast.File, error) { var buf bytes.Buffer - t := template.Must(template.New("main").Parse(mainTpl)) - if err := t.Execute(&buf, b); err != nil { + if err := b.tpl.Execute(&buf, b); err != nil { return nil, err } @@ -122,8 +120,11 @@ func (b *builder) merge() (*ast.File, error) { return ast.MergePackageFiles(pkg, ast.FilterImportDuplicates), nil } -// Build creates a runnable godog executable -// from current package go files +// Build creates a runnable godog executable file +// from current package source and test files +// it merges the files with the help of go/ast into +// a single main package file which has a custom +// main function to run features func Build() ([]byte, error) { b := newBuilder() err := filepath.Walk(".", func(path string, file os.FileInfo, err error) error { diff --git a/gherkin/example/ls_test.go b/gherkin/example/ls_test.go new file mode 100644 index 0000000..68ba06d --- /dev/null +++ b/gherkin/example/ls_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "log" + "regexp" + + "github.com/DATA-DOG/godog" +) + +func SomeContext(g godog.Suite) { + f := godog.StepHandlerFunc(func(args ...interface{}) error { + log.Println("step triggered") + return nil + }) + g.Step(regexp.MustCompile("hello"), f) +} diff --git a/steps.go b/steps.go deleted file mode 100644 index 2b8c988..0000000 --- a/steps.go +++ /dev/null @@ -1,44 +0,0 @@ -package godog - -import "regexp" - -var stepHandlers map[*regexp.Regexp]StepHandler - -// Objects implementing the StepHandler interface can be -// registered as step definitions in godog -// -// HandleStep method receives all arguments which -// will be matched according to the regular expression -// which is passed with a step registration. -// The error in return - represents a reason of failure. -// -// Returning signals that the step has finished -// and that the feature runner can move on to the next -// step. -type StepHandler interface { - HandleStep(args ...interface{}) 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(...interface{}) error - -// HandleStep calls f(step_arguments...). -func (f StepHandlerFunc) HandleStep(args ...interface{}) error { - return f(args...) -} - -// Step registers a StepHandler which will be triggered -// if regular expression will match a step from a feature file. -// -// If none of the StepHandlers are matched, then a pending -// step error will be raised. -func Step(exp *regexp.Regexp, h StepHandler) { - stepHandlers[exp] = h -} - -func init() { - stepHandlers = make(map[*regexp.Regexp]StepHandler) -} diff --git a/steps_test.go b/steps_test.go deleted file mode 100644 index d0112a2..0000000 --- a/steps_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package godog - -import ( - "log" - "regexp" -) - -func SomeContext(g Suite) { - f := StepHandlerFunc(func(args ...interface{}) error { - log.Println("step triggered") - return nil - }) - g.Step(regexp.MustCompile("hello"), f) -} diff --git a/suite.go b/suite.go index 0790671..4017d57 100644 --- a/suite.go +++ b/suite.go @@ -8,6 +8,32 @@ import ( "github.com/DATA-DOG/godog/gherkin" ) +// Objects implementing the StepHandler interface can be +// registered as step definitions in godog +// +// HandleStep method receives all arguments which +// will be matched according to the regular expression +// which is passed with a step registration. +// The error in return - represents a reason of failure. +// +// Returning signals that the step has finished +// and that the feature runner can move on to the next +// step. +type StepHandler interface { + HandleStep(args ...interface{}) 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(...interface{}) error + +// HandleStep calls f(step_arguments...). +func (f StepHandlerFunc) HandleStep(args ...interface{}) error { + return f(args...) +} + // Suite is an interface which allows various contexts // to register step definitions and event handlers type Suite interface { @@ -31,6 +57,16 @@ func New() *suite { } } +// Step allows to register a StepHandler in Godog +// feature suite, the handler will be applied to all +// steps matching the given regexp +// +// Note that if there are two handlers which may match +// the same step, then the only first matched handler +// will be applied +// +// If none of the StepHandlers are matched, then a pending +// step error will be raised. func (s *suite) Step(exp *regexp.Regexp, h StepHandler) { s.steps[exp] = h }