change: move assertions to builtin

This commit is contained in:
debugtalk
2021-10-03 10:08:24 +08:00
parent a2cc1fae7f
commit e88deb2a63
4 changed files with 20 additions and 16 deletions

15
builtin/assertion.go Normal file
View File

@@ -0,0 +1,15 @@
package builtin
import "github.com/stretchr/testify/assert"
var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool{
"equals": assert.EqualValues,
"equal": assert.EqualValues, // alias for equals
"greater_than": assert.Greater,
"less_than": assert.Less,
"greater_or_equals": assert.GreaterOrEqual,
"less_or_equals": assert.LessOrEqual,
"not_equal": assert.NotEqual,
"contains": assert.Contains,
"regex_match": assert.Regexp,
}

View File

@@ -2,7 +2,7 @@ package builtin
import "time"
var FunctionsMap = map[string]interface{}{
var Functions = map[string]interface{}{
"sleep": Sleep,
}

View File

@@ -170,7 +170,7 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s
}
func callFunction(funcName string, params []interface{}) (interface{}, error) {
function, ok := builtin.FunctionsMap[funcName]
function, ok := builtin.Functions[funcName]
if !ok {
// function not found
return nil, fmt.Errorf("function %s is not found", funcName)

View File

@@ -8,20 +8,9 @@ import (
"github.com/imroc/req"
"github.com/jmespath/go-jmespath"
"github.com/stretchr/testify/assert"
)
var assertFunctionsMap = map[string]func(t assert.TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool{
"equals": assert.EqualValues,
"equal": assert.EqualValues, // alias for equals
"greater_than": assert.Greater,
"less_than": assert.Less,
"greater_or_equals": assert.GreaterOrEqual,
"less_or_equals": assert.LessOrEqual,
"not_equal": assert.NotEqual,
"contains": assert.Contains,
"regex_match": assert.Regexp,
}
"github.com/httprunner/httpboomer/builtin"
)
func NewResponseObject(t *testing.T, resp *req.Resp) *ResponseObject {
// prepare response headers
@@ -111,7 +100,7 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[
// get assert method
assertMethod := validator.Assert
assertFunc := assertFunctionsMap[assertMethod]
assertFunc := builtin.Assertions[assertMethod]
// parse expected value
expectValue := parseData(validator.Expect, variablesMapping)