Skip to content

Commit

Permalink
[rewrite] #1281 wip first cut of call working
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 8, 2020
1 parent 08d0e7f commit 92adbb3
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 247 deletions.
11 changes: 8 additions & 3 deletions karate-core2/src/main/java/com/intuit/karate/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
*/
package com.intuit.karate.match;

import static com.intuit.karate.match.ValidatorResult.*;
import static com.intuit.karate.match.MatchResult.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

/**
*
* @author pthomas3
*/
public class Match {

public interface Validator extends Function<MatchValue, MatchResult> {
//
}

public static final Map<String, Validator> VALIDATORS = new HashMap(11);

static {
Expand Down Expand Up @@ -68,9 +73,9 @@ public static MatchResult execute(MatchType matchType, MatchValue actual, MatchV
MatchOperation mo = new MatchOperation(matchType, actual, expected);
mo.execute();
if (mo.pass) {
return MatchResult.PASS;
return PASS;
} else {
return MatchResult.fail(mo.getFailureReasons());
return fail(mo.getFailureReasons());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,19 @@ private boolean macroEqualsExpected(String expStr) {
if (validatorName.startsWith("regex")) {
String regex = validatorName.substring(5).trim();
RegexValidator validator = new RegexValidator(regex);
ValidatorResult vr = validator.apply(actual);
if (!vr.pass) {
return fail(vr.message);
MatchResult mr = validator.apply(actual);
if (!mr.pass) {
return fail(mr.message);
}
} else {
Validator validator = Match.VALIDATORS.get(validatorName);
Match.Validator validator = Match.VALIDATORS.get(validatorName);
if (validator != null) {
if (optional && actual.isNotPresent()) {
// pass
} else {
ValidatorResult vr = validator.apply(actual);
if (!vr.pass) {
return fail(vr.message);
MatchResult mr = validator.apply(actual);
if (!mr.pass) {
return fail(mr.message);
}
}
} else { // validator part was not used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,23 @@
*
* @author pthomas3
*/
public class RegexValidator implements Validator {
public class RegexValidator implements Match.Validator {

private final Pattern pattern;

public RegexValidator(String regex) {
regex = StringUtils.trimToEmpty(regex);
pattern = Pattern.compile(regex);
}
}

@Override
public ValidatorResult apply(MatchValue v) {
public MatchResult apply(MatchValue v) {
if (!v.isString()) {
return ValidatorResult.fail("not a string");
return MatchResult.fail("not a string");
}
String strValue = v.getValue();
Matcher matcher = pattern.matcher(strValue);
if (matcher.matches()) {
return ValidatorResult.PASS;
}
return ValidatorResult.fail("regex match failed");
Matcher matcher = pattern.matcher(strValue);
return matcher.matches() ? MatchResult.PASS : MatchResult.fail("regex match failed");
}

}
34 changes: 0 additions & 34 deletions karate-core2/src/main/java/com/intuit/karate/match/Validator.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,38 @@ public Path getRootParentPath() {
public Path getPath() {
return feature.getPath();
}

public FeatureRuntime(SuiteRuntime suite, Feature feature) {
this(suite, feature, ScenarioCall.NONE);
}

public FeatureRuntime(ScenarioCall call) {
this(call.parentRuntime.featureRuntime.suite, call.feature, call.parentRuntime.parentCall);
}

public FeatureRuntime(SuiteRuntime suite, Feature feature, ScenarioCall parentCall) {
private FeatureRuntime(SuiteRuntime suite, Feature feature, ScenarioCall parentCall) {
this.suite = suite;
this.feature = feature;
this.parentCall = parentCall;
this.rootFeature = parentCall.isNone() ? this : parentCall.parentRuntime.featureRuntime;
result = new FeatureResult(suite.results, feature);
scenarios = new ScenarioGenerator(this, feature.getSections().iterator());
}

private ScenarioRuntime currentScenario;

public Variable getResultVariable() {
if (currentScenario == null) {
return Variable.NULL;
}
return new Variable(currentScenario.engine.getAllVariablesAsMap());
}

@Override
public void run() {
while (scenarios.hasNext()) {
ScenarioRuntime sr = scenarios.next();
sr.run();
currentScenario = scenarios.next();
currentScenario.run();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,35 @@ public class ScenarioCall {
public static final ScenarioCall NONE = new ScenarioCall(null, null);

public final ScenarioRuntime parentRuntime;
public final int callDepth;
public final int depth;
public final Feature feature;

private boolean callonce;
private Object arg;
private Variable arg;
private boolean globalScope;

public void setArg(Object arg) {
public void setGlobalScope(boolean globalScope) {
this.globalScope = globalScope;
}

public boolean isGlobalScope() {
return globalScope;
}

public void setArg(Variable arg) {
this.arg = arg;
}

public Variable getArg() {
return arg;
}

public boolean isCallonce() {
return callonce;
}

public boolean isNone() {
return callDepth == 0;
return depth == 0;
}

public void setCallonce(boolean callonce) {
Expand All @@ -56,9 +73,9 @@ public ScenarioCall(ScenarioRuntime parentRuntime, Feature feature) {
this.parentRuntime = parentRuntime;
this.feature = feature;
if (parentRuntime == null) {
callDepth = 0;
depth = 0;
} else {
callDepth = parentRuntime.parentCall.callDepth + 1;
depth = parentRuntime.parentCall.depth + 1;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.intuit.karate.Logger;
import com.intuit.karate.StringUtils;
import com.intuit.karate.XmlUtils;
import com.intuit.karate.core.Feature;
import com.intuit.karate.data.Json;
import com.intuit.karate.data.JsonUtils;
import com.intuit.karate.graal.JsEngine;
Expand Down Expand Up @@ -95,6 +96,16 @@ public void put(String key, Object value) {
}
}

public void putAll(Map<String, Object> map) {
map.forEach((k, v) -> put(k, v));
}

public Map<String, Object> getAllVariablesAsMap() {
Map<String, Object> map = new HashMap(vars.size());
vars.forEach((k, v) -> map.put(k, v == null ? null : v.getValue()));
return map;
}

private static void validateVariableName(String name) {
if (!isValidVariableName(name)) {
throw new RuntimeException("invalid variable name: " + name);
Expand Down Expand Up @@ -382,7 +393,7 @@ public void replace(String name, String token, String value) {
String replaced = replacePlaceholderText(text, token, value);
vars.put(name, new Variable(replaced));
}

private static final String TOKEN = "token";

public void replaceTable(String text, List<Map<String, String>> list) {
Expand All @@ -407,8 +418,8 @@ public void replaceTable(String text, List<Map<String, String>> list) {
replace(text, token, value);
}
}
}

}

public void remove(String name, String path) {
set(name, path, null, true, false);
Expand Down Expand Up @@ -756,12 +767,28 @@ public static StringUtils.Pair parseCallArgs(String line) {
return new StringUtils.Pair(line.substring(0, pos), line.substring(pos));
}

public static Variable call(Variable called, String argString, boolean reuseParentConfig) {
return null;
public Variable call(boolean callOnce, String exp, boolean reuseParentConfig) {
StringUtils.Pair pair = parseCallArgs(exp);
Variable called = evalKarateExpression(pair.left);
Variable arg = pair.right == null ? null : evalKarateExpression(pair.right);
switch (called.type) {
case JAVA_FUNCTION:
case JS_FUNCTION:
return arg == null ? called.invokeFunction() : called.invokeFunction(new Object[]{arg.getValue()});
case KARATE_FEATURE:
return callFeature(called.getValue(), arg, reuseParentConfig);
default:
throw new RuntimeException("not a callable feature or js function: " + called);
}
}

private static Variable callWithCache(String callKey, Variable called, String arg, boolean reuseParentConfig) {
return null;
public Variable callFeature(Feature feature, Variable arg, boolean reuseParentConfig) {
ScenarioRuntime runtime = ScenarioRuntime.LOCAL.get();
ScenarioCall call = new ScenarioCall(runtime, feature);
call.setArg(arg);
FeatureRuntime fr = new FeatureRuntime(call);
fr.run();
return fr.getResultVariable();
}

public Variable evalJsonPath(Variable v, String path) {
Expand Down Expand Up @@ -840,13 +867,7 @@ public Variable evalKarateExpression(String text) {
} else {
text = text.substring(5);
}
StringUtils.Pair pair = parseCallArgs(text);
Variable called = evalKarateExpression(pair.left);
if (callOnce) {
return callWithCache(pair.left, called, pair.right, false);
} else {
return call(called, pair.right, false);
}
return call(callOnce, text, false);
} else if (isDollarPrefixedJsonPath(text)) {
return evalJsonPathOnVariableByName(VariableNames.RESPONSE, text);
} else if (isGetSyntax(text) || isDollarPrefixed(text)) { // special case in form
Expand Down
Loading

0 comments on commit 92adbb3

Please sign in to comment.