Documentation ¶
Overview ¶
Mesa is a package for creating and running table driven tests
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustAssert ¶ added in v0.8.0
MustAssert asserts the type of the given value and fails the test if the input cannot be asserted to the type.
Types ¶
type Ctx ¶
type Ctx struct { context.Context As *assert.Assertions Re *require.Assertions // contains filtered or unexported fields }
Ctx represents the test context containing the testing.T instance and assertion objects for convenience.
func (*Ctx) B ¶ added in v0.7.0
B returns the underlying testing.B instance if it is being used for benchmarking. The benchmark will fail if the Ctx is being used for tests.
func (*Ctx) ReportMetric ¶ added in v0.10.0
ReportMetric adds the benchmarking metric with the given name to the context. The metrics are divided by the number of calls (b.N) once the benchmark is complete. It will panic if the context is being used for tests
type Empty ¶ added in v0.2.1
type Empty any
Empty is a type used when testing structs and functions without fields or return values
type ErrorPair ¶ added in v0.5.0
ErrorPair is a convenience type used to wrap function outputs that return a value and an error
func NewErrorPair ¶ added in v0.5.1
NewErrorPair creates a new error pair with the provided value and error
type FunctionCase ¶
type FunctionCase[InputType, OutputType any] struct { // [Required] Name of the test case. Name string // [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 // [Optional] Reason to skip the test case. The test is only skipped if this field is not empty Skip string // [Optional] Function to execute before calling the target function. It will be called instead of the BeforeCall // function in the FunctionMesa if provided. BeforeCall func(ctx *Ctx, in InputType) // [Optional] Function to check the output of the target function. It will be called instead of the Check // function in the FunctionMesa if provided. Check func(ctx *Ctx, in InputType, out OutputType) // [Optional] Cleanup function to execute after the test case finishes. It will be called instead of the Cleanup // function in the FunctionMesa if provided. Cleanup func(ctx *Ctx) }
FunctionCase represents a test case with its associated properties.
type FunctionMesa ¶
type FunctionMesa[InputType, OutputType any] struct { // [Optional] Function to initialize anything before running the test cases Init func(ctx *Ctx) // [Required] Target function under test. Target func(ctx *Ctx, in InputType) OutputType // [Required] List of test cases. Cases []FunctionCase[InputType, OutputType] // [Optional] Function to execute before calling the target function. This is called when no BeforeCall function // is provided by the the case itself. BeforeCall func(ctx *Ctx, in InputType) // [Optional] Function to check the output of the target function. This is called when no Check function // is provided by the the case itself. Check func(ctx *Ctx, in InputType, out OutputType) // [Optional] Cleanup function to execute after the test case finishes. This is called when no Cleanup function // is provided by the the case itself. Cleanup func(ctx *Ctx) // [Optional] Teardown function is called after all cases finish Teardown func(ctx *Ctx) }
FunctionMesa represents a collection of test cases that execute the target function under each test case.
Example ¶
package main import ( "testing" "github.com/a20r/mesa" ) func Add(a, b int) int { return a + b } func main() { type input struct{ a, b int } m := mesa.FunctionMesa[input, int]{ Target: func(ctx *mesa.Ctx, in input) int { return Add(in.a, in.b) }, Cases: []mesa.FunctionCase[input, int]{ { Name: "Add 1 and 2", Input: input{a: 1, b: 2}, Check: func(ctx *mesa.Ctx, in input, out int) { ctx.As.Equal(3, out) }, }, { Name: "Add 0 and 0", Input: input{a: 0, b: 0}, Check: func(ctx *mesa.Ctx, in input, out int) { ctx.As.Equal(0, out) }, }, }, } var t *testing.T m.Run(t) }
Output:
func (FunctionMesa[I, O]) Run ¶
func (m FunctionMesa[I, O]) Run(t *testing.T)
Run executes all the test cases in the FunctionMesa instance.
type MethodBenchmarkCase ¶ added in v0.6.0
type MethodBenchmarkCase[InstanceType, FieldsType, InputType, OutputType any] struct { // [Required] Name of the benchmark case. Name string // [Optional] Fields associated with the instance. FieldsFn takes priority over Fields. If fields are not needed // to instantiate a the benchmark instance, no fields need to be provided. Fields FieldsType // [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) 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, 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. Check func(ctx *Ctx, inst InstanceType, in InputType, out OutputType) // [Optional] Reason to skip the benchmark case. The benchmark is only skipped if this field is not empty Skip string // [Optional] Function to execute before calling the target function. It will be called instead of the BeforeCall // function in the MethodMesa if provided. BeforeCall func(ctx *Ctx, inst InstanceType, in InputType) // [Optional] Cleanup function to execute after the benchmark case finishes. It will be called instead of the Cleanup // function in the MethodMesa if provided. Cleanup func(ctx *Ctx, inst InstanceType) }
MethodBenchmarkCase represents a benchmark case with its associated properties.
type MethodBenchmarkMesa ¶ added in v0.6.0
type MethodBenchmarkMesa[InstanceType, FieldsType, InputType, OutputType any] struct { // [Optional] Function to initialize anything before running the test cases Init func(ctx *Ctx) // [Required] Function to create a new instance. NewInstance func(ctx *Ctx, fields FieldsType) InstanceType // [Required] Target function under test. Target func(ctx *Ctx, inst InstanceType, in InputType) OutputType // [Required] List of test cases. Cases []MethodBenchmarkCase[InstanceType, FieldsType, InputType, OutputType] // [Optional] Function to check the output of the target function. It will be called instead of the Check function // in the MethodMesa if provided. Check func(ctx *Ctx, inst InstanceType, in InputType, out OutputType) // [Optional] Function to execute before calling the target function. This is called when no BeforeCall function // is provided by the the case itself. BeforeCall func(ctx *Ctx, inst InstanceType, in InputType) // [Optional] Cleanup function to execute after the test case finishes. This is called when no Cleanup function // is provided by the the case itself. Cleanup func(ctx *Ctx, inst InstanceType) // [Optional] Teardown function is called after all cases finish Teardown func(ctx *Ctx) }
MethodBenchmarkMesa represents a collection of test cases and the functions to create instances and execute the target function under test.
func (MethodBenchmarkMesa[Inst, F, I, O]) Run ¶ added in v0.6.0
func (m MethodBenchmarkMesa[Inst, F, I, O]) Run(b *testing.B)
Run executes all the benchmark cases in the Mesa instance.
func (MethodBenchmarkMesa[Inst, F, I, O]) WithCases ¶ added in v0.9.0
func (m MethodBenchmarkMesa[Inst, F, I, O]) WithCases( cases []MethodBenchmarkCase[Inst, F, I, O], ) MethodBenchmarkMesa[Inst, F, I, O]
WithCases returns a new instance using the provided cases.
type MethodCase ¶
type MethodCase[InstanceType, FieldsType, InputType, OutputType any] struct { // [Required] Name of the test case. Name string // [Optional] Fields associated with the instance. FieldsFn takes priority over Fields. If fields are not needed // to instantiate a the test instance, no fields need to be provided. Fields FieldsType // [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) 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, inst InstanceType) InputType // [Optional] Reason to skip the test case. The test is only skipped if this field is not empty Skip string // [Optional] Function to execute before calling the target function. It will be called instead of the BeforeCall // function in the MethodMesa if provided. BeforeCall func(ctx *Ctx, inst InstanceType, in 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. Check func(ctx *Ctx, inst InstanceType, in InputType, out OutputType) // [Optional] Cleanup function to execute after the test case finishes. It will be called instead of the Cleanup // function in the MethodMesa if provided. Cleanup func(ctx *Ctx, inst InstanceType) }
MethodCase represents a test case with its associated properties.
type MethodMesa ¶
type MethodMesa[InstanceType, FieldsType, InputType, OutputType any] struct { // [Optional] Function to initialize anything before running the test cases Init func(ctx *Ctx) // [Required] Function to create a new instance. NewInstance func(ctx *Ctx, fields FieldsType) InstanceType // [Required] Target function under test. Target func(ctx *Ctx, inst InstanceType, in InputType) OutputType // [Required] List of test cases. Cases []MethodCase[InstanceType, FieldsType, InputType, OutputType] // [Optional] Function to execute before calling the target function. This is called when no BeforeCall function // is provided by the the case itself. BeforeCall func(ctx *Ctx, inst InstanceType, in InputType) // [Optional] Function to check the output of the target function. This is called when no Check function // is provided by the the case itself. Check func(ctx *Ctx, inst InstanceType, in InputType, out OutputType) // [Optional] Cleanup function to execute after the test case finishes. This is called when no Cleanup function // is provided by the the case itself. Cleanup func(ctx *Ctx, inst InstanceType) // [Optional] Teardown function is called after all cases finish Teardown func(ctx *Ctx) }
MethodMesa represents a collection of test cases and the functions to create instances and execute the target function under test.
Example ¶
package main import ( "testing" "github.com/a20r/mesa" ) type MyStruct struct { Value int } func (s *MyStruct) Add(n int) { s.Value += n } func main() { m := mesa.MethodMesa[*MyStruct, int, int, mesa.Empty]{ NewInstance: func(ctx *mesa.Ctx, value int) *MyStruct { return &MyStruct{Value: value} }, Target: func(ctx *mesa.Ctx, inst *MyStruct, n int) mesa.Empty { inst.Add(n) return nil }, Cases: []mesa.MethodCase[*MyStruct, int, int, mesa.Empty]{ { Name: "Add 1 to 0", Fields: 0, Input: 1, Check: func(ctx *mesa.Ctx, inst *MyStruct, in int, _ mesa.Empty) { ctx.As.Equal(1, inst.Value) }, }, { Name: "Add 2 to 1", Fields: 1, Input: 2, Check: func(ctx *mesa.Ctx, inst *MyStruct, in int, _ mesa.Empty) { ctx.As.Equal(3, inst.Value) }, }, }, } var t *testing.T m.Run(t) }
Output:
func (MethodMesa[Inst, F, I, O]) Run ¶
func (m MethodMesa[Inst, F, I, O]) Run(t *testing.T)
Run executes all the test cases in the Mesa instance.