Skip to content

Commit

Permalink
a big one - finally we have a js api to do all the match types #1202
Browse files Browse the repository at this point in the history
yes that means each, contains, anything supported in the dsl / gherkin side
I resisted this for years because designing an api for all the types was so painful
but then it struck me - the match statement is parsed as plain-text and we have a java class for that
not the most conventional way - but hey you are not supposed to be doing too much in js
which was the other reason why I avoided this until now - anyway
  • Loading branch information
ptrthomas committed Jul 3, 2020
1 parent 8048c42 commit 1f67ab7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3267,7 +3267,7 @@ Operation | Description
<a name="karate-lowercase"><code>karate.lowerCase(object)</code></a> | 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)
<a name="karate-map"><code>karate.map(list, function)</code></a> | 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])
<a name="karate-mapwithkey"><code>karate.mapWithKey(list, string)</code></a> | convenient for the common case of transforming an array of primitives into an array of objects, see [JSON transforms](#json-transforms)
<a name="karate-match"><code>karate.match(actual, expected)</code></a> | 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).
<a name="karate-match"><code>karate.match(actual, expected)</code></a> | 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' }")`
<a name="karate-merge"><code>karate.merge(... maps)</code></a> | useful to merge the key-values of two (or more) JSON (or map-like) objects, see [JSON transforms](#json-transforms)
<a name="karate-os"><code>karate.os</code></a> | 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`
<a name="karate-pretty"><code>karate.pretty(value)</code></a> | return a 'pretty-printed', nicely indented string representation of the JSON value, also see: [`print`](#print)
Expand Down
24 changes: 17 additions & 7 deletions karate-core/src/main/java/com/intuit/karate/AssertionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> toMap() {
Map<String, Object> map = new HashMap(2);
map.put("pass", pass);
map.put("message", message);
return map;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,14 @@ public List valuesOf(Map map) {
}

public Map<String, Object> match(Object actual, Object expected) {
AssertionResult result = Script.matchNestedObject('.', "$", MatchType.EQUALS, actual, null, actual, expected, context);
Map<String, Object> 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<String, Object> 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<String, Object> map, ScriptObjectMirror som) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f67ab7

Please sign in to comment.