Skip to content

Commit

Permalink
Merge pull request #2190 from vincent-hennig/each_contains_deep
Browse files Browse the repository at this point in the history
Enable each contains deep functionality
  • Loading branch information
ptrthomas authored Nov 23, 2022
2 parents abeb49d + d64cdc9 commit 65520a9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ And you don't need to create additional Java classes for any of the payloads tha
| <a href="#match-contains-only-deep"><code>match contains only deep</code></a>
| <a href="#not-contains"><code>match !contains</code></a>
| <a href="#match-each"><code>match each</code></a>
| <a href="#match-each-contains-deep"><code>match each contains deep</code></a>
| <a href="#match-header"><code>match header</code></a>
| <a href="#fuzzy-matching">Fuzzy Matching</a>
| <a href="#schema-validation">Schema Validation</a>
Expand Down Expand Up @@ -3017,6 +3018,42 @@ Symbol | Evaluates To

There is a shortcut for `match each` explained in the next section that can be quite useful, especially for 'in-line' schema-like validations.

#### `match each contains deep`
`match each` can be combined with `contains deep` so that for each JSON object a “deep contains” match is performed within nested lists or objects.

This is useful for testing payloads with JSON arrays whose members have a few essential keys that you wish to validate.

```cucumber
Given def response =
"""
[
{
"a": 1,
"arr": [
{
"b": 2,
"c": 3
}
]
},
{
"a": 1,
"arr": [
{
"b": 2,
"c": 3
},
{
"b": 4,
"c": 5
}
]
}
]
"""
Then match each response contains deep { a: 1, arr: [ { b: 2 } ] }
```

## Schema Validation
Karate provides a far more simpler and more powerful way than [JSON-schema](http://json-schema.org) to validate the structure of a given payload. You can even mix domain and conditional validations and perform all assertions in a single step.

Expand Down
6 changes: 6 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/MatchStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ private static Match.Type getType(boolean each, boolean not, boolean contains, b
if (any) {
return Match.Type.EACH_CONTAINS_ANY;
}
if (deep) {
if (not) {
throw new RuntimeException("'each !contains deep' is not yet supported, use 'each contains deep' instead");
}
return Match.Type.EACH_CONTAINS_DEEP;
}
return not ? Match.Type.EACH_NOT_CONTAINS : Match.Type.EACH_CONTAINS;
}
return not ? Match.Type.EACH_NOT_EQUALS : Match.Type.EACH_EQUALS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void testMatchStep() {
test("hello world == foo", EQUALS, "hello", "world", "foo");
test("hello world contains only deep foo", CONTAINS_ONLY_DEEP, "hello", "world", "foo");
test("each hello world == foo", EACH_EQUALS, "hello", "world", "foo");
test("each hello world contains deep foo", EACH_CONTAINS_DEEP, "hello", "world", "foo");
test("{\"a\":1,\"b\":2} == '#object'", EQUALS, "({\"a\":1,\"b\":2})", null, "'#object'");
test("hello.foo(bar) != blah", NOT_EQUALS, "hello.foo(bar)", null, "blah");
test("foo count(/records//record) contains any blah", CONTAINS_ANY, "foo", "count(/records//record)", "blah");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,15 @@ void testMatchContainsOnlyDeep() {
"def response = { foo: [ 'a', 'b' ] } ",
"match response contains only deep { foo: [ 'b', 'a' ] }"
);
}
}

@Test
void testMatchEachContainsDeep() {
run(
"def response = [ { a: 1, arr: [ { b: 2, c: 3 }, { b: 4, c: 5 } ] } ]",
"match each response contains deep { a: 1, arr: [ { b: '#number', c: 3 } ] }"
);
}

@Test
void testJavaInteropStatic() {
Expand Down

0 comments on commit 65520a9

Please sign in to comment.