custom type for arguments in order to have convenient cast methods

Этот коммит содержится в:
gedi 2015-06-12 21:10:09 +03:00
родитель d111956758
коммит 54e8ddb5db
3 изменённых файлов: 38 добавлений и 10 удалений

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

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

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

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

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

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