Skip to content

Commit

Permalink
input fn takes inst now
Browse files Browse the repository at this point in the history
  • Loading branch information
a20r committed Dec 4, 2023
1 parent 9ef1b49 commit f92b73f
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions mesa.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ type MethodCase[InstanceType, FieldsType, InputType, OutputType any] struct {

// [Optional] FieldsFn returns the fields used for this case. FieldsFn takes priority over Fields. If fields are
// not needed to instantiate a the test instance, no fields need to be provided.
FieldsFn func(ctx *Ctx, in InputType) FieldsType
FieldsFn func(ctx *Ctx) FieldsType

// [Optional] Input data for the test case. InputFn takes priority over Input. The Input field can be empty if the
// target function does not take any arguments.
Input InputType

// [Optional] InputFn returns the input struct used for this case. It takes priority over the Input field. This can
// be empty if the target function does not take any arguments.
InputFn func(ctx *Ctx) InputType
InputFn func(ctx *Ctx, inst InstanceType) InputType

// [Optional] Reason to skip the test case. The test is only skipped if this field is not empty
Skip string
Expand Down Expand Up @@ -189,16 +189,16 @@ func (m MethodMesa[Inst, F, I, O]) Run(t *testing.T) {

ctx := newCtx(t)

if tt.InputFn != nil {
tt.Input = tt.InputFn(ctx)
}

if tt.FieldsFn != nil {
tt.Fields = tt.FieldsFn(ctx, tt.Input)
tt.Fields = tt.FieldsFn(ctx)
}

inst := m.NewInstance(ctx, tt.Fields)

if tt.InputFn != nil {
tt.Input = tt.InputFn(ctx, inst)
}

cleanup := func() {}

switch {
Expand Down Expand Up @@ -322,10 +322,12 @@ func (m FunctionMesa[I, O]) Run(t *testing.T) {
for i, c := range m.Cases {
c := c
im.Cases[i] = MethodCase[any, any, I, O]{
Name: c.Name,
Input: c.Input,
InputFn: c.InputFn,
Skip: c.Skip,
Name: c.Name,
Input: c.Input,
Skip: c.Skip,
InputFn: func(ctx *Ctx, inst any) I {
return c.InputFn(ctx)
},
}

checkAndSet(&im.Cases[i].BeforeCall, c.BeforeCall != nil, func(ctx *Ctx, _ any, in I) {
Expand Down Expand Up @@ -386,15 +388,15 @@ type MethodBenchmarkCase[InstanceType, FieldsType, InputType, OutputType any] st

// [Optional] FieldsFn returns the fields used for this case. FieldsFn takes priority over Fields. If fields are
// not needed to instantiate a the benchmark instance, no fields need to be provided.
FieldsFn func(ctx *Ctx, in InputType) FieldsType
FieldsFn func(ctx *Ctx) FieldsType

// [Optional] Input data for the benchmark case. InputFn takes priority over Input. The Input field can be empty if the
// target function does not take any arguments.
Input InputType

// [Optional] InputFn returns the input struct used for this case. It takes priority over the Input field. This can
// be empty if the target function does not take any arguments.
InputFn func(ctx *Ctx) InputType
InputFn func(ctx *Ctx, inst InstanceType) InputType

// [Optional] Function to check the output of the target function. It will be called instead of the Check function
// in the MethodMesa if provided.
Expand Down Expand Up @@ -434,16 +436,16 @@ func (m MethodBenchmarkMesa[Inst, F, I, O]) Run(b *testing.B) {

ctx := newCtx(b)

if bb.InputFn != nil {
bb.Input = bb.InputFn(ctx)
}

if bb.FieldsFn != nil {
bb.Fields = bb.FieldsFn(ctx, bb.Input)
bb.Fields = bb.FieldsFn(ctx)
}

inst := m.NewInstance(ctx, bb.Fields)

if bb.InputFn != nil {
bb.Input = bb.InputFn(ctx, inst)
}

cleanup := func() {}

switch {
Expand Down

0 comments on commit f92b73f

Please sign in to comment.