Skip to content

Commit 41c0454

Browse files
committed
support docstring syntax for rhs of js object property mutation
1 parent 6b05a69 commit 41c0454

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public interface Actions {
7373

7474
void eval(String exp);
7575

76+
void evalAssignDocString(String lhs, String rhs);
77+
7678
void evalDocString(String exp);
7779

7880
void eval(String name, String dotOrParen, String exp);

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

+6
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ public void evalDocString(String exp) {
365365
engine.evalJs(exp);
366366
}
367367

368+
@Override
369+
@When("^([\\w]+\\.[^=]+=$)")
370+
public void evalAssignDocString(String lhs, String rhs) {
371+
engine.evalJs(lhs + rhs);
372+
}
373+
368374
@Override
369375
@When("^([\\w]+)([^\\s^\\w])(.+)")
370376
public void eval(String name, String dotOrParen, String exp) {

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@
3131
import com.intuit.karate.StringUtils;
3232
import java.lang.reflect.InvocationTargetException;
3333
import java.lang.reflect.Method;
34-
import java.util.ArrayList;
35-
import java.util.Arrays;
36-
import java.util.Collection;
37-
import java.util.HashMap;
38-
import java.util.HashSet;
39-
import java.util.List;
40-
import java.util.Map;
41-
import java.util.Objects;
42-
import java.util.StringJoiner;
34+
import java.util.*;
4335
import java.util.regex.Matcher;
4436
import java.util.regex.Pattern;
4537
import org.slf4j.LoggerFactory;
@@ -261,8 +253,18 @@ public static Result execute(Step step, Actions actions) {
261253
KarateException e = new KarateException("no step-definition method match found for: " + text);
262254
return Result.failed(System.currentTimeMillis(), 0, e, step);
263255
} else if (matches.size() > 1) {
264-
KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);
265-
return Result.failed(System.currentTimeMillis(), 0, e, step);
256+
boolean evalAssign = false; // special case to support foo.bar = (docstring) in cucumber syntax
257+
for (MethodMatch m : matches) {
258+
if (m.getMethod().getName().equalsIgnoreCase("evalAssignDocString")) {
259+
evalAssign = true;
260+
matches = Collections.singletonList(m);
261+
break;
262+
}
263+
}
264+
if (!evalAssign) {
265+
KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);
266+
return Result.failed(System.currentTimeMillis(), 0, e, step);
267+
}
266268
}
267269
MethodMatch match = matches.get(0);
268270
Object last;

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

+5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ void testEvalAndSet() {
178178
run("eval-and-set.feature");
179179
}
180180

181+
@Test
182+
void testEvalAssign() {
183+
run("eval-assign.feature");
184+
}
185+
181186
@Test
182187
void testExtract() {
183188
run("extract.feature");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature:
2+
3+
Scenario:
4+
* def foo = { bar: 'one' }
5+
* foo.bar =
6+
"""
7+
{
8+
some: 'big',
9+
message: 'content'
10+
}
11+
"""
12+
* match foo == { bar: { some: 'big', message: 'content' } }

0 commit comments

Comments
 (0)