Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

fix: incorrect assertion func and json number parse rule #83

Merged
merged 12 commits into from Feb 15, 2022
14 changes: 7 additions & 7 deletions docs/BUILTIN.md
Expand Up @@ -16,13 +16,13 @@ HttpRunner+ validation should follow the following format. `check`, `assert` and
The `assert` method name will be mapped to a built-in function with the following function signature.

```go
func(t assert.TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool
func(t assert.TestingT, actual interface{}, expected interface{}, msgAndArgs ...interface{}) bool
```

Currently, HttpRunner+ has the following built-in assertion functions.

| `assert` | Description | A(check), B(expect) | examples |
| -- | -- | -- | -- |
| --- | --- | --- | --- |
| `eq`, `equals`, `equal` | value is equal | A == B | 9 eq 9 |
| `lt`, `less_than` | less than | A < B | 7 lt 8 |
| `le`, `less_or_equals` | less than or equals | A <= B | 7 le 8, 8 le 8 |
Expand All @@ -31,14 +31,14 @@ Currently, HttpRunner+ has the following built-in assertion functions.
| `ne`, `not_equal` | not equals | A != B | 6 ne 9 |
| `str_eq`, `string_equals` | string equals | str(A) == str(B) | 123 str_eq '123' |
| `len_eq`, `length_equals`, `length_equal` | length equals | len(A) == B | 'abc' len_eq 3, [1,2] len_eq 2 |
| `len_gt`, `count_gt` | length greater than | len(A) > B | 'abc' len_gt 2, [1,2,3] len_gt 2 |
| `len_ge`, `count_ge` | length greater than or equals | len(A) >= B | 'abc' len_ge 3, [1,2,3] len_gt 3 |
| `len_lt`, `count_lt` | length less than | len(A) < B | 'abc' len_lt 4, [1,2,3] len_lt 4 |
| `len_le`, `count_le` | length less than or equals | len(A) <= B | 'abc' len_le 3, [1,2,3] len_le 3 |
| `len_gt`, `count_gt`, `length_greater_than` | length greater than | len(A) > B | 'abc' len_gt 2, [1,2,3] len_gt 2 |
| `len_ge`, `count_ge`, `length_greater_or_equals` | length greater than or equals | len(A) >= B | 'abc' len_ge 3, [1,2,3] len_gt 3 |
| `len_lt`, `count_lt`, `length_less_than` | length less than | len(A) < B | 'abc' len_lt 4, [1,2,3] len_lt 4 |
| `len_le`, `count_le`, `length_less_or_equals` | length less than or equals | len(A) <= B | 'abc' len_le 3, [1,2,3] len_le 3 |
| `contains` | contains | [1, 2] contains 1 | 'abc' contains 'a', [1,2,3] len_lt 4 |
| `contained_by` | contained by | A in B | 'a' contained_by 'abc', 1 contained_by [1,2] |
| `type_match` | A and B are in the same type | type(A) == type(B) | 123 type_match 1 |
| `regex_match` | regex matches | re.match(B, A) | 'abcdef' regex 'a\w+d' |
| `regex_match` | regex matches | re.match(B, A) | 'abcdef' regex_match 'a\w+d' |
| `startswith` | starts with | A.startswith(B) is True | 'abc' startswith 'ab' |
| `endswith` | ends with | A.endswith(B) is True | 'abc' endswith 'bc' |

Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
@@ -1,5 +1,9 @@
# Release History

## v0.6.1 (2022-02-11)

- fix: assertion function errors and json number parse rule

## v0.6.0 (2022-02-08)

- feat: implement `rendezvous` mechanism for data driven
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/hrp.md
Expand Up @@ -33,4 +33,4 @@ Copyright 2021 debugtalk
* [hrp run](hrp_run.md) - run API test
* [hrp startproject](hrp_startproject.md) - create a scaffold project

