move step handler type to suite

Этот коммит содержится в:
gedi 2015-06-12 17:35:00 +03:00
родитель 0c1d529e06
коммит f292e412c6
5 изменённых файлов: 71 добавлений и 76 удалений

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

@ -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 {

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

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

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

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

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

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

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