test: Add test for stepdef model

Этот коммит содержится в:
tfreville 2020-12-10 19:12:59 +01:00 коммит произвёл Viacheslav Poturaev
родитель fca39e4e89
коммит 63fd657a22
2 изменённых файлов: 169 добавлений и 12 удалений

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

@ -62,7 +62,7 @@ func (sd *StepDefinition) Run() interface{} {
if err != nil {
return fmt.Errorf(`%w %d: "%s" to int64: %s`, ErrCannotConvert, i, s, err)
}
values = append(values, reflect.ValueOf(int64(v)))
values = append(values, reflect.ValueOf(v))
case reflect.Int32:
s, err := sd.shouldBeString(i)
if err != nil {

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

@ -1,6 +1,7 @@
package models_test
import (
"errors"
"fmt"
"reflect"
"strings"
@ -8,6 +9,7 @@ import (
"github.com/cucumber/messages-go/v16"
"github.com/cucumber/godog"
"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/models"
)
@ -153,23 +155,178 @@ func TestUnexpectedArguments(t *testing.T) {
}
def.Args = []interface{}{"1"}
if err := def.Run(); err == nil {
res := def.Run()
if res == nil {
t.Fatalf("expected an error due to wrong number of arguments, but got none")
}
def.Args = []interface{}{"one", "two"}
if err := def.Run(); err == nil {
t.Fatalf("expected conversion error, but got none")
err, ok := res.(error)
if !ok {
t.Fatalf("expected an error due to wrong number of arguments, but got %T instead", res)
}
// @TODO maybe we should support duration
// fn2 := func(err time.Duration) error { return nil }
// def = &models.StepDefinition{Handler: fn2, HandlerValue: reflect.ValueOf(fn2)}
if !errors.Is(err, models.ErrUnmatchedStepArgumentNumber) {
t.Fatalf("expected an error due to wrong number of arguments, but got %v instead", err)
}
}
// def.Args = []interface{}{"1"}
// if err := def.Run(); err == nil {
// t.Fatalf("expected an error due to wrong argument type, but got none")
// }
func TestStepDefinition_Run_StepShouldBeString(t *testing.T) {
test := func(t *testing.T, fn interface{}) {
def := &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Handler: fn,
},
HandlerValue: reflect.ValueOf(fn),
}
def.Args = []interface{}{12}
res := def.Run()
if res == nil {
t.Fatalf("expected a string convertion error, but got none")
}
err, ok := res.(error)
if !ok {
t.Fatalf("expected a string convertion error, but got %T instead", res)
}
if !errors.Is(err, models.ErrCannotConvert) {
t.Fatalf("expected a string convertion error, but got '%v' instead", err)
}
}
// Ensure step type error if step argument is not a string
// for all supported types.
test(t, func(a int) error { return nil })
test(t, func(a int64) error { return nil })
test(t, func(a int32) error { return nil })
test(t, func(a int16) error { return nil })
test(t, func(a int8) error { return nil })
test(t, func(a string) error { return nil })
test(t, func(a float64) error { return nil })
test(t, func(a float32) error { return nil })
test(t, func(a *godog.Table) error { return nil })
test(t, func(a *godog.DocString) error { return nil })
test(t, func(a []byte) error { return nil })
}
func TestStepDefinition_Run_InvalidHandlerParamConversion(t *testing.T) {
test := func(t *testing.T, fn interface{}) {
def := &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Handler: fn,
},
HandlerValue: reflect.ValueOf(fn),
}
def.Args = []interface{}{12}
res := def.Run()
if res == nil {
t.Fatalf("expected an unsupported argument type error, but got none")
}
err, ok := res.(error)
if !ok {
t.Fatalf("expected an unsupported argument type error, but got %T instead", res)
}
if !errors.Is(err, models.ErrUnsupportedArgumentType) {
t.Fatalf("expected an unsupported argument type error, but got '%v' instead", err)
}
}
// Lists some unsupported argument types for step handler.
// Pointers should work only for godog.Table/godog.DocString
test(t, func(a *int) error { return nil })
test(t, func(a *int64) error { return nil })
test(t, func(a *int32) error { return nil })
test(t, func(a *int16) error { return nil })
test(t, func(a *int8) error { return nil })
test(t, func(a *string) error { return nil })
test(t, func(a *float64) error { return nil })
test(t, func(a *float32) error { return nil })
// I cannot pass structures
test(t, func(a godog.Table) error { return nil })
test(t, func(a godog.DocString) error { return nil })
test(t, func(a testStruct) error { return nil })
// I cannot use maps
test(t, func(a map[string]interface{}) error { return nil })
test(t, func(a map[string]int) error { return nil })
// Slice works only for byte
test(t, func(a []int) error { return nil })
test(t, func(a []string) error { return nil })
test(t, func(a []bool) error { return nil })
// I cannot use bool
test(t, func(a bool) error { return nil })
}
func TestStepDefinition_Run_StringConversionToFunctionType(t *testing.T) {
test := func(t *testing.T, fn interface{}, args []interface{}) {
def := &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Handler: fn,
},
HandlerValue: reflect.ValueOf(fn),
Args: args,
}
res := def.Run()
if res == nil {
t.Fatalf("expected a cannot convert argument type error, but got none")
}
err, ok := res.(error)
if !ok {
t.Fatalf("expected a cannot convert argument type error, but got %T instead", res)
}
if !errors.Is(err, models.ErrCannotConvert) {
t.Fatalf("expected a cannot convert argument type error, but got '%v' instead", err)
}
}
// Lists some unsupported argument types for step handler.
// Cannot convert invalid int
test(t, func(a int) error { return nil }, []interface{}{"a"})
test(t, func(a int64) error { return nil }, []interface{}{"a"})
test(t, func(a int32) error { return nil }, []interface{}{"a"})
test(t, func(a int16) error { return nil }, []interface{}{"a"})
test(t, func(a int8) error { return nil }, []interface{}{"a"})
// Cannot convert invalid float
test(t, func(a float32) error { return nil }, []interface{}{"a"})
test(t, func(a float64) error { return nil }, []interface{}{"a"})
// Cannot convert to DataArg
test(t, func(a *godog.Table) error { return nil }, []interface{}{"194"})
// Cannot convert to DocString ?
test(t, func(a *godog.DocString) error { return nil }, []interface{}{"194"})
}
// @TODO maybe we should support duration
// fn2 := func(err time.Duration) error { return nil }
// def = &models.StepDefinition{Handler: fn2, HandlerValue: reflect.ValueOf(fn2)}
// def.Args = []interface{}{"1"}
// if err := def.Run(); err == nil {
// t.Fatalf("expected an error due to wrong argument type, but got none")
// }
type testStruct struct {
a string
}
func TestShouldSupportDocStringToStringConversion(t *testing.T) {