###### Auto generated by spf13/cobra on 8-Feb-2022
###### Auto generated by spf13/cobra on 15-Feb-2022
2 changes: 1 addition & 1 deletion docs/cmd/hrp_boom.md
Expand Up @@ -39,4 +39,4 @@ hrp boom [flags]

* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.

###### Auto generated by spf13/cobra on 8-Feb-2022
###### Auto generated by spf13/cobra on 15-Feb-2022
2 changes: 1 addition & 1 deletion docs/cmd/hrp_har2case.md
Expand Up @@ -23,4 +23,4 @@ hrp har2case $har_path... [flags]

* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.

###### Auto generated by spf13/cobra on 8-Feb-2022
###### Auto generated by spf13/cobra on 15-Feb-2022
2 changes: 1 addition & 1 deletion docs/cmd/hrp_run.md
Expand Up @@ -33,4 +33,4 @@ hrp run $path... [flags]

* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.

###### Auto generated by spf13/cobra on 8-Feb-2022
###### Auto generated by spf13/cobra on 15-Feb-2022
2 changes: 1 addition & 1 deletion docs/cmd/hrp_startproject.md
Expand Up @@ -16,4 +16,4 @@ hrp startproject $project_name [flags]

* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.

###### Auto generated by spf13/cobra on 8-Feb-2022
###### Auto generated by spf13/cobra on 15-Feb-2022
1 change: 0 additions & 1 deletion examples/extract_test.go
Expand Up @@ -62,7 +62,6 @@ func TestCaseExtractStepAssociation(t *testing.T) {
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("$statusCode", 200, "check status code").
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("$varFoo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
Expand Down
1 change: 0 additions & 1 deletion examples/request_test.go
Expand Up @@ -20,7 +20,6 @@ func TestCaseBasicRequest(t *testing.T) {
}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2"),
Expand Down
1 change: 0 additions & 1 deletion examples/validate_test.go
Expand Up @@ -25,7 +25,6 @@ func TestCaseValidateStep(t *testing.T) {
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code"). // assert status code
AssertEqual("headers.Connection", "keep-alive", "check header Connection"). // assert response header
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). // assert response header, with double quotes
AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath
AssertEqual("body.args.foo2", "bar2", "check args foo2").
Expand Down
3 changes: 0 additions & 3 deletions examples/variables_test.go
Expand Up @@ -22,7 +22,6 @@ func TestCaseConfigVariables(t *testing.T) {
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
Expand Down Expand Up @@ -53,7 +52,6 @@ func TestCaseStepVariables(t *testing.T) {
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
Expand Down Expand Up @@ -88,7 +86,6 @@ func TestCaseOverrideConfigVariables(t *testing.T) {
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
Expand Down
96 changes: 61 additions & 35 deletions internal/builtin/assertion.go
Expand Up @@ -8,31 +8,48 @@ import (
"github.com/stretchr/testify/assert"
)

var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool{
var Assertions = map[string]func(t assert.TestingT, actual interface{}, expected interface{}, msgAndArgs ...interface{}) bool{
"eq": assert.EqualValues,
"equals": assert.EqualValues,
"equal": assert.EqualValues, // alias for equals
"greater_than": assert.Greater,
"equal": assert.EqualValues,
"lt": assert.Less,
"less_than": assert.Less,
"greater_or_equals": assert.GreaterOrEqual,
"le": assert.LessOrEqual,
"less_or_equals": assert.LessOrEqual,
"gt": assert.Greater,
"greater_than": assert.Greater,
"ge": assert.GreaterOrEqual,
"greater_or_equals": assert.GreaterOrEqual,
"ne": assert.NotEqual,
"not_equal": assert.NotEqual,
"contained_by": assert.Contains,
"regex_match": assert.Regexp,
"contains": assert.Contains,
"type_match": assert.IsType,
// custom assertions
"startswith": StartsWith, // check if string starts with substring
"endswith": EndsWith, // check if string ends with substring
"startswith": StartsWith,
"endswith": EndsWith,
"len_eq": EqualLength,
"length_equals": EqualLength,
"length_equal": EqualLength, // alias for length_equals
"length_equal": EqualLength,
"len_lt": LessThanLength,
"count_lt": LessThanLength,
"length_less_than": LessThanLength,
"len_le": LessOrEqualsLength,
"count_le": LessOrEqualsLength,
"length_less_or_equals": LessOrEqualsLength,
"len_gt": GreaterThanLength,
"count_gt": GreaterThanLength,
"length_greater_than": GreaterThanLength,
"len_ge": GreaterOrEqualsLength,
"count_ge": GreaterOrEqualsLength,
"length_greater_or_equals": GreaterOrEqualsLength,
"contains": Contains,
"string_equals": EqualString,
"contained_by": ContainedBy,
"str_eq": StringEqual,
"string_equals": StringEqual,
"regex_match": RegexMatch,
}

func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
// StartsWith check if string starts with substring
func StartsWith(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, fmt.Sprintf("actual is %v", actual)) {
return false
}
Expand All @@ -44,7 +61,8 @@ func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...i
return assert.True(t, strings.HasPrefix(actualString, expectedString), msgAndArgs...)
}

func EndsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
// EndsWith check if string ends with substring
func EndsWith(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, fmt.Sprintf("actual is %v", actual)) {
return false
}
Expand All @@ -56,7 +74,7 @@ func EndsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...int
return assert.True(t, strings.HasSuffix(actualString, expectedString), msgAndArgs...)
}

func EqualLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
func EqualLength(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
Expand All @@ -65,7 +83,7 @@ func EqualLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...
return assert.Len(t, actual, length, msgAndArgs...)
}

func GreaterThanLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
func GreaterThanLength(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
Expand All @@ -80,7 +98,7 @@ func GreaterThanLength(t assert.TestingT, expected, actual interface{}, msgAndAr
return true
}

func GreaterOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
func GreaterOrEqualsLength(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
Expand All @@ -95,7 +113,7 @@ func GreaterOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgA
return true
}

func LessThanLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
func LessThanLength(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
Expand All @@ -110,7 +128,7 @@ func LessThanLength(t assert.TestingT, expected, actual interface{}, msgAndArgs
return true
}

func LessOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
func LessOrEqualsLength(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
Expand All @@ -125,6 +143,27 @@ func LessOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgAndA
return true
}

// ContainedBy assert whether actual element contains expected element
func ContainedBy(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
return assert.Contains(t, expected, actual, msgAndArgs)
}

func StringEqual(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, msgAndArgs) {
return false
}
if !assert.IsType(t, "string", expected, msgAndArgs) {
return false
}
actualString := actual.(string)
expectedString := expected.(string)
return assert.True(t, strings.EqualFold(actualString, expectedString), msgAndArgs)
}

func RegexMatch(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
return assert.Regexp(t, expected, actual, msgAndArgs)
}

func convertInt(value interface{}) (int, error) {
switch v := value.(type) {
case int:
Expand All @@ -147,28 +186,15 @@ func convertInt(value interface{}) (int, error) {
return int(v), nil
case uint64:
return int(v), nil
case float32:
return int(v), nil
case float64:
return int(v), nil
default:
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
}
}

// Contains assert whether actual element contains expected element
func Contains(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
return assert.Contains(t, actual, expected, msgAndArgs)
}

func EqualString(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, msgAndArgs) {
return false
}
if !assert.IsType(t, "string", expected, msgAndArgs) {
return false
}
actualString := actual.(string)
expectedString := expected.(string)
return assert.True(t, strings.EqualFold(actualString, expectedString), msgAndArgs)
}

// getLen try to get length of object.
// return (false, 0) if impossible.
func getLen(x interface{}) (ok bool, length int) {
Expand Down