Skip to content

Commit

Permalink
use chatgpt to regen the top of the code based on the new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnlon committed Oct 10, 2024
1 parent cb3b226 commit cf3c71a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
72 changes: 33 additions & 39 deletions test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (ctx ScenarioContext) Then(expr, stepFunc interface{}) {
func (ctx ScenarioContext) stepWithKeyword(expr interface{}, stepFunc interface{}, keyword formatters.Keyword) {
var regex *regexp.Regexp

// Validate the first input param is regex compatible
switch t := expr.(type) {
case *regexp.Regexp:
regex = t
Expand All @@ -289,17 +290,41 @@ func (ctx ScenarioContext) stepWithKeyword(expr interface{}, stepFunc interface{
case []byte:
regex = regexp.MustCompile(string(t))
default:
panic(fmt.Sprintf("expecting expr to be a *regexp.Regexp or a string, got type: %T", expr))
panic(fmt.Sprintf("expecting expr to be a *regexp.Regexp or a string or []byte, got type: %T", expr))
}

v := reflect.ValueOf(stepFunc)
fnTyp := v.Type()
if fnTyp.Kind() != reflect.Func {
// Validate that the handler is a function.
handlerType := reflect.TypeOf(stepFunc)
if handlerType.Kind() != reflect.Func {
panic(fmt.Sprintf("expected handler to be func, but got: %T", stepFunc))
}

if fnTyp.NumOut() > 2 {
panic(fmt.Sprintf("expected handler to return either zero, one or two values, but it has: %d", fnTyp.NumOut()))
// Validate the function's return types.
helpPrefix := "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error)"
isNested := false

numOut := handlerType.NumOut()
switch numOut {
case 0:
// No return values.
case 1:
// One return value: should be error, Steps, or context.Context.
outType := handlerType.Out(0)
if outType == reflect.TypeOf(Steps{}) {
isNested = true
} else {
if outType != errorInterface && outType != contextInterface {
panic(fmt.Sprintf("%s, but got: %v", helpPrefix, outType))
}
}
case 2:
// Two return values: should be (context.Context, error).
if handlerType.Out(0) != contextInterface || handlerType.Out(1) != errorInterface {
panic(fmt.Sprintf("%s, but got: %v, %v", helpPrefix, handlerType.Out(0), handlerType.Out(1)))
}
default:
// More than two return values.
panic(fmt.Sprintf("expected handler to return either zero, one or two values, but it has: %d", numOut))
}

def := &models.StepDefinition{
Expand All @@ -308,39 +333,8 @@ func (ctx ScenarioContext) stepWithKeyword(expr interface{}, stepFunc interface{
Expr: regex,
Keyword: keyword,
},
HandlerValue: v,
}

// verify valid return types
helpPrefix := "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error)"

if fnTyp.NumOut() == 1 {
typ0 := fnTyp.Out(0)

if typ0 == reflect.TypeOf(Steps{}) {
// a return value of Steps is ok
def.Nested = true
} else {
switch typ0.Kind() {
case reflect.Interface:
// error and context are ok
if !typ0.Implements(errorInterface) && !typ0.Implements(contextInterface) {
panic(fmt.Sprintf("%s, but got: %s", helpPrefix, typ0.Kind()))
}
case reflect.Slice:
panic(fmt.Sprintf("%s, but got: []%s", helpPrefix, typ0.Elem().Kind()))
default:
panic(fmt.Sprintf("%s, but got: %s", helpPrefix, typ0.Kind()))
}
}
}

if fnTyp.NumOut() == 2 {
typ0 := fnTyp.Out(0)
typ1 := fnTyp.Out(1)
if !typ0.Implements(contextInterface) || !typ1.Implements(errorInterface) {
panic(fmt.Sprintf("%s, but got (%v, %s)", helpPrefix, typ0.Name(), typ1.Name()))
}
HandlerValue: reflect.ValueOf(stepFunc),
Nested: isNested,
}

// stash the step
Expand Down
8 changes: 4 additions & 4 deletions test_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestScenarioContext_Step(t *testing.T) {

for _, c := range []tc{
{n: "ScenarioContext should panic if step expression is neither a string, regex or byte slice",
p: "expecting expr to be a *regexp.Regexp or a string, got type: int",
p: "expecting expr to be a *regexp.Regexp or a string or []byte, got type: int",
f: func() { ctx.Step(1251, okVoidResult) }},
{n: "ScenarioContext should panic if step handler is not a function",
p: "expected handler to be func, but got: int",
Expand All @@ -53,20 +53,20 @@ func TestScenarioContext_Step(t *testing.T) {
f: func() { ctx.Step(".*", nokLimitCase5) }},

{n: "ScenarioContext should panic if step expression is neither a string, regex or byte slice",
p: "expecting expr to be a *regexp.Regexp or a string, got type: int",
p: "expecting expr to be a *regexp.Regexp or a string or []byte, got type: int",
f: func() { ctx.Step(1251, okVoidResult) }},

{n: "ScenarioContext should panic if step return type is []string",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: []string",
f: func() { ctx.Step(".*", nokSliceStringResult) }},
{n: "ScenarioContext should panic if step handler return type is not an error or string slice or void (interface)",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: interface",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: interface {}",
f: func() { ctx.Step(".*", nokInvalidReturnInterfaceType) }},
{n: "ScenarioContext should panic if step handler return type is not an error or string slice or void (slice)",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: []int",
f: func() { ctx.Step(".*", nokInvalidReturnSliceType) }},
{n: "ScenarioContext should panic if step handler return type is not an error or string slice or void (other)",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: chan",
p: "expected handler to return one of error or context.Context or godog.Steps or (context.Context, error), but got: chan int",
f: func() { ctx.Step(".*", nokInvalidReturnOtherType) }},
} {
t.Run(c.n, func(t *testing.T) {
Expand Down

0 comments on commit cf3c71a

Please sign in to comment.