Skip to content

Commit 6a3f4aa

Browse files
committed
match each will now fail if empty array #2364
1 parent c94b9e5 commit 6a3f4aa

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

karate-core/src/main/java/com/intuit/karate/MatchOperation.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ boolean execute() {
159159
case EACH_CONTAINS_DEEP:
160160
if (actual.isList()) {
161161
List list = actual.getValue();
162-
Match.Type nestedMatchType = fromMatchEach();
162+
if (list.isEmpty()) {
163+
return fail("match each failed, empty array / list");
164+
}
165+
Match.Type nestedMatchType = fromMatchEach();
163166
int count = list.size();
164167
for (int i = 0; i < count; i++) {
165168
Object o = list.get(i);

karate-core/src/test/java/com/intuit/karate/MatchTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ void testEach() {
233233
match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "#object");
234234
match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "{ a: '#number' }");
235235
}
236+
237+
@Test
238+
void testEachEmpty() {
239+
match("[]", EACH_EQUALS, "#number", FAILS);
240+
message("match each failed, empty array / list");
241+
}
236242

237243
@Test
238244
void testEachWithMagicVariables() {

karate-core/src/test/java/com/intuit/karate/core/ScenarioRuntimeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ void testMatchSchemaArray() {
764764
run(
765765
"def temp = { foo: '#string' }",
766766
"def schema = '#[] temp'",
767-
"match [] == schema",
768767
"match [{ foo: 'bar' }] == schema"
769768
);
770769
}

karate-core/src/test/java/com/intuit/karate/core/schema-like.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ Scenario: complex nested arrays
142142
{
143143
"foo": {
144144
"bars": [
145-
{ "barOne": "dc", "barTwos": [] },
146145
{ "barOne": "dc", "barTwos": [{ title: 'blah' }] },
147-
{ "barOne": "dc", "barTwos": [{ title: 'blah' }], barThrees: [] },
148146
{ "barOne": "dc", "barTwos": [{ title: 'blah' }], barThrees: [{ num: 1 }] }
149147
]
150148
}

karate-core/src/test/java/com/intuit/karate/core/schema-read.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ Feature:
33
Scenario:
44
* def schema = "#[] read('schema-read.json')"
55
* print schema
6-
* match [] == schema
7-
* match [{ foo: 'bar', items: [] }] == schema
86
* match [{ foo: 'bar', items: [{ a: 1 }] }] == schema

0 commit comments

Comments
 (0)