How to use macroToMatchType method of com.intuit.karate.MatchOperation class

Best Karate code snippet using com.intuit.karate.MatchOperation.macroToMatchType

Source:MatchOperation.java Github

copy

Full Screen

...95 default:96 throw new RuntimeException("unexpected outer match type: " + type);97 }98 }99 private static Match.Type macroToMatchType(boolean each, String macro) {100 if (macro.startsWith("^^")) {101 return each ? Match.Type.EACH_CONTAINS_ONLY : Match.Type.CONTAINS_ONLY;102 } else if (macro.startsWith("^+")) {103 return each ? Match.Type.EACH_CONTAINS_DEEP : Match.Type.CONTAINS_DEEP;104 } else if (macro.startsWith("^*")) {105 return each ? Match.Type.EACH_CONTAINS_ANY : Match.Type.CONTAINS_ANY;106 } else if (macro.startsWith("^")) {107 return each ? Match.Type.EACH_CONTAINS : Match.Type.CONTAINS;108 } else if (macro.startsWith("!^")) {109 return each ? Match.Type.EACH_NOT_CONTAINS : Match.Type.NOT_CONTAINS;110 } else if (macro.startsWith("!=")) {111 return each ? Match.Type.EACH_NOT_EQUALS : Match.Type.NOT_EQUALS;112 } else {113 return each ? Match.Type.EACH_EQUALS : Match.Type.EQUALS;114 }115 }116 private static int matchTypeToStartPos(Match.Type mt) {117 switch (mt) {118 case CONTAINS_ONLY:119 case EACH_CONTAINS_ONLY:120 case CONTAINS_DEEP:121 case EACH_CONTAINS_DEEP:122 case CONTAINS_ANY:123 case EACH_CONTAINS_ANY:124 case NOT_CONTAINS:125 case EACH_NOT_CONTAINS:126 case NOT_EQUALS:127 case EACH_NOT_EQUALS:128 return 2;129 case CONTAINS:130 case EACH_CONTAINS:131 return 1;132 default:133 return 0;134 }135 }136 boolean execute() {137 switch (type) {138 case EACH_CONTAINS:139 case EACH_NOT_CONTAINS:140 case EACH_CONTAINS_ONLY:141 case EACH_CONTAINS_ANY:142 case EACH_EQUALS:143 case EACH_NOT_EQUALS:144 case EACH_CONTAINS_DEEP:145 if (actual.isList()) {146 List list = actual.getValue();147 Match.Type nestedMatchType = fromMatchEach();148 int count = list.size();149 for (int i = 0; i < count; i++) {150 Object o = list.get(i);151 context.JS.put("_$", o);152 MatchOperation mo = new MatchOperation(context.descend(i), nestedMatchType, new Match.Value(o), expected);153 mo.execute();154 context.JS.bindings.removeMember("_$");155 if (!mo.pass) {156 return fail("match each failed at index " + i);157 }158 }159 // if we reached here all / each LHS items completed successfully160 return true;161 } else {162 return fail("actual is not an array or list");163 }164 default:165 // do nothing166 }167 if (actual.isNotPresent()) {168 if (!expected.isString() || !expected.getAsString().startsWith("#")) {169 return fail("actual path does not exist");170 }171 }172 if (actual.type != expected.type) {173 switch (type) {174 case CONTAINS:175 case NOT_CONTAINS:176 case CONTAINS_ANY:177 case CONTAINS_ONLY:178 case CONTAINS_DEEP:179 case CONTAINS_ANY_DEEP:180 if (!expected.isList()) {181 MatchOperation mo = new MatchOperation(context, type, actual, new Match.Value(Collections.singletonList(expected.getValue())));182 mo.execute();183 return mo.pass ? pass() : fail(mo.failReason);184 }185 break;186 default:187 // do nothing188 }189 if (expected.isXml() && actual.isMap()) {190 // special case, auto-convert rhs 191 MatchOperation mo = new MatchOperation(context, type, actual, new Match.Value(XmlUtils.toObject(expected.getValue(), true)));192 mo.execute();193 return mo.pass ? pass() : fail(mo.failReason);194 }195 if (expected.isString()) {196 String expStr = expected.getValue();197 if (!expStr.startsWith("#")) { // edge case if rhs is macro198 return type == Match.Type.NOT_EQUALS ? pass() : fail("data types don't match");199 }200 } else {201 return type == Match.Type.NOT_EQUALS ? pass() : fail("data types don't match");202 }203 }204 if (expected.isString()) {205 String expStr = expected.getValue();206 if (expStr.startsWith("#")) {207 switch (type) {208 case NOT_EQUALS:209 return macroEqualsExpected(expStr) ? fail("is equal") : pass();210 case NOT_CONTAINS:211 return macroEqualsExpected(expStr) ? fail("actual contains expected") : pass();212 default:213 return macroEqualsExpected(expStr) ? pass() : fail(null);214 }215 }216 }217 switch (type) {218 case EQUALS:219 return actualEqualsExpected() ? pass() : fail("not equal");220 case NOT_EQUALS:221 return actualEqualsExpected() ? fail("is equal") : pass();222 case CONTAINS:223 case CONTAINS_ANY:224 case CONTAINS_ONLY:225 case CONTAINS_DEEP:226 case CONTAINS_ANY_DEEP:227 return actualContainsExpected() ? pass() : fail("actual does not contain expected");228 case NOT_CONTAINS:229 return actualContainsExpected() ? fail("actual contains expected") : pass();230 default:231 throw new RuntimeException("unexpected match type: " + type);232 }233 }234 private boolean macroEqualsExpected(String expStr) {235 boolean optional = expStr.startsWith("##");236 if (optional && actual.isNull()) { // exit early237 return true;238 }239 int minLength = optional ? 3 : 2;240 if (expStr.length() > minLength) {241 String macro = expStr.substring(minLength - 1);242 if (macro.startsWith("(") && macro.endsWith(")")) {243 macro = macro.substring(1, macro.length() - 1);244 Match.Type nestedType = macroToMatchType(false, macro);245 int startPos = matchTypeToStartPos(nestedType);246 macro = macro.substring(startPos);247 if (actual.isList()) { // special case, look for partial maps within list248 if (nestedType == Match.Type.CONTAINS) {249 nestedType = Match.Type.CONTAINS_DEEP;250 } else if (nestedType == Match.Type.CONTAINS_ANY) {251 nestedType = Match.Type.CONTAINS_ANY_DEEP;252 }253 }254 context.JS.put("$", context.root.actual.getValue());255 context.JS.put("_", actual.getValue());256 JsValue jv = context.JS.eval(macro);257 context.JS.bindings.removeMember("$");258 context.JS.bindings.removeMember("_");259 MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv.getValue()));260 return mo.execute();261 } else if (macro.startsWith("[")) {262 int closeBracketPos = macro.indexOf(']');263 if (closeBracketPos != -1) { // array, match each264 if (!actual.isList()) {265 return fail("actual is not an array");266 }267 if (closeBracketPos > 1) {268 String bracketContents = macro.substring(1, closeBracketPos);269 List listAct = actual.getValue();270 int listSize = listAct.size();271 context.JS.put("$", context.root.actual.getValue());272 context.JS.put("_", listSize);273 String sizeExpr;274 if (bracketContents.indexOf('_') != -1) { // #[_ < 5] 275 sizeExpr = bracketContents;276 } else { // #[5] | #[$.foo] 277 sizeExpr = bracketContents + " == _";278 }279 JsValue jv = context.JS.eval(sizeExpr);280 context.JS.bindings.removeMember("$");281 context.JS.bindings.removeMember("_");282 if (!jv.isTrue()) {283 return fail("actual array length is " + listSize);284 }285 }286 if (macro.length() > closeBracketPos + 1) {287 macro = StringUtils.trimToNull(macro.substring(closeBracketPos + 1));288 if (macro != null) {289 if (macro.startsWith("(") && macro.endsWith(")")) {290 macro = macro.substring(1, macro.length() - 1); // strip parens291 }292 if (macro.startsWith("?")) { // #[]? _.length == 3293 macro = "#" + macro;294 }295 if (macro.startsWith("#")) {296 MatchOperation mo = new MatchOperation(context, Match.Type.EACH_EQUALS, actual, new Match.Value(macro));297 mo.execute();298 return mo.pass ? pass() : fail("all array elements matched");299 } else { // schema reference300 Match.Type nestedType = macroToMatchType(true, macro); // match each301 int startPos = matchTypeToStartPos(nestedType);302 macro = macro.substring(startPos);303 JsValue jv = context.JS.eval(macro);304 MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv.getValue()));305 return mo.execute();306 }307 }308 }309 return true; // expression within square brackets is ok310 }311 } else { // '#? _ != 0' | '#string' | '#number? _ > 0'312 int questionPos = macro.indexOf('?');313 String validatorName = null;314 // in case of regex we don't want to remove the '?'...

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1def matchType = MatchOperation.macroToMatchType('contains')2def matchType = MatchOperation.macroToMatchType('contains')3def matchType = MatchOperation.macroToMatchType('contains')4def matchType = MatchOperation.macroToMatchType('contains')5def matchType = MatchOperation.macroToMatchType('contains')6def matchType = MatchOperation.macroToMatchType('contains')7def matchType = MatchOperation.macroToMatchType('contains')8def matchType = MatchOperation.macroToMatchType('contains')9def matchType = MatchOperation.macroToMatchType('contains')10def matchType = MatchOperation.macroToMatchType('contains')11def matchType = MatchOperation.macroToMatchType('contains')12def matchType = MatchOperation.macroToMatchType('contains')

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1def actual = { "name": "John", "age": 30, "city": "New York" }2def expected = { "name": match.macroToMatchType("John"), "age": match.macroToMatchType(30), "city": match.macroToMatchType("New York") }3match.assertMatch(actual, expected)4def actual = { "name": "John", "age": 30, "city": "New York" }5def expected = { "name": match.macroToMatchType("John"), "age": match.macroToMatchType(30), "city": match.macroToMatchType("New York") }6match.assertMatch(actual, expected)7def actual = { "name": "John", "age": 30, "city": "New York" }8def expected = { "name": match.macroToMatchType("John"), "age": match.macroToMatchType(30), "city": match.macroToMatchType("New York") }9match.assertMatch(actual, expected)10def actual = { "name": "John", "age": 30, "city": "New York" }11def expected = { "name": match.macroToMatchType("John"), "age": match.macroToMatchType(30), "city": match.macroToMatchType("New York") }12match.assertMatch(actual, expected)13def actual = { "name": "John", "age": 30, "city": "New York" }14def expected = { "name": match.macroToMatchType("John"), "age": match.macroToMatchType(30), "city": match.macroToMatchType("New York") }

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1* def type = result.macroToMatchType('string')2* def type = result.macroToMatchType('number')3* def type = result.macroToMatchType('boolean')4* def type = result.macroToMatchType('null')5* def type = result.macroToMatchType('array')6* def type = result.macroToMatchType('object')7* def type = result.macroToMatchType('function')8* def type = result.macroToMatchType('date')9* def type = result.macroToMatchType('binary')10* def type = result.macroToMatchType('regex')11* def type = result.macroToMatchType('any')12* def type = result.macroToMatchType('xml')13* def type = result.macroToMatchType('json')14* def type = result.macroToMatchType('jsonString')15* def type = result.macroToMatchType('xmlString')16* def type = result.macroToMatchType('gpath')17* def type = result.macroToMatchType('xpath')

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1def match = { String s -> s == 'foo' }2def matchType = MatchOperation.macroToMatchType(match)3def match = { String s -> s == 'foo' }4def matchType = MatchOperation.macroToMatchType(match)5def match = { String s -> s == 'foo' }6def matchType = MatchOperation.macroToMatchType(match)7def match = { String s -> s == 'foo' }8def matchType = MatchOperation.macroToMatchType(match)9def match = { String s -> s == 'foo' }10def matchType = MatchOperation.macroToMatchType(match)11def match = { String s -> s == 'foo' }12def matchType = MatchOperation.macroToMatchType(match)13def match = { String s -> s == 'foo' }14def matchType = MatchOperation.macroToMatchType(match)15def match = { String s -> s == 'foo' }16def matchType = MatchOperation.macroToMatchType(match)17def match = { String s -> s == 'foo' }18def matchType = MatchOperation.macroToMatchType(match)19def match = { String s -> s == 'foo' }20def matchType = MatchOperation.macroToMatchType(match)

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1def resp = call read('classpath:com/intuit/karate/core/feature-matching.feature')2match.macroToMatchType(resp, 'response', 'status', 200)3match.macroToMatchType(resp, 'response', 'body', { it.contains('Hello World') })4match.macroToMatchType(resp, 'response', 'headers', { it['Content-Type'] == 'text/plain' })5match.macroToMatchType(resp, 'response', 'headers[Content-Type]', 'text/plain')6match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it == 'text/plain' })7match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.contains('text') })8match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.contains('plain') })9match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.startsWith('text') })10match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.endsWith('plain') })11* def resp = call read('classpath:com/intuit/karate/core/feature-matching.feature')12* match.macroToMatchType(resp, 'response', 'status', 200)13* match.macroToMatchType(resp, 'response', 'body', { it.contains('Hello World') })14* match.macroToMatchType(resp, 'response', 'headers', { it['Content-Type'] == 'text/plain' })15* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', 'text/plain')16* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it == 'text/plain' })17* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.contains('text') })18* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.contains('plain') })19* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.startsWith('text') })20* match.macroToMatchType(resp, 'response', 'headers[Content-Type]', { it.endsWith('plain') })

Full Screen

Full Screen

macroToMatchType

Using AI Code Generation

copy

Full Screen

1def json = { name: 'John', age: 30 }2def result = match json {3}4def json = { name: 'John', age: '30' }5def result = match json {6}7def json = { name: 'John', age: '30' }8def result = match json {9}10def json = { name: 'John', age: '30' }11def result = match json {12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Karate automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful