test: spec checkInput() method

This commit is contained in:
Adrien Marquès 2020-03-29 19:14:12 +02:00
parent 66985dfbd0
commit 7e42c1b6d9
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
1 changed files with 103 additions and 0 deletions

103
dynamic/spec_test.go Normal file
View File

@ -0,0 +1,103 @@
package dynamic
import (
"errors"
"reflect"
"testing"
)
func TestInputCheck(t *testing.T) {
tcases := []struct {
Input map[string]reflect.Type
Fn interface{}
Err error
}{
// no input
{
Input: map[string]reflect.Type{},
Fn: func() {},
Err: nil,
},
// func can have any arguments if not specified
{
Input: map[string]reflect.Type{},
Fn: func(int, string) {},
Err: nil,
},
// missing input struct in func
{
Input: map[string]reflect.Type{
"Test1": reflect.TypeOf(int(0)),
},
Fn: func() {},
Err: ErrMissingHandlerArgumentParam,
},
// input not a struct
{
Input: map[string]reflect.Type{
"Test1": reflect.TypeOf(int(0)),
},
Fn: func(int) {},
Err: ErrMissingParamArgument,
},
// unexported param name
{
Input: map[string]reflect.Type{
"test1": reflect.TypeOf(int(0)),
},
Fn: func(struct{}) {},
Err: ErrUnexportedName,
},
// input field missing
{
Input: map[string]reflect.Type{
"Test1": reflect.TypeOf(int(0)),
},
Fn: func(struct{}) {},
Err: ErrMissingParamFromConfig,
},
// input field invalid type
{
Input: map[string]reflect.Type{
"Test1": reflect.TypeOf(int(0)),
},
Fn: func(struct{ Test1 string }) {},
Err: ErrWrongParamTypeFromConfig,
},
// input field valid type
{
Input: map[string]reflect.Type{
"Test1": reflect.TypeOf(int(0)),
},
Fn: func(struct{ Test1 int }) {},
Err: nil,
},
}
for i, tcase := range tcases {
t.Run("case."+string(i), func(t *testing.T) {
// mock spec
s := spec{
Input: tcase.Input,
Output: nil,
}
err := s.checkInput(reflect.ValueOf(tcase.Fn))
if err == nil && tcase.Err != nil {
t.Errorf("expected an error: '%s'", tcase.Err.Error())
t.FailNow()
}
if err != nil && tcase.Err == nil {
t.Errorf("unexpected error: '%s'", err.Error())
t.FailNow()
}
if err != nil && tcase.Err != nil {
if !errors.Is(err, tcase.Err) {
t.Errorf("expected the error <%s> got <%s>", tcase.Err, err)
t.FailNow()
}
}
})
}
}