Skip to content

Commit

Permalink
Merge pull request #67 from wklken/ft_matches_support_array
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken authored Nov 12, 2023
2 parents f8db032 + 2415639 commit 24a4b0d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
14 changes: 14 additions & 0 deletions docs/Usage/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ assert:
- world
```

note: the `*matches` support a single regex or a list of regexs, if the value is a list, it's `AND` relationship.

```yaml
assert:
# body should both matches .ello and .orld
body_matches:
- .ello
- .orld
# body should not matches .ello and .orld
body_not_matches:
- .ello
- .orld
```

## assert header

```yaml
Expand Down
35 changes: 35 additions & 0 deletions pkg/assert/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assert

import (
"fmt"
"reflect"
"regexp"
)

Expand All @@ -25,3 +26,37 @@ func NotMatches(text, expr interface{}) (bool, string) {

return false, fmt.Sprintf("not_matches | `%v` should not match `%v`", prettyLine(text), expr)
}

func StringMatchesAll(text, exprs interface{}) (bool, string) {
listValue := reflect.ValueOf(exprs)
if reflect.TypeOf(exprs).Kind() != reflect.Slice {
return false, fmt.Sprintf("matches_all| `%v` should be slice", prettyLine(exprs))
}

for i := 0; i < listValue.Len(); i++ {
expr := listValue.Index(i).Interface()
if !test(text, expr, RegexpMatch) {
// FIXME: add index in error info
return false, fmt.Sprintf("matches | `%v` should match `%v`", prettyLine(text), expr)
}
}

return true, "OK"
}

func StringNotMatchesAll(text, exprs interface{}) (bool, string) {
listValue := reflect.ValueOf(exprs)
if reflect.TypeOf(exprs).Kind() != reflect.Slice {
return false, fmt.Sprintf("not_matches_all| `%v` should be slice", prettyLine(exprs))
}

for i := 0; i < listValue.Len(); i++ {
expr := listValue.Index(i).Interface()
if test(text, expr, RegexpMatch) {
// FIXME: add index in error info
return false, fmt.Sprintf("not_matches | `%v` should not match `%v`", prettyLine(text), expr)
}
}

return true, "OK"
}
4 changes: 2 additions & 2 deletions pkg/assert/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func StringContainsAll(s, elements interface{}) (bool, string) {
func StringNotContainsAll(s, elements interface{}) (bool, string) {
listValue := reflect.ValueOf(elements)
if reflect.TypeOf(elements).Kind() != reflect.Slice {
return false, fmt.Sprintf("contains_all | `%v` should be slice", prettyLine(elements))
return false, fmt.Sprintf("not_contains_all | `%v` should be slice", prettyLine(elements))
}

for i := 0; i < listValue.Len(); i++ {
element := listValue.Index(i).Interface()
if test(s, element, strings.Contains) {
// FIXME: add index in error info
return false, fmt.Sprintf("contains | `%v` should not contains `%v`", prettyLine(s), element)
return false, fmt.Sprintf("not_contains | `%v` should not contains `%v`", prettyLine(s), element)
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/assertion/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ func DoKeysAssertion(
{
key: "assert.body_matches",
ctx: Ctx{
f: assert.Matches,
// f: assert.Matches,
f: assert.StringMatchesAll,
element1: bodyStr,
element2: c.Assert.BodyMatches,
},
},
{
key: "assert.body_not_matches",
ctx: Ctx{
f: assert.NotMatches,
// f: assert.NotMatches,
f: assert.StringNotMatchesAll,
element1: bodyStr,
element2: c.Assert.BodyNotMatches,
},
Expand Down
15 changes: 8 additions & 7 deletions pkg/config/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ type Assert struct {
BodyEndsWith string `yaml:"body_endswith" mapstructure:"body_endswith"`
BodyNotStartsWith string `yaml:"body_not_startswith" mapstructure:"body_not_startswith"`
BodyNotEndsWith string `yaml:"body_not_endswith" mapstructure:"body_not_endswith"`
BodyMatches string `yaml:"body_matches" mapstructure:"body_matches"`
BodyNotMatches string `yaml:"body_not_matches" mapstructure:"body_not_matches"`

Header map[string]interface{} `yaml:"header"`
HeaderExists []string `yaml:"header_exists" mapstructure:"header_exists"`
HeaderValueMatches map[string]string `yaml:"header_value_matches" mapstructure:"header_value_matches"`
HeaderValueContains map[string]string `yaml:"header_value_contains" mapstructure:"header_value_contains"`
BodyMatches []string `yaml:"body_matches" mapstructure:"body_matches"`
BodyNotMatches []string `yaml:"body_not_matches" mapstructure:"body_not_matches"`

Header map[string]interface{} `yaml:"header"`
HeaderExists []string `yaml:"header_exists" mapstructure:"header_exists"`
// FIXME: support []string for matches
HeaderValueMatches map[string]string `yaml:"header_value_matches" mapstructure:"header_value_matches"`
HeaderValueContains map[string]string `yaml:"header_value_contains" mapstructure:"header_value_contains"`

JSON []AssertJSON `yaml:"json" mapstructure:"json"`
XML []AssertXML `yaml:"xml" mapstructure:"xml"`
Expand Down

0 comments on commit 24a4b0d

Please sign in to comment.