Skip to content

Commit

Permalink
feat(venom): add ShouldJSONContain and ShouldJSONContainWithKey and n…
Browse files Browse the repository at this point in the history
…egations (#746) (#747)

Signed-off-by: florian.cazals <[email protected]>
  • Loading branch information
floriancazals authored Nov 23, 2023
1 parent b0b06b9 commit 5c611c9
Show file tree
Hide file tree
Showing 7 changed files with 766 additions and 0 deletions.
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

0 comments on commit 5c611c9

Please sign in to comment.