custom type for arguments in order to have convenient cast methods
Этот коммит содержится в:
родитель
d111956758
коммит
54e8ddb5db
3 изменённых файлов: 38 добавлений и 10 удалений
|
@ -19,6 +19,7 @@ func main() {
|
||||||
builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano())
|
builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano())
|
||||||
defer os.Remove(builtFile) // comment out for debug
|
defer os.Remove(builtFile) // comment out for debug
|
||||||
|
|
||||||
|
// @TODO: handle multiple init functions
|
||||||
buf, err := godog.Build()
|
buf, err := godog.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -1,16 +1,28 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog"
|
"github.com/DATA-DOG/godog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SomeContext(g godog.Suite) {
|
type lsFeature struct{}
|
||||||
f := godog.StepHandlerFunc(func(args ...interface{}) error {
|
|
||||||
log.Println("step triggered")
|
func (s *lsFeature) inDirectory(args ...godog.Arg) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
}
|
||||||
g.Step(regexp.MustCompile("hello"), f)
|
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
|
|
21
suite.go
21
suite.go
|
@ -4,10 +4,25 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog/gherkin"
|
"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
|
// Objects implementing the StepHandler interface can be
|
||||||
// registered as step definitions in godog
|
// registered as step definitions in godog
|
||||||
//
|
//
|
||||||
|
@ -20,17 +35,17 @@ import (
|
||||||
// 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 interface {
|
||||||
HandleStep(args ...interface{}) error
|
HandleStep(args ...Arg) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// StepHandlerFunc type is an adapter to allow the use of
|
// StepHandlerFunc type is an adapter to allow the use of
|
||||||
// ordinary functions as Step handlers. If f is a function
|
// ordinary functions as Step handlers. If f is a function
|
||||||
// with the appropriate signature, StepHandlerFunc(f) is a
|
// with the appropriate signature, StepHandlerFunc(f) is a
|
||||||
// StepHandler object that calls f.
|
// StepHandler object that calls f.
|
||||||
type StepHandlerFunc func(...interface{}) error
|
type StepHandlerFunc func(...Arg) error
|
||||||
|
|
||||||
// HandleStep calls f(step_arguments...).
|
// HandleStep calls f(step_arguments...).
|
||||||
func (f StepHandlerFunc) HandleStep(args ...interface{}) error {
|
func (f StepHandlerFunc) HandleStep(args ...Arg) error {
|
||||||
return f(args...)
|
return f(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче