diff --git a/README.md b/README.md index d89b21c48..f0a6c7de1 100755 --- a/README.md +++ b/README.md @@ -3267,7 +3267,7 @@ Operation | Description karate.lowerCase(object) | useful to brute-force all keys and values in a JSON or XML payload to lower-case, useful in some cases, see [example](karate-junit4/src/test/java/com/intuit/karate/junit4/demos/lower-case.feature) karate.map(list, function) | functional-style 'map' operation useful to transform list-like objects (e.g. JSON arrays), see [example](karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature), the second argument has to be a JS function (item, [index]) karate.mapWithKey(list, string) | convenient for the common case of transforming an array of primitives into an array of objects, see [JSON transforms](#json-transforms) -karate.match(actual, expected) | brings the power of the *fuzzy* [`match`](#match) syntax into Karate-JS, returns a JSON in the form `{ pass: '#boolean', message: '#string' }` and you can find an example [here](karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature). +karate.match(actual, expected) | brings the power of the *fuzzy* [`match`](#match) syntax into Karate-JS, returns a JSON in the form `{ pass: '#boolean', message: '#string' }` and you can find an example [here](karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature) - you can even place a *full* match expression like this: `karate.match("each foo contains { a: '#number' }")` karate.merge(... maps) | useful to merge the key-values of two (or more) JSON (or map-like) objects, see [JSON transforms](#json-transforms) karate.os | returns the operating system details as JSON, for e.g. `{ type: 'macosx', name: 'Mac OS X' }` - useful for writing conditional logic, the possible `type`-s being: `macosx`, `windows`, `linux` and `unknown` karate.pretty(value) | return a 'pretty-printed', nicely indented string representation of the JSON value, also see: [`print`](#print) diff --git a/karate-core/src/main/java/com/intuit/karate/AssertionResult.java b/karate-core/src/main/java/com/intuit/karate/AssertionResult.java index 6bfcc5465..b9dee1633 100755 --- a/karate-core/src/main/java/com/intuit/karate/AssertionResult.java +++ b/karate-core/src/main/java/com/intuit/karate/AssertionResult.java @@ -23,29 +23,39 @@ */ package com.intuit.karate; +import java.util.HashMap; +import java.util.Map; + /** * * @author pthomas3 */ public class AssertionResult { - + public final String message; public final boolean pass; - + public static final AssertionResult PASS = new AssertionResult(true, null); - + private AssertionResult(boolean pass, String message) { this.pass = pass; this.message = message; } - + public static AssertionResult fail(String message) { return new AssertionResult(false, message); } @Override public String toString() { - return pass ? "passed" : "assertion failed: " + message; - } - + return pass ? "passed" : "assertion failed: " + message; + } + + public Map toMap() { + Map map = new HashMap(2); + map.put("pass", pass); + map.put("message", message); + return map; + } + } diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScriptBridge.java b/karate-core/src/main/java/com/intuit/karate/core/ScriptBridge.java index 6008083ec..07f0a5908 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScriptBridge.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScriptBridge.java @@ -186,11 +186,14 @@ public List valuesOf(Map map) { } public Map match(Object actual, Object expected) { - AssertionResult result = Script.matchNestedObject('.', "$", MatchType.EQUALS, actual, null, actual, expected, context); - Map map = new HashMap(2); - map.put("pass", result.pass); - map.put("message", result.message); - return map; + AssertionResult ar = Script.matchNestedObject('.', "$", MatchType.EQUALS, actual, null, actual, expected, context); + return ar.toMap(); + } + + public Map match(String exp) { + MatchStep ms = new MatchStep(exp); + AssertionResult ar = Script.matchNamed(ms.type, ms.name, ms.path, ms.expected, context); + return ar.toMap(); } public void forEach(Map map, ScriptObjectMirror som) { diff --git a/karate-core/src/test/java/com/intuit/karate/core/MatchStepTest.java b/karate-core/src/test/java/com/intuit/karate/core/MatchStepTest.java index a7d0fed1c..adb4e0ba2 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/MatchStepTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/MatchStepTest.java @@ -1,7 +1,5 @@ package com.intuit.karate.core; -import com.intuit.karate.core.MatchStep; -import com.intuit.karate.core.MatchType; import org.junit.Test; import static org.junit.Assert.*; import static com.intuit.karate.core.MatchType.*; diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature b/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature index 1206e041e..e34a75e3b 100644 --- a/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature @@ -537,9 +537,20 @@ Scenario: contains / not contains Scenario: match in js * def foo = { hello: 'world' } - * def result = karate.match(foo, { hello: '#string'} ) - * match result == { pass: true, message: null } - * if (result.pass) karate.log('*** passed') + * def res = karate.match(foo, { hello: '#string' } ) + * match res == { pass: true, message: null } + * def res = karate.match(foo, { hello: '#number' } ) + * match res == { pass: false, message: '#notnull' } + +Scenario: advanced match in js + * def foo = { a: 1, b: 'foo' } + * def res = karate.match("foo contains { a: '#number' }") + * match res == { pass: true, message: null } + * def res = karate.match("foo == { a: '#number' }") + * match res == { pass: false, message: '#notnull' } + * def foo = [1, 2, 3] + * def res = karate.match("each foo == '#number'") + * match res == { pass: true, message: null } Scenario: karate.os * def temp = karate.os