Skip to content

Commit

Permalink
test: Add test for stepdef model
Browse files Browse the repository at this point in the history
  • Loading branch information
tfreville authored and vearutop committed Jul 13, 2021
1 parent fca39e4 commit 63fd657
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/models/stepdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
179 changes: 168 additions & 11 deletions internal/models/stepdef_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package models_test

import (
"errors"
"fmt"
"reflect"
"strings"
"testing"

"github.com/cucumber/messages-go/v16"

"github.com/cucumber/godog"
"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/models"
)
Expand Down Expand Up @@ -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)
}

if !errors.Is(err, models.ErrUnmatchedStepArgumentNumber) {
t.Fatalf("expected an error due to wrong number of arguments, but got %v instead", err)
}
}

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

// @TODO maybe we should support duration
// fn2 := func(err time.Duration) error { return nil }
// def = &models.StepDefinition{Handler: fn2, HandlerValue: reflect.ValueOf(fn2)}
// 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")
// }

// 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) {
Expand Down

0 comments on commit 63fd657

Please # to comment.