Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShouldJSONContain and ShouldJSONContainWithKey and negations (#746) #747

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ The value `this-value-is-secret` will not be printed in your console, `venom.log
* ShouldNotBeBetweenOrEqual - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldNotBeBetweenOrEqual.yml)
* ShouldContain - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldContain.yml)
* ShouldNotContain - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldNotContain.yml)
* ShouldJSONContain - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldJSONContain.yml)
* ShouldNotJSONContain - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldNotJSONContain.yml)
* ShouldJSONContainWithKey - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldJSONContainWithKey.yml)
* ShouldNotJSONContainWithKey - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldNotJSONContainWithKey.yml)
* ShouldContainKey - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldContainKey.yml)
* ShouldNotContainKey - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldNotContainKey.yml)
* ShouldBeIn - [example](https://github.com/ovh/venom/tree/master/tests/assertions/ShouldBeIn.yml)
Expand Down
106 changes: 106 additions & 0 deletions assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ var assertMap = map[string]AssertFunc{
"ShouldNotBeBetweenOrEqual": ShouldNotBeBetweenOrEqual,
"ShouldContain": ShouldContain,
"ShouldNotContain": ShouldNotContain,
"ShouldJSONContain": ShouldJSONContain,
"ShouldNotJSONContain": ShouldNotJSONContain,
"ShouldJSONContainWithKey": ShouldJSONContainWithKey,
"ShouldNotJSONContainWithKey": ShouldNotJSONContainWithKey,
"ShouldContainKey": ShouldContainKey,
"ShouldNotContainKey": ShouldNotContainKey,
"ShouldBeIn": ShouldBeIn,
Expand Down Expand Up @@ -608,6 +612,108 @@ func ShouldNotContain(actual interface{}, expected ...interface{}) error {
return nil
}

// ShouldJSONContain receives exactly two parameters. The first is a slice, the
// second is a proposed JSON member.
// Equality is determined using ShouldJSONEqual.
func ShouldJSONContain(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
}
actualSlice, err := cast.ToSliceE(actual)
if err != nil {
return err
}
for i := range actualSlice {
if ShouldJSONEqual(actualSlice[i], expected[0]) == nil {
return nil
}
}
return fmt.Errorf("expected '%v' to contain %v but it wasnt", actual, expected[0])
}

// ShouldNotJSONContain receives exactly two parameters. The first is a slice, the
// second is a proposed JSON member.
// Equality is determined using ShouldJSONEqual.
func ShouldNotJSONContain(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
}
actualSlice, err := cast.ToSliceE(actual)
if err != nil {
return err
}
for i := range actualSlice {
if ShouldJSONEqual(actualSlice[i], expected[0]) == nil {
return fmt.Errorf("expected '%v' not contain %v but it was", actual, expected[0])
}
}
return nil
}

// ShouldJSONContainWithKey receives exactly three parameters. The first is a slice, the
// second is a key in the inner slice structure and the third is a proposed value associated to the key.
// Equality is determined using ShouldJSONEqual.
func ShouldJSONContainWithKey(actual interface{}, expected ...interface{}) error {
if err := need(2, expected); err != nil {
return err
}
actualSlice, err := cast.ToSliceE(actual)
if err != nil {
return err
}
if reflect.TypeOf(expected[0]).Kind() != reflect.String {
return fmt.Errorf("expected '%v' to be a string", expected[0])
}
expectedKey := cast.ToString(expected[0])
for i := range actualSlice {
if reflect.TypeOf(actualSlice[i]).Kind() != reflect.Map && reflect.TypeOf(actualSlice[i]).Kind() != reflect.Struct {
return fmt.Errorf("expected '%v' to be a map or a struct", actualSlice[i])
}
elem := cast.ToStringMap(actualSlice[i])
if err != nil {
return err
}
if _, ok := elem[expectedKey]; ok {
if ShouldJSONEqual(elem[expectedKey], expected[1]) == nil {
return nil
}
}
}
return fmt.Errorf("expected '%v' contain a key '%s' with value %v but it wasnt", actual, expectedKey, expected[1])
}

// ShouldNotJSONContainWithKey receives exactly three parameters. The first is a slice, the
// second is a key in the inner slice structure and the third is a proposed value associated to the key.
// Equality is determined using ShouldJSONEqual.
func ShouldNotJSONContainWithKey(actual interface{}, expected ...interface{}) error {
if err := need(2, expected); err != nil {
return err
}
actualSlice, err := cast.ToSliceE(actual)
if err != nil {
return err
}
if reflect.TypeOf(expected[0]).Kind() != reflect.String {
return fmt.Errorf("expected '%v' to be a string", expected[0])
}
expectedKey := cast.ToString(expected[0])
for i := range actualSlice {
if reflect.TypeOf(actualSlice[i]).Kind() != reflect.Map && reflect.TypeOf(actualSlice[i]).Kind() != reflect.Struct {
return fmt.Errorf("expected '%v' to be a map or a struct currently %v", actualSlice[i], reflect.TypeOf(actualSlice[i]).Kind())
}
elem := cast.ToStringMap(actualSlice[i])
if err != nil {
return err
}
if _, ok := elem[expectedKey]; ok {
if ShouldJSONEqual(elem[expectedKey], expected[1]) == nil {
return fmt.Errorf("expected '%v' not contain a key '%s' with value %v but it was", actual, expectedKey, expected[1])
}
}
}
return nil
}

// ShouldContainKey receives exactly two parameters. The first is a map and the
// second is a proposed key.
func ShouldContainKey(actual interface{}, expected ...interface{}) error {
Expand Down
Loading