Script { public static ScriptValue evalKarateExpression(String text, ScenarioContext context) { text = StringUtils.trimToNull(text); if (text == null) { return ScriptValue.NULL; } ScriptValue varValue = getIfVariableReference(text, context); if (varValue != null) { return varValue; } boolean callOnce = isCallOnceSyntax(text); if (callOnce || isCallSyntax(text)) { if (callOnce) { text = text.substring(9); } else { text = text.substring(5); } StringUtils.Pair pair = parseCallArgs(text); ScriptValue called = evalKarateExpression(pair.left, context); if (callOnce) { return callWithCache(pair.left, called, pair.right, context, false); } else { return call(called, pair.right, context, false); } } else if (isDollarPrefixedJsonPath(text)) { return evalJsonPathOnVarByName(ScriptValueMap.VAR_RESPONSE, text, context); } else if (isGetSyntax(text) || isDollarPrefixed(text)) { int index = -1; if (text.startsWith("$")) { text = text.substring(1); } else if (text.startsWith("get[")) { int pos = text.indexOf(']'); index = Integer.valueOf(text.substring(4, pos)); text = text.substring(pos + 2); } else { text = text.substring(4); } String left; String right; if (isDollarPrefixedJsonPath(text)) { left = ScriptValueMap.VAR_RESPONSE; right = text; } else if (isVariableAndSpaceAndPath(text)) { int pos = text.indexOf(' '); right = text.substring(pos + 1); left = text.substring(0, pos); } else { StringUtils.Pair pair = parseVariableAndPath(text); left = pair.left; right = pair.right; } ScriptValue sv; if (isXmlPath(right) || isXmlPathFunction(right)) { sv = evalXmlPathOnVarByName(left, right, context); } else { sv = evalJsonPathOnVarByName(left, right, context); } if (index != -1 && sv.isListLike()) { List list = sv.getAsList(); if (!list.isEmpty()) { return new ScriptValue(list.get(index)); } } return sv; } else if (isJson(text)) { DocumentContext doc = JsonUtils.toJsonDoc(text); evalJsonEmbeddedExpressions(doc, context); return new ScriptValue(doc); } else if (isXml(text)) { Document doc = XmlUtils.toXmlDoc(text); evalXmlEmbeddedExpressions(doc, context); return new ScriptValue(doc); } else if (isXmlPath(text)) { return evalXmlPathOnVarByName(ScriptValueMap.VAR_RESPONSE, text, context); } else { return evalJsExpression(text, context); } } private  Script(); static final boolean isCallSyntax(String text); static final boolean isCallOnceSyntax(String text); static final boolean isGetSyntax(String text); static final boolean isJson(String text); static final boolean isJsonPath(String text); static final boolean isXml(String text); static final boolean isXmlPath(String text); static final boolean isXmlPathFunction(String text); static final boolean isEmbeddedExpression(String text); static final boolean isWithinParentheses(String text); static final boolean isContainsMacro(String text); static final boolean isContainsOnlyMacro(String text); static final boolean isContainsAnyMacro(String text); static final boolean isNotContainsMacro(String text); static final boolean isDollarPrefixedJsonPath(String text); static final boolean isDollarPrefixedParen(String text); static final boolean isDollarPrefixed(String text); static final boolean isVariable(String text); static final boolean isVariableAndSpaceAndPath(String text); static StringUtils.Pair parseVariableAndPath(String text); static ScriptValue evalKarateExpressionForMatch(String text, ScenarioContext context); static ScriptValue getIfVariableReference(String text, ScenarioContext context); static ScriptValue evalKarateExpression(String text, ScenarioContext context); static ScriptValue evalXmlPathOnVarByName(String name, String path, ScenarioContext context); static ScriptValue evalXmlPathOnXmlNode(Node doc, String path); static ScriptValue evalJsonPathOnVarByName(String name, String exp, ScenarioContext context); static ScriptValue evalJsExpression(String exp, ScenarioContext context); static ScriptValue evalJsExpression(String exp, ScenarioContext context, ScriptValue selfValue, Object root, Object parent); static boolean isValidVariableName(String name); static void evalJsonEmbeddedExpressions(DocumentContext doc, ScenarioContext context); static void evalXmlEmbeddedExpressions(Node node, ScenarioContext context); static ScriptValue copy(String name, String exp, ScenarioContext context, boolean validateName); static ScriptValue assign(String name, String exp, ScenarioContext context); static ScriptValue assign(AssignType assignType, String name, String exp, ScenarioContext context, boolean validateName); static DocumentContext toJsonDoc(ScriptValue sv, ScenarioContext context); static AssertionResult matchNamed(MatchType matchType, String expression, String path, String expected, ScenarioContext context); static AssertionResult matchScriptValue(MatchType matchType, ScriptValue actual, String path, String expected, ScenarioContext context); static AssertionResult matchString(MatchType matchType, ScriptValue actual, String expected, String path, ScenarioContext context); static boolean isMacro(String text); static boolean isOptionalMacro(String text); static AssertionResult matchStringOrPattern(char delimiter, String path, MatchType stringMatchType,
            Object actRoot, Object actParent, ScriptValue actValue, String expected, ScenarioContext context); static AssertionResult matchXml(MatchType matchType, ScriptValue actual, String path, String expression, ScenarioContext context); static AssertionResult matchJsonOrObject(MatchType matchType, ScriptValue actual, String path, String expression, ScenarioContext context); static AssertionResult matchFailed(MatchType matchType, String path,
            Object actObject, Object expObject, String reason); static AssertionResult matchNestedObject(char delimiter, String path, MatchType matchType,
            Object actRoot, Object actParent, Object actObject, Object expObject, ScenarioContext context); static boolean isPrimitive(Class clazz); static void removeValueByPath(String name, String path, ScenarioContext context); static void setValueByPath(String name, String path, ScriptValue value, ScenarioContext context); static void setValueByPath(String name, String path, String exp, ScenarioContext context); static void setValueByPath(String name, String path, String exp, boolean delete, ScenarioContext context, boolean viaTable); static void setValueByPath(String name, String path, ScriptValue value, boolean delete, ScenarioContext context, boolean viaTable); static ScriptValue call(ScriptValue called, String argString, ScenarioContext context, boolean reuseParentConfig); static ScriptValue evalJavaFunctionCall(Function function, Object callArg, ScenarioContext context); static ScriptValue evalJsFunctionCall(ScriptObjectMirror som, Object callArg, ScenarioContext context); static ScriptValue evalFeatureCall(Feature feature, Object callArg, ScenarioContext context, boolean reuseParentConfig); static StringUtils.Pair parseCallArgs(String line); static void callAndUpdateConfigAndAlsoVarsIfMapReturned(boolean callOnce, String name, String arg, ScenarioContext context); static AssertionResult assertBoolean(String expression, ScenarioContext context); static String replacePlaceholderText(String text, String token, String replaceWith, ScenarioContext context); static String replacePlaceholders(String text, List<Map<String, String>> list, ScenarioContext context); static List<Map<String, Object>> evalTable(List<Map<String, String>> list, ScenarioContext context); static void setByPathTable(String name, String path, List<Map<String, String>> list, ScenarioContext context); static final String VAR_SELF; static final String VAR_ROOT; static final String VAR_PARENT; static final String VAR_LOOP; static final String VAR_ARG; static final String VAR_HEADER; static final Map<String, Validator> VALIDATORS; }
@Test public void testEvalUrl() { ScenarioContext ctx = getContext(); String url = "'http: assertEquals("http: }
@Test public void testEvalParamWithDot() { ScenarioContext ctx = getContext(); String param = "'ACS.Itself'"; assertEquals("ACS.Itself", Script.evalKarateExpression(param, ctx).getAsString()); }