mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-16 20:07:37 +08:00
refactor: hashicorp tests
This commit is contained in:
@@ -23,9 +23,28 @@ func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
|
||||
for index, argument := range args {
|
||||
if argument == nil {
|
||||
argumentsValue[index] = reflect.Zero(fn.Type().In(index))
|
||||
} else {
|
||||
argumentsValue[index] = reflect.ValueOf(args[index])
|
||||
continue
|
||||
}
|
||||
|
||||
argumentValue := reflect.ValueOf(argument)
|
||||
expectArgumentType := fn.Type().In(index)
|
||||
actualArgumentType := reflect.TypeOf(argument)
|
||||
|
||||
// type match
|
||||
if expectArgumentType == actualArgumentType {
|
||||
argumentsValue[index] = argumentValue
|
||||
continue
|
||||
}
|
||||
|
||||
// type not match, check if convertible
|
||||
if !actualArgumentType.ConvertibleTo(expectArgumentType) {
|
||||
// function argument type not match and not convertible
|
||||
err := fmt.Errorf("function argument %d's type is neither match nor convertible, expect %v, actual %v",
|
||||
index, expectArgumentType, actualArgumentType)
|
||||
return nil, err
|
||||
}
|
||||
// convert argument to expect type
|
||||
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
|
||||
}
|
||||
|
||||
resultValues := fn.Call(argumentsValue)
|
||||
@@ -40,8 +59,14 @@ func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
|
||||
return resultValues[0].Interface(), nil
|
||||
}
|
||||
} else if len(resultValues) == 1 {
|
||||
// return one arguments: interface{}
|
||||
return resultValues[0].Interface(), nil
|
||||
// return one argument
|
||||
if err, ok := resultValues[0].Interface().(error); ok {
|
||||
// return error
|
||||
return nil, err
|
||||
} else {
|
||||
// return interface{}
|
||||
return resultValues[0].Interface(), nil
|
||||
}
|
||||
} else {
|
||||
// return more than 2 arguments, unexpected
|
||||
err := fmt.Errorf("function should return at most 2 arguments")
|
||||
62
plugin/shared/call_test.go
Normal file
62
plugin/shared/call_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCallFunc(t *testing.T) {
|
||||
type data struct {
|
||||
f interface{}
|
||||
args []interface{}
|
||||
expVal interface{}
|
||||
expErr error
|
||||
}
|
||||
params := []data{
|
||||
// zero argument, zero return
|
||||
{f: func() {}, args: []interface{}{}, expVal: nil, expErr: nil},
|
||||
// zero argument, return one value
|
||||
{f: func() int { return 1 }, args: []interface{}{}, expVal: 1, expErr: nil},
|
||||
{f: func() string { return "a" }, args: []interface{}{}, expVal: "a", expErr: nil},
|
||||
{f: func() interface{} { return 1.23 }, args: []interface{}{}, expVal: 1.23, expErr: nil},
|
||||
// zero argument, return error
|
||||
{f: func() error { return errors.New("xxx") }, args: []interface{}{}, expVal: nil, expErr: errors.New("xxx")},
|
||||
// zero argument, return one value and error
|
||||
{f: func() (int, error) { return 1, errors.New("xxx") }, args: []interface{}{}, expVal: 1, expErr: errors.New("xxx")},
|
||||
{f: func() (interface{}, error) { return 1.23, errors.New("xxx") }, args: []interface{}{}, expVal: 1.23, expErr: errors.New("xxx")},
|
||||
// one argument, return one value
|
||||
{f: func(n int) int { return n * n }, args: []interface{}{2}, expVal: 4},
|
||||
{f: func(c string) string { return c + c }, args: []interface{}{"p"}, expVal: "pp"},
|
||||
{f: func(arg interface{}) interface{} { return fmt.Sprintf("%v", arg) }, args: []interface{}{1.23}, expVal: "1.23"},
|
||||
// two arguments in same type
|
||||
{f: func(a, b int) int { return a * b }, args: []interface{}{2, 3}, expVal: 6},
|
||||
// two arguments in different type
|
||||
{
|
||||
f: func(n int, c string) string {
|
||||
var s string
|
||||
for i := 0; i < n; i++ {
|
||||
s += c
|
||||
}
|
||||
return s
|
||||
},
|
||||
args: []interface{}{3, "p"},
|
||||
expVal: "ppp",
|
||||
},
|
||||
}
|
||||
|
||||
for _, p := range params {
|
||||
fn := reflect.ValueOf(p.f)
|
||||
val, err := CallFunc(fn, p.args...)
|
||||
if !assert.Equal(t, p.expErr, err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, p.expVal, val) {
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user