Best Karate code snippet using com.intuit.karate.Match.containsOnly
Source:MatchTest.java
...104 void testJavaSet() {105 Set<String> set = new HashSet();106 set.add("foo");107 set.add("bar");108 Match.that(set).containsOnly("['foo', 'bar']");109 }110 @Test111 void testJavaArray() {112 String[] strArray = new String[] {"foo", "bar"};113 Match.that(strArray).containsOnly("['foo', 'bar']");114 int[] intArray = new int[] {1, 2, 3};115 Match.that(intArray).isEqualTo("[1, 2, 3]");116 }117 @Test118 void testNotEquals() {119 match("[1, 2]", NOT_EQUALS, "#[1]");120 match("[1, 2]", NOT_EQUALS, "#[]? _ > 2");121 log();122 }123 @Test124 void testList() {125 match("[1, 2, 3]", EQUALS, "[1, 2, 3]");126 match("[1, 2, 3]", NOT_EQUALS, "[1, 2, 4]");127 match("[1, 2]", EQUALS, "[1, 2, 4]", FAILS);128 match("[1, 2, 3]", CONTAINS, "[1, 2, 3]");129 match("[1, 2, 3]", CONTAINS_ONLY, "[1, 2, 3]");130 match("[1, 2, 3]", CONTAINS_ONLY, "[3, 2, 1]");131 match("[1, 5, 10]", CONTAINS_ONLY, "[10, 5, 1]");132 match("[4, 2]", CONTAINS_ONLY, "[2, 4]");133 match("[5]", CONTAINS_ONLY, "[5]");134 match("[4, 4]", CONTAINS_ONLY, "[4, 4]");135 match("[1, 2, 2]", CONTAINS_ONLY, "[2, 2, 1]");136 match("[1, 2, 3]", CONTAINS_ONLY, "[2, 2, 3]", FAILS);137 match("[2, 2, 3]", CONTAINS_ONLY, "[1, 2, 3]", FAILS);138 match("[1, 4, 7]", CONTAINS_ONLY, "[4, 7]", FAILS);139 match("[1, 2, 3]", CONTAINS, "[1, 2, 4]", FAILS);140 match("[1, 2, 3]", NOT_CONTAINS, "[1, 2, 4]");141 match("[1, 2, 3]", CONTAINS_ANY, "[1, 2, 4]");142 match("[1, 2, 3]", CONTAINS_ANY, "[1, 2, 4, 5]");143 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", EQUALS, "[{ a: 1 }, { b: 2 }, { c: 3 }]");144 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", EQUALS, "[{ a: 1 }, { b: 2 }, { c: 4 }]", FAILS);145 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS, "[{ a: 1 }, { b: 2 }, { c: 3 }]");146 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS_ONLY, "[{ a: 1 }, { b: 2 }, { c: 3 }]");147 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS, "[{ a: 1 }, { c: 3 }]");148 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS_ANY, "[{ a: 9 }, { c: 3 }]");149 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS_ANY, "[{ a: 9 }, { c: 9 }]", FAILS);150 match("[{ a: 1 }, { b: 2 }, { c: 3 }]", CONTAINS_DEEP, "[{ a: 1 }, { c: 3 }]");151 match("[{ a: 1 }, { b: [1, 2, 3] }]", CONTAINS_DEEP, "[{ b: [2] }]");152 match("{ a: { foo: 'bar' } }", CONTAINS_DEEP, "{ a: '#object' }");153 }154 @ParameterizedTest155 @ValueSource(strings = {"CONTAINS", "CONTAINS_DEEP"})156 void testListContains(String containsType) {157 match("['foo', 'bar']", containsType, "baz", FAILS);158 message("actual array does not contain expected item - baz");159 match("['foo', 'bar']", containsType, "['baz']", FAILS);160 message("actual array does not contain expected item - baz");161 }162 @Test163 void testListContainsRegex() {164 match("['foo', 'bar']", CONTAINS, "#regex .{3}");165 match("['foo', 'bar']", CONTAINS_DEEP, "#regex .{3}");166 match("['foo', 'bar']", CONTAINS_ANY, "#regex .{3}");167 match("['foo', 'bar']", CONTAINS_ANY_DEEP, "#regex .{3}");168 match("{ array: ['foo', 'bar'] }", EQUALS, "{ array: '#[] #regex .{3}' }");169 match("{ array: ['foo', 'bar'] }", CONTAINS, "{ array: '#[] #regex .{3}' }");170 match("{ array: ['foo', 'bar'] }", CONTAINS_DEEP, "{ array: '#[] #regex .{3}' }");171 match("{ array: ['foo', 'bar'] }", CONTAINS_DEEP, "{ array: '#array' }");172 match("{ array: ['foo', 'bar'] }", CONTAINS_ANY, "{ array: '#[] #regex .{3}' }");173 match("{ array: ['foo', 'bar'] }", CONTAINS_ANY_DEEP, "{ array: '#[] #regex .{3}' }");174 }175 @Test176 void testListNotContains() {177 match("['foo', 'bar']", NOT_CONTAINS, "baz");178 match("['foo', 'bar']", NOT_CONTAINS, "bar", FAILS);179 message("actual contains expected");180 match(181 "[{ foo: 1 }, { foo: 2 }, { foo: 3 }]",182 CONTAINS,183 "[{ foo: 0 }, { foo: 2 }, { foo: 3 }]",184 FAILS);185 message("$[0] | not equal"); // TODO improve error message for this case186 }187 @Test188 void testEach() {189 match("[1, 2, 3]", EACH_EQUALS, "#number");190 match("[1, 2, 3]", EACH_EQUALS, "#number? _ > 0");191 match("[1, 2, 3]", EACH_EQUALS, "#number? _ < 2", FAILS);192 message("match each failed at index 1");193 match("[1, 'a', 3]", EACH_EQUALS, "#number", FAILS);194 message("$[1] | not a number");195 match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "#object");196 match("[{ a: 1 }, { a: 2 }]", EACH_EQUALS, "{ a: '#number' }");197 }198 @Test199 void testEachWithMagicVariables() {200 match("[{a: 1, b: 2}, {a: 2, b: 4}]", EACH_EQUALS, "{ a: '#number', b: '#(_$.a * 2)' }");201 match("[{a: 1, b: 2}, {a: 2, b: 4}]", EACH_EQUALS, "{ a: '#number', b: '#? _ == _$.a * 2' }");202 match("[{a: 1, b: 2}, {a: 2, b: 4}]", EACH_CONTAINS, "{ b: '#(_$.a * 2)' }");203 match("[{a: 1, b: 2}, {a: 2, b: 4}]", EACH_CONTAINS, "{ b: '#? _ == _$.a * 2' }");204 }205 @ParameterizedTest206 @ValueSource(strings = {"EQUALS", "CONTAINS", "CONTAINS_DEEP"})207 void testArray(String matchType) {208 match("[{ a: 1 }, { a: 2 }]", matchType, "#[2]");209 match("[{ a: 1 }, { a: 2 }]", matchType, "#[] #object");210 }211 @ParameterizedTest212 @CsvSource(value = {213 "EQUALS;EACH_EQUALS",214 "CONTAINS;EACH_CONTAINS",215 "CONTAINS_DEEP;EACH_CONTAINS_DEEP"}, delimiter = ';')216 void testSchema(String matchType, String matchEachType) {217 Json json = Json.of("{ a: '#number' }");218 Map map = json.asMap();219 match("[{ a: 1 }, { a: 2 }]", matchEachType, map);220 JsEngine.global().put("schema", map);221 match("[{ a: 1 }, { a: 2 }]", matchType, "#[] schema");222 match("{ a: 'x', b: { c: 'y' } }", matchType, "{ a: '#string', b: { c: '#string' } }");223 }224 @ParameterizedTest225 @ValueSource(strings = {"EQUALS", "CONTAINS", "CONTAINS_DEEP"})226 void testSchemaOptionalObject(String matchType) {227 Json part = Json.of("{ bar: '#string' }");228 JsEngine.global().put("part", part.asMap());229 match("{ foo: null }", matchType, "{ foo: '##(bar)' }");230 }231 @Test232 void testMap() {233 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: 3, a: 1 }");234 match("{ a: 1, b: 2, c: 3 }", CONTAINS, "{ b: 2, c: 3, a: 1 }");235 match("{ a: 1, b: 2, c: 3 }", CONTAINS_ONLY, "{ b: 2, c: 3, a: 1 }");236 match("{ a: 1, b: 2, c: 3 }", CONTAINS_DEEP, "{ c: 3, a: 1 }");237 match("{ a: 1, b: 2, c: [1, 2] }", CONTAINS_DEEP, "{ a: 1, c: [2] }");238 match("{ a: 1, b: 2, c: 3 }", CONTAINS, "{ b: 2 }");239 match("{ a: 1, b: 2, c: 3 }", CONTAINS, "{ }");240 match("{ a: 1, b: 2, c: 3 }", CONTAINS_ANY, "{ }");241 match("{ a: 1, b: 2, c: 3 }", CONTAINS_DEEP, "{ }");242 match("{ a: 1, b: 2, c: 3 }", CONTAINS_ANY, "{ z: 9, b: 2 }");243 match("{ a: 1, b: 2, c: 3 }", CONTAINS, "{ z: 9, x: 2 }", FAILS);244 message("$ | actual does not contain expected | actual does not contain key - 'z'");245 match("{ a: 1, b: 2, c: 3 }", CONTAINS_ANY, "{ z: 9, x: 2 }", FAILS);246 message("$ | actual does not contain expected | no key-values matched");247 message("$.x | data types don't match");248 message("$.z | data types don't match");249 match("{ a: 1, b: 2, c: 3 }", NOT_CONTAINS, "{ a: 1 }", FAILS);250 message("$ | actual contains expected");251 match("{ a: 1, b: 2, c: 3 }", NOT_CONTAINS, "{}");252 }253 @Test254 void testJsonFailureMessages() {255 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ a: 1, b: 9, c: 3 }", FAILS);256 message("$.b | not equal");257 match("{ a: { b: { c: 1 } } }", EQUALS, "{ a: { b: { c: 2 } } }", FAILS);258 message("$.a.b.c | not equal");259 }260 @Test261 void testXmlFailureMessages() {262 match("<a><b><c>1</c></b></a>", EQUALS, "<a><b><c>2</c></b></a>", FAILS);263 message("/ | not equal | match failed for name: 'a'");264 message("/a | not equal | match failed for name: 'b'");265 message("/a/b | not equal | match failed for name: 'c'");266 message("/a/b/c | not equal");267 match("<hello foo=\"bar\">world</hello>", EQUALS, "<hello foo=\"baz\">world</hello>", FAILS);268 message("/ | not equal | match failed for name: 'hello'");269 message("/hello/@foo | not equal");270 }271 @Test272 void testMapFuzzyIgnores() {273 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: 3, z: '#ignore', a: 1 }");274 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: 3, z: '#notpresent', a: 1 }");275 match(276 "{ a: 1, b: 2, c: 3 }",277 EQUALS,278 "{ b: 2, c: 3, z: '##anything', a: 1 }"); // not really correct, TODO !279 }280 @Test281 void testMapFuzzy() {282 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#number', a: 1 }");283 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#present', a: 1 }");284 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#notnull', a: 1 }");285 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#null', a: 1 }", FAILS);286 message("$.c | not null");287 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#string', a: 1 }", FAILS);288 message("$.c | not a string");289 match("{ a: 1, b: 2, c: 3 }", EQUALS, "{ b: 2, c: '#notpresent', a: 1 }", FAILS);290 message("$.c | present");291 match("{ a: 1, b: 'foo', c: 2 }", EQUALS, "{ b: '#regex foo', c: 2, a: 1 }");292 match("{ a: 1, b: 'foo', c: 2 }", EQUALS, "{ b: '#regex .+', c: 2, a: 1 }");293 match("{ a: 1, b: 'foo', c: 2 }", EQUALS, "{ b: '#regex .{3}', c: 2, a: 1 }");294 match("{ a: 1, b: 'foo', c: 2 }", EQUALS, "{ b: '#regex .{2}', c: 2, a: 1 }", FAILS);295 message("$.b | regex match failed");296 }297 @Test298 void testNotPresentOnLhs() {299 match("#notpresent", EQUALS, 2, FAILS);300 message("actual path does not exist");301 match("#notpresent", EQUALS, "foo", FAILS);302 message("actual path does not exist");303 }304 @Test305 void testXml() {306 match("<root>foo</root>", EQUALS, "<root>foo</root>");307 match("<root>foo</root>", CONTAINS, "<root>foo</root>");308 match("<root>foo</root>", EQUALS, "<root>bar</root>", FAILS);309 match("<root>foo</root>", CONTAINS, "<root>bar</root>", FAILS);310 match("<root><a>1</a><b>2</b></root>", EQUALS, "<root><a>1</a><b>2</b></root>");311 match("<root><a>1</a><b>2</b></root>", EQUALS, "<root><b>2</b><a>1</a></root>");312 match("<root><a>1</a><b>2</b></root>", CONTAINS, "<root><b>2</b><a>1</a></root>");313 match("<root><a>1</a><b>2</b></root>", CONTAINS, "<root><a>1</a><b>9</b></root>", FAILS);314 }315 @ParameterizedTest316 @ValueSource(strings = {"EQUALS", "CONTAINS", "CONTAINS_DEEP"})317 void testXmlSchema(String matchType) {318 match("<root></root>", matchType, "<root>#null</root>"); // TODO controversial319 match("<root></root>", matchType, "<root>#present</root>");320 match(321 "<root><a>x</a><b><c>y</c></b></root>",322 matchType,323 "<root><a>#string</a><b><c>#string</c></b></root>");324 match(325 "<root><a>x</a><b></b></root>",326 matchType,327 "<root><a>#string</a><b><c>#string</c></b></root>",328 FAILS);329 match(330 "<root><a>x</a><b><c></c></b></root>",331 matchType,332 "<root><a>#string</a><b><c>#string</c></b></root>",333 FAILS);334 match(335 "<root><a>x</a><b><c>y</c></b></root>",336 matchType,337 "<root><a>#string</a><b><c>#string</c></b></root>");338 }339 @Test340 void testApiUsage() {341 Match.that("[1, 2, 3]").contains(2);342 Match.that("[1, 2, 3]").isEachEqualTo("#number");343 Match.that("[1, 2, 3]").containsOnly("[3, 2, 1]");344 Match.that("{ a: 1, b: 2 }").contains("{ b: 2 }");345 Match.that("{ a: 1, b: 2, c: { d: 3, e: 4} }").containsDeep("{ b: 2, c: { e: 4 } }");346 }347 @ParameterizedTest348 @ValueSource(strings = {"EQUALS", "CONTAINS", "CONTAINS_DEEP"})349 void testRegex(String matchType) {350 match(351 "{ number: '/en/search?q=test' }",352 matchType,353 "{ number: '#regex /\\\\w{2}/search\\\\?q=(.*)+' }");354 match(355 "{ number: '/us/search?q=test' }",356 matchType,357 "{ number: '#regex /\\\\w{2}/search\\\\?q=(.*)+' }");...
containsOnly
Using AI Code Generation
1def json1 = read('classpath:json1.json')2def json2 = read('classpath:json2.json')3def json3 = read('classpath:json3.json')4def json4 = read('classpath:json4.json')5def json5 = read('classpath:json5.json')6def json6 = read('classpath:json6.json')7def json1 = read('classpath:json1.json')8def json2 = read('classpath:json2.json')9def json3 = read('classpath:json3.json')10def json4 = read('classpath:json4.json')11def json5 = read('classpath:json5.json')12def json6 = read('classpath:json6.json')13def json1 = read('classpath:json1.json')14def json2 = read('classpath:json2.json')15def json3 = read('classpath:json3.json')16def json4 = read('classpath:json4.json')17def json5 = read('classpath:json5.json')18def json6 = read('classpath:json6.json')19def json1 = read('classpath:json1.json')20def json2 = read('classpath:json2.json')21def json3 = read('classpath:json3.json')22def json4 = read('classpath:json4.json')23def json5 = read('classpath:json5.json')24def json6 = read('classpath:json6.json')25def json1 = read('classpath:json1.json')26def json2 = read('classpath:json2.json')27def json3 = read('classpath:json3.json')28def json4 = read('classpath:json4.json')29def json5 = read('classpath:json5.json')30def json6 = read('classpath:json6.json')31def json1 = read('classpath:json1.json')32def json2 = read('classpath:json2.json')33def json3 = read('classpath:json3.json')34def json4 = read('classpath:json4.json')35def json5 = read('classpath:json5.json')36def json6 = read('classpath:json6.json')37def json1 = read('classpath:json1.json')38def json2 = read('classpath:json2.json')39def json3 = read('classpath:json3.json')40def json4 = read('classpath:json4.json')41def json5 = read('classpath:json5.json')42def json6 = read('
containsOnly
Using AI Code Generation
1* def actual = { foo: 'bar', baz: 123 }2* def expected = { foo: '#string', baz: '#number' }3* match actual containsOnly { foo: '#string', baz: '#number', extra: '#string' }4* def actual = { foo: 'bar', baz: 123 }5* def expected = { foo: '#string', baz: '#number', extra: '#string' }6* def actual = { foo: 'bar', baz: 123 }7* def expected = { foo: '#string', baz: '#number', extra: '#string' }8* match actual containsOnly {}9* def actual = { foo: 'bar', baz: 123 }10* def expected = { foo: '#string', baz: '#number', extra: '#string' }11* def actual = { foo: 'bar', baz: 123 }12* def expected = { foo: '#string', baz: '#number', extra: '#string' }13* def actual = { foo: 'bar', baz: 123 }14* def expected = { foo: '#string', baz: '#number', extra: '#string' }15* def actual = { foo: 'bar', baz: 123 }16* def expected = { foo: '#string', baz: '#number', extra: '#string' }17* def actual = { foo: 'bar', baz: 123 }18* def expected = { foo: '#string', baz: '#number', extra: '#string' }19* def actual = { foo: 'bar', baz: 123 }20* def expected = { foo: '#string', baz: '#number', extra: '#string' }21* def actual = {
containsOnly
Using AI Code Generation
1def json = read('classpath:com/intuit/karate/demo/demo.json')2match json containsOnly { name: 'John', age: 30 }3match json containsOnly { name: 'John', age: '#number' }4match json containsOnly { name: '#string', age: '#number' }5match json containsOnly { name: '#string', age: '#number', '#any': '#string' }6match json containsOnly { name: '#string', age: '#number', '#any': '#string', '#any': '#string' }7match json containsOnly { name: '#string', age: '#number', '#any': '#string', '#any': '#string', '#any': '#string' }8match json containsOnly { name: '#string', age: '#number', '#any': '#string', '#any': '#string', '#any': '#string', '#any': '#string' }9match json containsOnly { name: '#string', age: '#number', '#any': '#string', '#any': '#string', '#any': '#string', '#any': '#string', '#any': '#string' }10match json containsOnly { na
containsOnly
Using AI Code Generation
1* def response = { "id": 1, "name": "John", "age": 25, "address": { "street": "1st Street", "city": "New York", "zip": 10001 } }2* match response containsOnly { "id": 1, "name": "John", "age": 25, "address": { "city": "New York", "zip": 10001 } }3* match response containsOnly { "id": 1, "name": "John", "address": { "city": "New York" } }4* match response containsOnly { "id": 1, "name": "John", "address": { "zip": 10001 } }5* def response = { "id": 1, "name": "John", "age": 25, "address": { "street": "1st Street", "city": "New York", "zip": 10001 } }6* match com.intuit.karate.JsonUtils.containsOnly(response, { "id": 1, "name": "John", "age": 25, "address": { "city": "New York", "zip": 10001 } })7* match com.intuit.karate.JsonUtils.containsOnly(response, { "id": 1, "name": "John", "address": { "city": "New York" } })8* match com.intuit.karate.JsonUtils.containsOnly(response, { "id": 1, "name": "John", "address": { "zip": 10001 } })9* def response = { "id": 1, "name": "John", "age": 25, "address": { "street": "1st Street", "city": "New York", "zip": 10001 } }10* match org.skyscreamer.jsonassert.JSONAssert.assertEquals(response, { "id": 1, "name": "John", "age": 25, "address": { "city": "New York", "zip": 10001 } }, false)11* match org.skyscreamer.jsonassert.JSONAssert.assertEquals(response, { "id": 1, "
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!