From 54e8ddb5db53e3535cd58fc3eddb79f78db303de Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 12 Jun 2015 21:10:09 +0300 Subject: [PATCH] custom type for arguments in order to have convenient cast methods --- cmd/godog/main.go | 1 + gherkin/example/ls_test.go | 26 +++++++++++++++++++------- suite.go | 21 ++++++++++++++++++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cmd/godog/main.go b/cmd/godog/main.go index 4bae592..e5be484 100644 --- a/cmd/godog/main.go +++ b/cmd/godog/main.go @@ -19,6 +19,7 @@ func main() { builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano()) defer os.Remove(builtFile) // comment out for debug + // @TODO: handle multiple init functions buf, err := godog.Build() if err != nil { panic(err) diff --git a/gherkin/example/ls_test.go b/gherkin/example/ls_test.go index 68ba06d..e46ed19 100644 --- a/gherkin/example/ls_test.go +++ b/gherkin/example/ls_test.go @@ -1,16 +1,28 @@ 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) +type lsFeature struct{} + +func (s *lsFeature) inDirectory(args ...godog.Arg) error { + return nil +} + +func (s *lsFeature) haveFile(args ...godog.Arg) error { + return nil +} + +func SuiteContext(g godog.Suite) { + f := &lsFeature{} + + g.Step( + regexp.MustCompile(`^I am in a directory "([^"]*)"$`), + godog.StepHandlerFunc(f.inDirectory)) + g.Step( + regexp.MustCompile(`^I have a file named "([^"]*)"$`), + godog.StepHandlerFunc(f.haveFile)) } diff --git a/suite.go b/suite.go index 9d57a28..d27e614 100644 --- a/suite.go +++ b/suite.go @@ -4,10 +4,25 @@ import ( "flag" "fmt" "regexp" + "strconv" "github.com/DATA-DOG/godog/gherkin" ) +// Arg is an argument for StepHandler parsed from +// the regexp submatch to handle the step +type Arg string + +// Float converts an argument to float64 value +// or panics if it does not know how to convert it +func (a Arg) Float() float64 { + v, err := strconv.ParseFloat(string(a), 64) + if err == nil { + return v + } + panic(fmt.Sprintf(`cannot convert string "%s" to float64: %s`, a, err)) +} + // Objects implementing the StepHandler interface can be // registered as step definitions in godog // @@ -20,17 +35,17 @@ import ( // and that the feature runner can move on to the next // step. type StepHandler interface { - HandleStep(args ...interface{}) 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(...interface{}) error +type StepHandlerFunc func(...Arg) error // HandleStep calls f(step_arguments...). -func (f StepHandlerFunc) HandleStep(args ...interface{}) error { +func (f StepHandlerFunc) HandleStep(args ...Arg) error { return f(args...) }