1package godog 2 3import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/cucumber/messages-go/v10" 9) 10 11func TestShouldSupportIntTypes(t *testing.T) { 12 fn := func(a int64, b int32, c int16, d int8) error { return nil } 13 14 def := &StepDefinition{ 15 Handler: fn, 16 hv: reflect.ValueOf(fn), 17 } 18 19 def.args = []interface{}{"1", "1", "1", "1"} 20 if err := def.run(); err != nil { 21 t.Fatalf("unexpected error: %v", err) 22 } 23 24 def.args = []interface{}{"1", "1", "1", strings.Repeat("1", 9)} 25 if err := def.run(); err == nil { 26 t.Fatalf("expected convertion fail for int8, but got none") 27 } 28} 29 30func TestShouldSupportFloatTypes(t *testing.T) { 31 fn := func(a float64, b float32) error { return nil } 32 33 def := &StepDefinition{ 34 Handler: fn, 35 hv: reflect.ValueOf(fn), 36 } 37 38 def.args = []interface{}{"1.1", "1.09"} 39 if err := def.run(); err != nil { 40 t.Fatalf("unexpected error: %v", err) 41 } 42 43 def.args = []interface{}{"1.08", strings.Repeat("1", 65) + ".67"} 44 if err := def.run(); err == nil { 45 t.Fatalf("expected convertion fail for float32, but got none") 46 } 47} 48 49func TestShouldNotSupportOtherPointerTypesThanGherkin(t *testing.T) { 50 fn1 := func(a *int) error { return nil } 51 fn2 := func(a *messages.PickleStepArgument_PickleDocString) error { return nil } 52 fn3 := func(a *messages.PickleStepArgument_PickleTable) error { return nil } 53 54 def1 := &StepDefinition{Handler: fn1, hv: reflect.ValueOf(fn1), args: []interface{}{(*int)(nil)}} 55 def2 := &StepDefinition{Handler: fn2, hv: reflect.ValueOf(fn2), args: []interface{}{&messages.PickleStepArgument_PickleDocString{}}} 56 def3 := &StepDefinition{Handler: fn3, hv: reflect.ValueOf(fn3), args: []interface{}{(*messages.PickleStepArgument_PickleTable)(nil)}} 57 58 if err := def1.run(); err == nil { 59 t.Fatalf("expected conversion error, but got none") 60 } 61 if err := def2.run(); err != nil { 62 t.Fatalf("unexpected error: %v", err) 63 } 64 if err := def3.run(); err != nil { 65 t.Fatalf("unexpected error: %v", err) 66 } 67} 68 69func TestShouldSupportOnlyByteSlice(t *testing.T) { 70 fn1 := func(a []byte) error { return nil } 71 fn2 := func(a []string) error { return nil } 72 73 def1 := &StepDefinition{Handler: fn1, hv: reflect.ValueOf(fn1), args: []interface{}{"str"}} 74 def2 := &StepDefinition{Handler: fn2, hv: reflect.ValueOf(fn2), args: []interface{}{[]string{}}} 75 76 if err := def1.run(); err != nil { 77 t.Fatalf("unexpected error: %v", err) 78 } 79 if err := def2.run(); err == nil { 80 t.Fatalf("expected conversion error, but got none") 81 } 82} 83 84func TestUnexpectedArguments(t *testing.T) { 85 fn := func(a, b int) error { return nil } 86 def := &StepDefinition{Handler: fn, hv: reflect.ValueOf(fn)} 87 88 def.args = []interface{}{"1"} 89 if err := def.run(); err == nil { 90 t.Fatalf("expected an error due to wrong number of arguments, but got none") 91 } 92 93 def.args = []interface{}{"one", "two"} 94 if err := def.run(); err == nil { 95 t.Fatalf("expected conversion error, but got none") 96 } 97 98 // @TODO maybe we should support duration 99 // fn2 := func(err time.Duration) error { return nil } 100 // def = &StepDefinition{Handler: fn2, hv: reflect.ValueOf(fn2)} 101 102 // def.args = []interface{}{"1"} 103 // if err := def.run(); err == nil { 104 // t.Fatalf("expected an error due to wrong argument type, but got none") 105 // } 106} 107