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

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

Source:MatchOperation.java Github

copy

Full Screen

...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 '?'315 if (questionPos != -1 && !macro.startsWith(REGEX)) {316 validatorName = macro.substring(0, questionPos);317 if (macro.length() > questionPos + 1) {318 macro = StringUtils.trimToEmpty(macro.substring(questionPos + 1));319 } else {320 macro = "";321 }322 } else {323 validatorName = macro;324 macro = "";325 }326 validatorName = StringUtils.trimToNull(validatorName);327 if (validatorName != null) {328 Match.Validator validator = null;329 if (validatorName.startsWith(REGEX)) {330 String regex = validatorName.substring(5).trim();331 validator = new Match.RegexValidator(regex);332 } else {333 validator = Match.VALIDATORS.get(validatorName);334 }335 if (validator != null) {336 if (optional && (actual.isNotPresent() || actual.isNull())) {337 // pass338 } else if (!optional && actual.isNotPresent()) {339 // if the element is not present the expected result can only be340 // the notpresent keyword, ignored or an optional comparison341 return expected.isNotPresent() || "#ignore".contentEquals(expected.getAsString());342 } else {343 Match.Result mr = validator.apply(actual);344 if (!mr.pass) {345 return fail(mr.message);346 }347 }348 } else if (!validatorName.startsWith(REGEX)) { // expected is a string that happens to start with "#"349 String actualValue = actual.getValue();350 switch (type) {351 case CONTAINS:352 return actualValue.contains(expStr);353 default:354 return actualValue.equals(expStr);355 }356 }357 }358 macro = StringUtils.trimToNull(macro);359 if (macro != null && questionPos != -1) {360 context.JS.put("$", context.root.actual.getValue());361 context.JS.put("_", actual.getValue());362 JsValue jv = context.JS.eval(macro);363 context.JS.bindings.removeMember("$");364 context.JS.bindings.removeMember("_");365 if (!jv.isTrue()) {366 return fail("evaluated to 'false'");367 }368 }369 }370 }371 return true; // all ok372 }373 private boolean actualEqualsExpected() {374 switch (actual.type) {375 case NULL:376 return true; // both are null377 case BOOLEAN:378 boolean actBoolean = actual.getValue();379 boolean expBoolean = expected.getValue();380 return actBoolean == expBoolean;381 case NUMBER:382 if (actual.getValue() instanceof BigDecimal || expected.getValue() instanceof BigDecimal) {383 BigDecimal actBigDecimal = toBigDecimal(actual.getValue());384 BigDecimal expBigDecimal = toBigDecimal(expected.getValue());385 return actBigDecimal.compareTo(expBigDecimal) == 0;386 } else {387 Number actNumber = actual.getValue();388 Number expNumber = expected.getValue();389 return actNumber.doubleValue() == expNumber.doubleValue();390 }391 case STRING:392 return actual.getValue().equals(expected.getValue());393 case BYTES:394 byte[] actBytes = actual.getValue();395 byte[] expBytes = expected.getValue();396 return Arrays.equals(actBytes, expBytes);397 case LIST:398 List actList = actual.getValue();399 List expList = expected.getValue();400 int actListCount = actList.size();401 int expListCount = expList.size();402 if (actListCount != expListCount) {403 return fail("actual array length is not equal to expected - " + actListCount + ":" + expListCount);404 }405 for (int i = 0; i < actListCount; i++) {406 Match.Value actListValue = new Match.Value(actList.get(i));407 Match.Value expListValue = new Match.Value(expList.get(i));408 MatchOperation mo = new MatchOperation(context.descend(i), Match.Type.EQUALS, actListValue, expListValue);409 mo.execute();410 if (!mo.pass) {411 return fail("array match failed at index " + i);412 }413 }414 return true;415 case MAP:416 Map<String, Object> actMap = actual.getValue();417 Map<String, Object> expMap = expected.getValue();418 return matchMapValues(actMap, expMap);419 case XML:420 Map<String, Object> actXml = (Map) XmlUtils.toObject(actual.getValue(), true);421 Map<String, Object> expXml = (Map) XmlUtils.toObject(expected.getValue(), true);422 return matchMapValues(actXml, expXml);423 case OTHER:424 return actual.getValue().equals(expected.getValue());425 default:426 throw new RuntimeException("unexpected type (match equals): " + actual.type);427 }428 }429 private boolean matchMapValues(Map<String, Object> actMap, Map<String, Object> expMap) { // combined logic for equals and contains430 if (actMap.size() > expMap.size() && (type == Match.Type.EQUALS || type == Match.Type.CONTAINS_ONLY)) {431 int sizeDiff = actMap.size() - expMap.size();432 Map<String, Object> diffMap = new LinkedHashMap(actMap);433 for (String key : expMap.keySet()) {434 diffMap.remove(key);435 }436 return fail("actual has " + sizeDiff + " more key(s) than expected - " + JsonUtils.toJson(diffMap));437 }438 Set<String> unMatchedKeysAct = new LinkedHashSet(actMap.keySet());439 Set<String> unMatchedKeysExp = new LinkedHashSet(expMap.keySet());440 for (Map.Entry<String, Object> expEntry : expMap.entrySet()) {441 String key = expEntry.getKey();442 Object childExp = expEntry.getValue();443 if (!actMap.containsKey(key)) {444 if (childExp instanceof String) {445 String childString = (String) childExp;446 if (childString.startsWith("##") || childString.equals("#ignore") || childString.equals("#notpresent")) {447 if (type == Match.Type.CONTAINS_ANY || type == Match.Type.CONTAINS_ANY_DEEP) {448 return true; // exit early449 }450 unMatchedKeysExp.remove(key);451 if (unMatchedKeysExp.isEmpty()) {452 if (type == Match.Type.CONTAINS || type == Match.Type.CONTAINS_DEEP) {453 return true; // all expected keys matched454 }455 }456 continue;457 }458 }459 if (type != Match.Type.CONTAINS_ANY && type != Match.Type.CONTAINS_ANY_DEEP) {460 return fail("actual does not contain key - '" + key + "'");461 }462 }463 Match.Value childActValue = new Match.Value(actMap.get(key));464 Match.Type childMatchType;465 if (type == Match.Type.CONTAINS_DEEP) {466 childMatchType = childActValue.isMapOrListOrXml() ? Match.Type.CONTAINS_DEEP : Match.Type.EQUALS;467 } else {468 childMatchType = Match.Type.EQUALS;469 }470 MatchOperation mo = new MatchOperation(context.descend(key), childMatchType, childActValue, new Match.Value(childExp));471 mo.execute();472 if (mo.pass) {473 if (type == Match.Type.CONTAINS_ANY || type == Match.Type.CONTAINS_ANY_DEEP) {474 return true; // exit early475 }476 unMatchedKeysExp.remove(key);477 if (unMatchedKeysExp.isEmpty()) {478 if (type == Match.Type.CONTAINS || type == Match.Type.CONTAINS_DEEP) {479 return true; // all expected keys matched480 }481 }482 unMatchedKeysAct.remove(key);483 } else if (type == Match.Type.EQUALS) {484 return fail("match failed for name: '" + key + "'");485 }486 }487 if (type == Match.Type.CONTAINS_ANY || type == Match.Type.CONTAINS_ANY_DEEP) {488 return unMatchedKeysExp.isEmpty() ? true : fail("no key-values matched");489 }490 if (unMatchedKeysExp.isEmpty()) { 491 if (type == Match.Type.CONTAINS || type == Match.Type.CONTAINS_DEEP) {492 return true; // all expected keys matched, expMap was empty in the first place 493 }494 if (type == Match.Type.NOT_CONTAINS && !expMap.isEmpty()) {495 return true; // hack alert: the NOT_CONTAINS will be reversed by the calling routine496 }497 }498 if (!unMatchedKeysExp.isEmpty()) {499 return fail("all key-values did not match, expected has un-matched keys - " + unMatchedKeysExp);500 }501 if (!unMatchedKeysAct.isEmpty()) {502 return fail("all key-values did not match, actual has un-matched keys - " + unMatchedKeysAct);503 }504 return true;505 }506 private boolean actualContainsExpected() {507 switch (actual.type) {508 case STRING:509 String actString = actual.getValue();510 String expString = expected.getValue();511 return actString.contains(expString);512 case LIST:513 List actList = actual.getValue();514 List expList = expected.getValue();515 int actListCount = actList.size();516 int expListCount = expList.size();517 if (type != Match.Type.CONTAINS_ANY && type != Match.Type.CONTAINS_ANY_DEEP && expListCount > actListCount) {518 return fail("actual array length is less than expected - " + actListCount + ":" + expListCount);519 }520 if (type == Match.Type.CONTAINS_ONLY && expListCount != actListCount) {...

Full Screen

Full Screen

actualContainsExpected

Using AI Code Generation

copy

Full Screen

1MatchOperation actualContainsExpected = new MatchOperation('actualContainsExpected', { actual, expected ->2 actual.contains(expected)3})4def match = actualContainsExpected(actual, expected)5def match = actualContainsExpected(actual, expected)6MatchOperation actualContainsExpected = new MatchOperation('actualContainsExpected', { actual, expected ->7 actual.contains(expected)8})9def match = actualContainsExpected(actual, expected)10def match = actualContainsExpected(actual, expected)11MatchOperation actualContainsExpected = new MatchOperation('actualContainsExpected', { actual, expected ->12 actual.contains(expected)13})14def match = actualContainsExpected(actual, expected)15def match = actualContainsExpected(actual, expected)16MatchOperation actualContainsExpected = new MatchOperation('actualContainsExpected', { actual, expected ->17 actual.contains(expected)18})19def match = actualContainsExpected(actual, expected)20def match = actualContainsExpected(actual, expected)21MatchOperation actualContainsExpected = new MatchOperation('actualContainsExpected', { actual, expected

Full Screen

Full Screen

actualContainsExpected

Using AI Code Generation

copy

Full Screen

1 * def actual = {id: 1, name: 'John', address: {city: 'New York', state: 'NY'}}2 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY'}}3 * match actualContainsExpected(actual, expected)4 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}5 * match actualContainsExpected(actual, expected)6 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}7 * match actualContainsExpected(actual, expected)8 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}9 * match actualContainsExpected(actual, expected)10 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}11 * match actualContainsExpected(actual, expected)12 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}13 * match actualContainsExpected(actual, expected)14 * def expected = {id: 1, name: 'John', address: {city: 'New York', state: 'NY', zip: '12345'}}

Full Screen

Full Screen

actualContainsExpected

Using AI Code Generation

copy

Full Screen

1def actualContainsExpected(actual, expected) {2 if (actual instanceof List && !(expected instanceof List)) {3 return actual.contains(expected)4 }5}6def actualContainsExpected(actual, expected) {7 if (actual instanceof List && !(expected instanceof List)) {8 return actual.contains(expected)9 }10}11def actualContainsExpected(actual, expected) {12 if (actual instanceof List && !(expected instanceof List)) {13 return actual.contains(expected)14 }15}16def actualContainsExpected(actual, expected) {17 if (actual instanceof List && !(expected instanceof List)) {18 return actual.contains(expected)19 }20}21def actualContainsExpected(actual, expected) {22 if (actual instanceof List && !(expected instanceof List)) {23 return actual.contains(expected)24 }25}26def actualContainsExpected(actual, expected) {27 if (actual instanceof List && !(expected instanceof List)) {28 return actual.contains(expected)29 }30}31def actualContainsExpected(actual, expected)

Full Screen

Full Screen

actualContainsExpected

Using AI Code Generation

copy

Full Screen

1* match actualContainsExpected(actual, expected) == true2* match actualContainsExpected(actual, expected) == false3* match actualContainsExpected(actual, 'WORLD', true) == true4* match actualContainsExpected(actual, 'FOO', true) == false5* match actualContainsExpected(actual, expected, false) == true6* match actualContainsExpected(actual, expected, false) == false7* match actualContainsExpected(actual, 'WORLD', true) == true8* match actualContainsExpected(actual, 'FOO', true) == false9* match actualContainsExpected(expected, actual) == true10* match actualContainsExpected(expected, actual) == false11* match actualContainsExpected('WORLD', actual, true) == true12* match actualContainsExpected('

Full Screen

Full Screen

actualContainsExpected

Using AI Code Generation

copy

Full Screen

1* def response = { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" }2* match response contains { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" }3* match response contains { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" } actualContainsExpected4* def response = { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" }5* match response contains { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" }6* match response contains { "id": "12345", "name": "John Smith", "age": 35, "address": "123 Main St", "city": "New York", "state": "NY", "zip": "10021" } actualContainsExpected

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