Skip to content

Upgrading

Joshua Smith edited this page Oct 28, 2022 · 3 revisions

Upgrading to 0.7 (generics)

  • requires running go version 1.18 or later

Converting from 0.6.x

func(in trial.Input)(result interface{}, err error)

Convert to strong type

  1. Convert in into type Q, required by function
  2. Update the result interface to type B, result of function
  3. Add type definition to Cases Cases[A,B]{}

Keep using trial.Input

  1. Keep in variable
  2. Update the result interface to type B, result of function
  3. Add type definition to Cases Cases[trial.Input,B]{}
  4. Make sure each Input in a case is using the trial.Args() function

Converting from 0.3.x

func(args ...interface{})(result interface{}, err error)

Single variable function

  1. Convert the args into the type A, required by function
  2. Update the result interface to type B, result of function
  3. Add type definition to Cases Cases[A,B]{}

Multi-variable function

  1. Convert the args into a multi variable type A
    • an input struct containing all required values struct{I int; B float64} example
    • a slice of the values. ex: []int example
    • the helper type trial.Input returned when using trial.Args
  2. Update the result interface to type B, result of function
  3. Add type definition to Cases Cases[A,B]{}
  4. It may be required to change the Input field of each to the type A
func Add(i1, i2 int) int {
	return i1 + i2
}

func TestAdd(t *testing.T) {
	testFn := func(in trial.Input) (int, error) {
		return Add(in.Slice(0).Int(), in.Slice(1).Int()), nil
	}
	cases := trial.Cases[trial.Input, int]{
		"Add two numbers": {
			Input:    trial.Args(1, 2),
			Expected: 3,
		},
	}
	trial.New(testFn, cases).Test(t)
}

func TestAddUpdated(t *testing.T) {
	testFn := func(args trial.Input) (int, error) {
		return Add(args.Slice(0).Int(), args.Slice(1).Int()), nil
	}
	cases := trial.Cases[trial.Input, int]{
		"Add two numbers": {
			Input:    trial.Args(1, 2),
			Expected: 3,
		},
	}
	trial.New(testFn, cases).Test(t)

}

Clone this wiki locally