Skip to content

Commit

Permalink
match each will now fail if empty array #2364
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Aug 9, 2023
1 parent c94b9e5 commit 6a3f4aa
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ boolean execute() {
case EACH_CONTAINS_DEEP:
if (actual.isList()) {
List list = actual.getValue();
Match.Type nestedMatchType = fromMatchEach();
if (list.isEmpty()) {
return fail("match each failed, empty array / list");
}
Match.Type nestedMatchType = fromMatchEach();
int count = list.size();
for (int i = 0; i < count; i++) {
Object o = list.get(i);
Expand Down
6 changes: 6 additions & 0 deletions karate-core/src/test/java/com/intuit/karate/MatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ void testEach() {
match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "#object");
match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "{ a: '#number' }");
}

@Test
void testEachEmpty() {
match("[]", EACH_EQUALS, "#number", FAILS);
message("match each failed, empty array / list");
}

@Test
void testEachWithMagicVariables() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ void testMatchSchemaArray() {
run(
"def temp = { foo: '#string' }",
"def schema = '#[] temp'",
"match [] == schema",
"match [{ foo: 'bar' }] == schema"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ Scenario: complex nested arrays
{
"foo": {
"bars": [
{ "barOne": "dc", "barTwos": [] },
{ "barOne": "dc", "barTwos": [{ title: 'blah' }] },
{ "barOne": "dc", "barTwos": [{ title: 'blah' }], barThrees: [] },
{ "barOne": "dc", "barTwos": [{ title: 'blah' }], barThrees: [{ num: 1 }] }
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ Feature:
Scenario:
* def schema = "#[] read('schema-read.json')"
* print schema
* match [] == schema
* match [{ foo: 'bar', items: [] }] == schema
* match [{ foo: 'bar', items: [{ a: 1 }] }] == schema

0 comments on commit 6a3f4aa

Please sign in to comment.