-
Notifications
You must be signed in to change notification settings - Fork 2
Upgrading
Joshua Smith edited this page Oct 28, 2022
·
3 revisions
- requires running go version 1.18 or later
func(in trial.Input)(result interface{}, err error)
Convert to strong type
- Convert in into type Q, required by function
- Update the result interface to type B, result of function
- Add type definition to Cases
Cases[A,B]{}
Keep using trial.Input
- Keep in variable
- Update the result interface to type B, result of function
- Add type definition to Cases
Cases[trial.Input,B]{} - Make sure each Input in a case is using the
trial.Args()function
func(args ...interface{})(result interface{}, err error)
Single variable function
- Convert the args into the type A, required by function
- Update the result interface to type B, result of function
- Add type definition to Cases
Cases[A,B]{}
Multi-variable function
- Convert the args into a multi variable type A
- Update the result interface to type B, result of function
- Add type definition to Cases
Cases[A,B]{} - It may be required to change the
Inputfield 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)
}