Script { public static ScriptValue assign(String name, String exp, ScenarioContext context) { return assign(AssignType.AUTO, name, exp, context, true); } 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 testCastJsonToString() { ScenarioContext ctx = getContext(); Script.assign("myJson", "{ root: { foo: 'bar' } }", ctx); Script.assign(AssignType.STRING, "myString", "myJson", ctx, true); ScriptValue value = ctx.vars.get("myString"); assertEquals(ScriptValue.Type.STRING, value.getType()); assertEquals("{\"root\":{\"foo\":\"bar\"}}", value.getAsString()); }
@Test public void testCastStringToJson() { ScenarioContext ctx = getContext(); Script.assign("myString", "{\"root\":{\"foo\":\"bar\"}}", ctx); Script.assign(AssignType.JSON, "myJson", "myString", ctx, true); ScriptValue value = ctx.vars.get("myJson"); assertEquals(ScriptValue.Type.JSON, value.getType()); assertEquals("{\"root\":{\"foo\":\"bar\"}}", value.getAsString()); }
@Test public void testCastJsonToXml() { ScenarioContext ctx = getContext(); Script.assign("myJson", "{ root: { foo: 'bar' } }", ctx); Script.assign(AssignType.XML, "myXml", "myJson", ctx, true); ScriptValue value = ctx.vars.get("myXml"); assertEquals(ScriptValue.Type.XML, value.getType()); assertEquals("<root><foo>bar</foo></root>", value.getAsString()); }
@Test public void testCastStringToXml() { ScenarioContext ctx = getContext(); Script.assign(AssignType.STRING, "myString", "<root><foo>bar</foo></root>", ctx, true); Script.assign(AssignType.XML, "myXml", "myString", ctx, true); ScriptValue value = ctx.vars.get("myXml"); assertEquals(ScriptValue.Type.XML, value.getType()); assertEquals("<root><foo>bar</foo></root>", value.getAsString()); }
@Test public void testCastXmlToString() { ScenarioContext ctx = getContext(); Script.assign("myXml", "<root><foo>bar</foo></root>", ctx); Script.assign(AssignType.XML_STRING, "myString", "myXml", ctx, true); ScriptValue value = ctx.vars.get("myString"); assertEquals(ScriptValue.Type.STRING, value.getType()); assertEquals("<root><foo>bar</foo></root>", value.getValue()); }
@Test public void testAssigningAndCallingFunctionThatCanBeUsedToAssignVariable() { ScenarioContext ctx = getContext(); Script.assign("foo", "function(){ return 'world' }", ctx); Script.assign("hello", "call foo", ctx); ScriptValue hello = ctx.vars.get("hello"); assertEquals("world", hello.getValue()); }
@Test public void testAssigningAndCallingFunctionWithArgumentsThatCanBeUsedToAssignVariable() { ScenarioContext ctx = getContext(); Script.assign("foo", "function(pre){ return pre + ' world' }", ctx); Script.assign("hello", "call foo 'hello'", ctx); ScriptValue hello = ctx.vars.get("hello"); assertEquals("hello world", hello.getValue()); }
@Test public void testCallingFunctionWithJsonArray() { ScenarioContext ctx = getContext(); Script.assign("foo", "function(a){ return a[0] }", ctx); Script.assign("bar", "call foo ['hello']", ctx); ScriptValue bar = ctx.vars.get("bar"); assertEquals("hello", bar.getValue()); }
@Test public void testCallingFunctionWithJavaList() { ScenarioContext ctx = getContext(); Script.assign("foo", "function(a){ return a[0] }", ctx); Script.assign("bar", "['hello']", ctx); Script.assign("baz", "call foo bar", ctx); ScriptValue baz = ctx.vars.get("baz"); assertEquals("hello", baz.getValue()); }
@Test public void testKarateEnvAccessFromScript() { FeatureContext featureContext = FeatureContext.forEnv("baz"); CallContext callContext = new CallContext(null, true); ScenarioContext ctx = new ScenarioContext(featureContext, callContext, null, null); Script.assign("foo", "function(){ return karate.env }", ctx); Script.assign("bar", "call foo", ctx); ScriptValue bar = ctx.vars.get("bar"); assertEquals("baz", bar.getValue()); featureContext = FeatureContext.forEnv(); ctx = new ScenarioContext(featureContext, callContext, null, null); Script.assign("foo", "function(){ return karate.env }", ctx); Script.assign("bar", "call foo", ctx); bar = ctx.vars.get("bar"); assertNull(bar.getValue()); }