Skip to content

Commit

Permalink
support docstring syntax for rhs of js object property mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Mar 3, 2025
1 parent 6b05a69 commit 41c0454
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
2 changes: 2 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public interface Actions {

void eval(String exp);

void evalAssignDocString(String lhs, String rhs);

void evalDocString(String exp);

void eval(String name, String dotOrParen, String exp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ public void evalDocString(String exp) {
engine.evalJs(exp);
}

@Override
@When("^([\\w]+\\.[^=]+=$)")
public void evalAssignDocString(String lhs, String rhs) {
engine.evalJs(lhs + rhs);
}

@Override
@When("^([\\w]+)([^\\s^\\w])(.+)")
public void eval(String name, String dotOrParen, String exp) {
Expand Down
24 changes: 13 additions & 11 deletions karate-core/src/main/java/com/intuit/karate/core/StepRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@
import com.intuit.karate.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -261,8 +253,18 @@ public static Result execute(Step step, Actions actions) {
KarateException e = new KarateException("no step-definition method match found for: " + text);
return Result.failed(System.currentTimeMillis(), 0, e, step);
} else if (matches.size() > 1) {
KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);
return Result.failed(System.currentTimeMillis(), 0, e, step);
boolean evalAssign = false; // special case to support foo.bar = (docstring) in cucumber syntax
for (MethodMatch m : matches) {
if (m.getMethod().getName().equalsIgnoreCase("evalAssignDocString")) {
evalAssign = true;
matches = Collections.singletonList(m);
break;
}
}
if (!evalAssign) {
KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);
return Result.failed(System.currentTimeMillis(), 0, e, step);
}
}
MethodMatch match = matches.get(0);
Object last;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ void testEvalAndSet() {
run("eval-and-set.feature");
}

@Test
void testEvalAssign() {
run("eval-assign.feature");
}

@Test
void testExtract() {
run("extract.feature");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature:

Scenario:
* def foo = { bar: 'one' }
* foo.bar =
"""
{
some: 'big',
message: 'content'
}
"""
* match foo == { bar: { some: 'big', message: 'content' } }

0 comments on commit 41c0454

Please sign in to comment.