Best Kotest code snippet using io.kotest.matchers.collections.matchers.Array.shouldExistInOrder
TraceIteratorTest.kt
Source:TraceIteratorTest.kt
1/*2 * This file is part of OMJ.3 *4 * OMJ is free software: you can redistribute it and/or modify5 * it under the terms of the GNU General Public License as published by6 * the Free Software Foundation, either version 3 of the License, or7 * (at your option) any later version.8 *9 * OMJ is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12 * GNU General Public License for more details.13 *14 * You should have received a copy of the GNU General Public License15 * along with OMJ. If not, see <https://www.gnu.org/licenses/>.16 */17package com.octogonapus.omj.ui.model18import com.octogonapus.omj.testutil.CompileUtil19import io.kotest.assertions.throwables.shouldThrow20import io.kotest.matchers.collections.shouldBeStrictlyIncreasingWith21import io.kotest.matchers.collections.shouldExist22import io.kotest.matchers.collections.shouldExistInOrder23import io.kotest.matchers.collections.shouldHaveSize24import mu.KotlinLogging25import org.junit.jupiter.api.Nested26import org.junit.jupiter.api.Test27import org.junit.jupiter.api.io.TempDir28import java.io.BufferedInputStream29import java.io.File30import java.io.FileInputStream31internal class TraceIteratorTest {32 @Nested33 inner class MethodTraceTests {34 @Test35 fun `parse method call with no args`(@TempDir tempDir: File) {36 val traces = generateTraces(tempDir, "agent-test_noargs.jar")37 traces.shouldExistInOrder(38 {39 it.constructorCall(40 receiverType = "com.agenttest.noargs.Foo",41 callerClass = "com.agenttest.noargs.Main"42 )43 },44 {45 it.virtualMethodCall(46 receiverType = "com.agenttest.noargs.Foo",47 callerClass = "com.agenttest.noargs.Main",48 methodName = "with"49 )50 }51 )52 }53 @Test54 fun `parse method call with args byte 3C`(@TempDir tempDir: File) {55 val traces = generateTraces(tempDir, "agent-test_byte3c.jar")56 traces.shouldExistInOrder(57 {58 it.constructorCall(59 receiverType = "com.agenttest.byte3c.Foo",60 callerClass = "com.agenttest.byte3c.Main"61 )62 },63 {64 it.virtualMethodCall(65 receiverType = "com.agenttest.byte3c.Foo",66 methodName = "with",67 callerClass = "com.agenttest.byte3c.Main",68 args = listOf("byte" to "60")69 )70 }71 )72 }73 @Test74 fun `parse method call with args char Q`(@TempDir tempDir: File) {75 val traces = generateTraces(tempDir, "agent-test_charQ.jar")76 traces.shouldExistInOrder(77 {78 it.constructorCall(79 receiverType = "com.agenttest.charQ.Foo",80 callerClass = "com.agenttest.charQ.Main"81 )82 },83 {84 it.virtualMethodCall(85 receiverType = "com.agenttest.charQ.Foo",86 methodName = "with",87 callerClass = "com.agenttest.charQ.Main",88 args = listOf("char" to "Q")89 )90 }91 )92 }93 @Test94 fun `parse method call with args double 1p2`(@TempDir tempDir: File) {95 val traces = generateTraces(tempDir, "agent-test_double1p2.jar")96 traces.shouldExistInOrder(97 {98 it.constructorCall(99 receiverType = "com.agenttest.double1p2.Foo",100 callerClass = "com.agenttest.double1p2.Main"101 )102 },103 {104 it.virtualMethodCall(105 receiverType = "com.agenttest.double1p2.Foo",106 methodName = "with",107 callerClass = "com.agenttest.double1p2.Main",108 args = listOf("double" to "1.2")109 )110 }111 )112 }113 @Test114 fun `parse method call with args float 4p3`(@TempDir tempDir: File) {115 val traces = generateTraces(tempDir, "agent-test_float4p3.jar")116 traces.shouldExistInOrder(117 {118 it.constructorCall(119 receiverType = "com.agenttest.float4p3.Foo",120 callerClass = "com.agenttest.float4p3.Main"121 )122 },123 {124 it.virtualMethodCall(125 receiverType = "com.agenttest.float4p3.Foo",126 methodName = "with",127 callerClass = "com.agenttest.float4p3.Main",128 args = listOf("float" to "4.3")129 )130 }131 )132 }133 @Test134 fun `parse method call with args int 42`(@TempDir tempDir: File) {135 val traces = generateTraces(tempDir, "agent-test_int42.jar")136 traces.shouldExistInOrder(137 {138 it.constructorCall(139 receiverType = "com.agenttest.int42.Foo",140 callerClass = "com.agenttest.int42.Main"141 )142 },143 {144 it.virtualMethodCall(145 receiverType = "com.agenttest.int42.Foo",146 methodName = "with",147 callerClass = "com.agenttest.int42.Main",148 args = listOf("int" to "42")149 )150 }151 )152 }153 @Test154 fun `parse method call with args long 123456789123456789`(@TempDir tempDir: File) {155 val traces = generateTraces(tempDir, "agent-test_long123456789123456789.jar")156 traces.shouldExistInOrder(157 {158 it.constructorCall(159 receiverType = "com.agenttest.long123456789123456789.Foo",160 callerClass = "com.agenttest.long123456789123456789.Main"161 )162 },163 {164 it.virtualMethodCall(165 receiverType = "com.agenttest.long123456789123456789.Foo",166 methodName = "with",167 callerClass = "com.agenttest.long123456789123456789.Main",168 args = listOf("long" to "123456789123456789")169 )170 }171 )172 }173 @Test174 fun `parse method call with args string hello`(@TempDir tempDir: File) {175 val traces = generateTraces(tempDir, "agent-test_stringHello.jar")176 traces.shouldExistInOrder(177 {178 it.constructorCall(179 receiverType = "com.agenttest.stringHello.Foo",180 callerClass = "com.agenttest.stringHello.Main"181 )182 },183 {184 it.virtualMethodCall(185 receiverType = "com.agenttest.stringHello.Foo",186 methodName = "with",187 callerClass = "com.agenttest.stringHello.Main",188 args = listOf("java.lang.String" to "Hello")189 )190 }191 )192 }193 @Test194 fun `parse method call with args string hello with null byte`(@TempDir tempDir: File) {195 val traces = generateTraces(tempDir, "agent-test_stringHelloNull1.jar")196 traces.shouldExistInOrder(197 {198 it.constructorCall(199 receiverType = "com.agenttest.stringHelloNull1.Foo",200 callerClass = "com.agenttest.stringHelloNull1.Main"201 )202 },203 {204 it.virtualMethodCall(205 receiverType = "com.agenttest.stringHelloNull1.Foo",206 methodName = "with",207 callerClass = "com.agenttest.stringHelloNull1.Main",208 args = listOf("java.lang.String" to "Hello\u0000 1")209 )210 }211 )212 }213 @Test214 fun `parse method call with args object`(@TempDir tempDir: File) {215 val traces = generateTraces(tempDir, "agent-test_objectStringArray.jar")216 traces.shouldExistInOrder(217 {218 it.constructorCall(219 receiverType = "com.agenttest.objectStringArray.Foo",220 callerClass = "com.agenttest.objectStringArray.Main"221 )222 },223 {224 it.virtualMethodCall(225 receiverType = "com.agenttest.objectStringArray.Foo",226 methodName = "with",227 callerClass = "com.agenttest.objectStringArray.Main",228 args = listOf("[Ljava.lang.String;" to null)229 )230 }231 )232 }233 @Test234 fun `parse method call with args short 12345`(@TempDir tempDir: File) {235 val traces = generateTraces(tempDir, "agent-test_short12345.jar")236 traces.shouldExistInOrder(237 {238 it.constructorCall(239 receiverType = "com.agenttest.short12345.Foo",240 callerClass = "com.agenttest.short12345.Main"241 )242 },243 {244 it.virtualMethodCall(245 receiverType = "com.agenttest.short12345.Foo",246 methodName = "with",247 callerClass = "com.agenttest.short12345.Main",248 args = listOf("short" to "12345")249 )250 }251 )252 }253 @Test254 fun `parse method call with args boolean true`(@TempDir tempDir: File) {255 val traces = generateTraces(tempDir, "agent-test_booleanTrue.jar")256 traces.shouldExistInOrder(257 {258 it.constructorCall(259 receiverType = "com.agenttest.booleanTrue.Foo",260 callerClass = "com.agenttest.booleanTrue.Main"261 )262 },263 {264 it.virtualMethodCall(265 receiverType = "com.agenttest.booleanTrue.Foo",266 methodName = "with",267 callerClass = "com.agenttest.booleanTrue.Main",268 args = listOf("boolean" to "true")269 )270 }271 )272 }273 @Test274 fun `parse method call with args object MyDataClass`(@TempDir tempDir: File) {275 val traces = generateTraces(tempDir, "agent-test_objectTestDataClass.jar")276 traces.shouldExistInOrder(277 {278 it.constructorCall(279 receiverType = "com.agenttest.objectTestDataClass.Foo",280 callerClass = "com.agenttest.objectTestDataClass.Main"281 )282 },283 {284 it.virtualMethodCall(285 receiverType = "com.agenttest.objectTestDataClass.Foo",286 methodName = "with",287 callerClass = "com.agenttest.objectTestDataClass.Main",288 args = listOf("com.agenttest.objectTestDataClass.TestDataClass" to null)289 )290 }291 )292 }293 @Test294 fun `parse constructor call with int 6`(@TempDir tempDir: File) {295 val traces = generateTraces(tempDir, "agent-test_constructorInt6.jar")296 traces.shouldExistInOrder(297 {298 it.constructorCall(299 receiverType = "com.agenttest.constructorInt6.Foo",300 callerClass = "com.agenttest.constructorInt6.Main",301 args = listOf("int" to "6")302 )303 }304 )305 }306 @Test307 fun `parse a static method call in a static block`(@TempDir tempDir: File) {308 val traces = generateTraces(tempDir, "agent-test_staticBlockCallStaticMethod.jar")309 traces.shouldExistInOrder(310 {311 it.staticMethodCall(312 methodName = "callMe",313 callerClass = "com.agenttest.staticBlockCallStaticMethod.Foo"314 )315 }316 )317 }318 @Test319 fun `parse a static method call with an int after a double`(@TempDir tempDir: File) {320 val traces = generateTraces(tempDir, "agent-test_methodCallIntAfterDouble.jar")321 traces.shouldExistInOrder(322 {323 it.staticMethodCall(324 methodName = "callMe",325 callerClass = "com.agenttest.methodCallIntAfterDouble.Main",326 args = listOf("double" to "4.2", "int" to "1")327 )328 }329 )330 }331 }332 @Nested333 inner class LocalVariableStoreTraceTests {334 @Test335 fun `test boolean store`(@TempDir tempDir: File) {336 val traces = generateTraces(tempDir, "agent-test_storeBoolean.jar")337 traces.shouldExist {338 it.storeVar("com.agenttest.storeBoolean.Main", "boolean", "b", "true")339 }340 }341 @Test342 fun `test byte store`(@TempDir tempDir: File) {343 val traces = generateTraces(tempDir, "agent-test_storeByte.jar")344 traces.shouldExist {345 it.storeVar("com.agenttest.storeByte.Main", "byte", "b", "250")346 }347 }348 @Test349 fun `test char store`(@TempDir tempDir: File) {350 val traces = generateTraces(tempDir, "agent-test_storeChar.jar")351 traces.shouldExist {352 it.storeVar("com.agenttest.storeChar.Main", "char", "c", "Q")353 }354 }355 @Test356 fun `test double store`(@TempDir tempDir: File) {357 val traces = generateTraces(tempDir, "agent-test_storeDouble.jar")358 traces.shouldExist {359 it.storeVar("com.agenttest.storeDouble.Main", "double", "d", "4.2")360 }361 }362 @Test363 fun `test float store`(@TempDir tempDir: File) {364 val traces = generateTraces(tempDir, "agent-test_storeFloat.jar")365 traces.shouldExist {366 it.storeVar("com.agenttest.storeFloat.Main", "float", "f", "2.3")367 }368 }369 @Test370 fun `test int store`(@TempDir tempDir: File) {371 val traces = generateTraces(tempDir, "agent-test_storeInt.jar")372 traces.shouldExist {373 it.storeVar("com.agenttest.storeInt.Main", "int", "i", "123456")374 }375 }376 @Test377 fun `test long store`(@TempDir tempDir: File) {378 val traces = generateTraces(tempDir, "agent-test_storeLong.jar")379 traces.shouldExist {380 it.storeVar("com.agenttest.storeLong.Main", "long", "l", "123456789123456789")381 }382 }383 @Test384 fun `test ref store`(@TempDir tempDir: File) {385 val traces = generateTraces(tempDir, "agent-test_storeRef.jar")386 traces.shouldExist {387 it.storeVar("com.agenttest.storeRef.Main", "java.lang.Object", "o", null)388 }389 }390 @Test391 fun `test short store`(@TempDir tempDir: File) {392 val traces = generateTraces(tempDir, "agent-test_storeShort.jar")393 traces.shouldExist {394 it.storeVar("com.agenttest.storeShort.Main", "short", "s", "12345")395 }396 }397 @Test398 fun `test string store`(@TempDir tempDir: File) {399 val traces = generateTraces(tempDir, "agent-test_storeString.jar")400 traces.shouldExist {401 it.storeVar("com.agenttest.storeString.Main", "java.lang.String", "s", "My String")402 }403 }404 @Test405 fun `test int increment`(@TempDir tempDir: File) {406 val traces = generateTraces(tempDir, "agent-test_storeIncrementInt.jar")407 traces.shouldExist {408 // Started at 3 and was incremented to 4409 it.storeVar("com.agenttest.storeIncrementInt.Main", "int", "i", "4")410 }411 }412 @Test413 fun `test int array store`(@TempDir tempDir: File) {414 val traces = generateTraces(tempDir, "agent-test_storeIntArray.jar")415 traces.shouldExistInOrder(416 {417 it.storeVar("com.agenttest.storeIntArray.Main", "[I", "i", null)418 },419 {420 it.storeArray(421 containingClass = "com.agenttest.storeIntArray.Main",422 varType = "int",423 arrayIndex = 0,424 value = "6"425 )426 }427 )428 }429 @Test430 fun `test int array store one-liner`(@TempDir tempDir: File) {431 val traces = generateTraces(tempDir, "agent-test_storeIntArrayOneLiner.jar")432 traces.shouldExistInOrder(433 {434 it.storeArray(435 containingClass = "com.agenttest.storeIntArrayOneLiner.Main",436 varType = "int",437 arrayIndex = 0,438 value = "6"439 )440 },441 {442 it.storeVar("com.agenttest.storeIntArrayOneLiner.Main", "[I", "i", null)443 }444 )445 }446 @Test447 fun `test double array store`(@TempDir tempDir: File) {448 val traces = generateTraces(tempDir, "agent-test_storeDoubleArray.jar")449 traces.shouldExistInOrder(450 {451 it.storeVar("com.agenttest.storeDoubleArray.Main", "[D", "d", null)452 },453 {454 it.storeArray(455 containingClass = "com.agenttest.storeDoubleArray.Main",456 varType = "double",457 arrayIndex = 0,458 value = "4.2"459 )460 }461 )462 }463 @Test464 fun `test double array store one-liner`(@TempDir tempDir: File) {465 val traces = generateTraces(tempDir, "agent-test_storeDoubleArrayOneLiner.jar")466 traces.shouldExistInOrder(467 {468 it.storeArray(469 containingClass = "com.agenttest.storeDoubleArrayOneLiner.Main",470 varType = "double",471 arrayIndex = 0,472 value = "4.2"473 )474 },475 {476 it.storeVar("com.agenttest.storeDoubleArrayOneLiner.Main", "[D", "d", null)477 }478 )479 }480 @Test481 fun `test object array store`(@TempDir tempDir: File) {482 val traces = generateTraces(tempDir, "agent-test_storeObjectArray.jar")483 traces.shouldExistInOrder(484 {485 it.storeVar(486 "com.agenttest.storeObjectArray.Main",487 "[Ljava.lang.Object;",488 "o",489 null490 )491 },492 {493 it.storeArray(494 containingClass = "com.agenttest.storeObjectArray.Main",495 varType = "java.lang.Object",496 arrayIndex = 0,497 value = null498 )499 }500 )501 }502 @Test503 fun `test object array store one-liner`(@TempDir tempDir: File) {504 val traces = generateTraces(tempDir, "agent-test_storeObjectArrayOneLiner.jar")505 traces.shouldExistInOrder(506 {507 it.storeArray(508 containingClass = "com.agenttest.storeObjectArrayOneLiner.Main",509 varType = "java.lang.Object",510 arrayIndex = 0,511 value = null512 )513 },514 {515 it.storeVar(516 "com.agenttest.storeObjectArrayOneLiner.Main",517 "[Ljava.lang.Object;",518 "o",519 null520 )521 }522 )523 }524 @Test525 fun `test string array store`(@TempDir tempDir: File) {526 val traces = generateTraces(tempDir, "agent-test_storeStringArray.jar")527 traces.shouldExistInOrder(528 {529 it.storeVar(530 "com.agenttest.storeStringArray.Main",531 "[Ljava.lang.String;",532 "o",533 null534 )535 },536 {537 it.storeArray(538 containingClass = "com.agenttest.storeStringArray.Main",539 varType = "java.lang.String",540 arrayIndex = 0,541 value = "Hello"542 )543 }544 )545 }546 @Test547 fun `test string array store one-liner`(@TempDir tempDir: File) {548 val traces = generateTraces(tempDir, "agent-test_storeStringArrayOneLiner.jar")549 traces.shouldExistInOrder(550 {551 it.storeArray(552 containingClass = "com.agenttest.storeStringArrayOneLiner.Main",553 varType = "java.lang.String",554 arrayIndex = 0,555 value = "Hello"556 )557 },558 {559 it.storeVar(560 "com.agenttest.storeStringArrayOneLiner.Main",561 "[Ljava.lang.String;",562 "o",563 null564 )565 }566 )567 }568 @Test569 fun `test 2D int array store`(@TempDir tempDir: File) {570 val traces = generateTraces(tempDir, "agent-test_storeMultiIntArray.jar")571 traces.shouldExistInOrder(572 {573 it.storeVar("com.agenttest.storeMultiIntArray.Main", "[[I", "i", null)574 },575 {576 it.storeArray(577 "com.agenttest.storeMultiIntArray.Main",578 "int",579 0,580 "6"581 )582 }583 )584 }585 @Test586 fun `test 3D int array store`(@TempDir tempDir: File) {587 val traces = generateTraces(tempDir, "agent-test_storeMultiIntArray3.jar")588 traces.shouldExistInOrder(589 {590 it.storeVar("com.agenttest.storeMultiIntArray3.Main", "[[[I", "i", null)591 },592 {593 it.storeArray(594 "com.agenttest.storeMultiIntArray3.Main",595 "int",596 0,597 "6"598 )599 }600 )601 }602 @Test603 fun `test boolean array store`(@TempDir tempDir: File) {604 val traces = generateTraces(tempDir, "agent-test_storeBooleanArray.jar")605 traces.shouldExistInOrder(606 {607 it.storeVar("com.agenttest.storeBooleanArray.Main", "[Z", "b", null)608 },609 {610 it.storeArray(611 containingClass = "com.agenttest.storeBooleanArray.Main",612 varType = "boolean",613 arrayIndex = 0,614 value = "true"615 )616 }617 )618 }619 @Test620 fun `test byte array store`(@TempDir tempDir: File) {621 val traces = generateTraces(tempDir, "agent-test_storeByteArray.jar")622 traces.shouldExistInOrder(623 {624 it.storeVar("com.agenttest.storeByteArray.Main", "[B", "b", null)625 },626 {627 it.storeArray(628 containingClass = "com.agenttest.storeByteArray.Main",629 varType = "byte",630 arrayIndex = 0,631 value = "250"632 )633 }634 )635 }636 @Test637 fun `test char array store`(@TempDir tempDir: File) {638 val traces = generateTraces(tempDir, "agent-test_storeCharArray.jar")639 traces.shouldExistInOrder(640 {641 it.storeVar("com.agenttest.storeCharArray.Main", "[C", "c", null)642 },643 {644 it.storeArray(645 containingClass = "com.agenttest.storeCharArray.Main",646 varType = "char",647 arrayIndex = 0,648 value = "Q"649 )650 }651 )652 }653 @Test654 fun `test float array store`(@TempDir tempDir: File) {655 val traces = generateTraces(tempDir, "agent-test_storeFloatArray.jar")656 traces.shouldExistInOrder(657 {658 it.storeVar("com.agenttest.storeFloatArray.Main", "[F", "f", null)659 },660 {661 it.storeArray(662 containingClass = "com.agenttest.storeFloatArray.Main",663 varType = "float",664 arrayIndex = 0,665 value = "2.3"666 )667 }668 )669 }670 @Test671 fun `test long array store`(@TempDir tempDir: File) {672 val traces = generateTraces(tempDir, "agent-test_storeLongArray.jar")673 traces.shouldExistInOrder(674 {675 it.storeVar("com.agenttest.storeLongArray.Main", "[J", "l", null)676 },677 {678 it.storeArray(679 containingClass = "com.agenttest.storeLongArray.Main",680 varType = "long",681 arrayIndex = 0,682 value = "123456789123456789"683 )684 }685 )686 }687 @Test688 fun `test short array store`(@TempDir tempDir: File) {689 val traces = generateTraces(tempDir, "agent-test_storeShortArray.jar")690 traces.shouldExistInOrder(691 {692 it.storeVar("com.agenttest.storeShortArray.Main", "[S", "s", null)693 },694 {695 it.storeArray(696 containingClass = "com.agenttest.storeShortArray.Main",697 varType = "short",698 arrayIndex = 0,699 value = "12345"700 )701 }702 )703 }704 @Test705 fun `test multithreaded int array store`(@TempDir tempDir: File) {706 val traces = generateTraces(tempDir, "agent-test_storeIntMultithreaded.jar")707 // One thread writes even numbers and the other writes odd numbers. Both use a lock so708 // all numbers should be traced.709 repeat(20) { number ->710 traces.shouldExist {711 it.storeVar(712 "com.agenttest.storeIntMultithreaded.Main",713 "int",714 "com.agenttest.storeIntMultithreaded.Main.i",715 "$number"716 )717 }718 }719 // All traces should be in order720 traces.shouldBeStrictlyIncreasingWith(Comparator { t1, t2 -> (t1.index - t2.index).toInt() })721 }722 }723 @Nested724 inner class FormalMethodParameterStoreTraceTests {725 @Test726 fun `mutate both method params`(@TempDir tempDir: File) {727 val traces = generateTraces(tempDir, "agent-test_storeTwoMethodParams.jar")728 traces.shouldExist {729 it.storeVar(730 "com.agenttest.storeTwoMethodParams.Main",731 "java.lang.String",732 "s",733 "Second"734 )735 it.storeVar("com.agenttest.storeTwoMethodParams.Main", "int", "i", "2")736 }737 }738 }739 @Nested740 inner class PutFieldTraceTests {741 @Test742 fun `test put boolean`(@TempDir tempDir: File) {743 val traces = generateTraces(tempDir, "agent-test_putBooleanField.jar")744 traces.shouldExist {745 it.storeVar(746 "com.agenttest.putBooleanField.Main",747 "boolean",748 "com.agenttest.putBooleanField.Main.b",749 "true"750 )751 }752 }753 @Test754 fun `test put byte`(@TempDir tempDir: File) {755 val traces = generateTraces(tempDir, "agent-test_putByteField.jar")756 traces.shouldExist {757 it.storeVar(758 "com.agenttest.putByteField.Main",759 "byte",760 "com.agenttest.putByteField.Main.b",761 "250"762 )763 }764 }765 @Test766 fun `test put char`(@TempDir tempDir: File) {767 val traces = generateTraces(tempDir, "agent-test_putCharField.jar")768 traces.shouldExist {769 it.storeVar(770 "com.agenttest.putCharField.Main",771 "char",772 "com.agenttest.putCharField.Main.c",773 "Q"774 )775 }776 }777 @Test778 fun `test put double`(@TempDir tempDir: File) {779 val traces = generateTraces(tempDir, "agent-test_putDoubleField.jar")780 traces.shouldExist {781 it.storeVar(782 "com.agenttest.putDoubleField.Main",783 "double",784 "com.agenttest.putDoubleField.Main.d",785 "4.2"786 )787 }788 }789 @Test790 fun `test put float`(@TempDir tempDir: File) {791 val traces = generateTraces(tempDir, "agent-test_putFloatField.jar")792 traces.shouldExist {793 it.storeVar(794 "com.agenttest.putFloatField.Main",795 "float",796 "com.agenttest.putFloatField.Main.f",797 "2.3"798 )799 }800 }801 @Test802 fun `test put int`(@TempDir tempDir: File) {803 val traces = generateTraces(tempDir, "agent-test_putIntField.jar")804 traces.shouldExist {805 it.storeVar(806 "com.agenttest.putIntField.Main",807 "int",808 "com.agenttest.putIntField.Main.i",809 "7"810 )811 }812 }813 @Test814 fun `test put long`(@TempDir tempDir: File) {815 val traces = generateTraces(tempDir, "agent-test_putLongField.jar")816 traces.shouldExist {817 it.storeVar(818 "com.agenttest.putLongField.Main",819 "long",820 "com.agenttest.putLongField.Main.l",821 "123456789123456789"822 )823 }824 }825 @Test826 fun `test put object`(@TempDir tempDir: File) {827 val traces = generateTraces(tempDir, "agent-test_putObjectField.jar")828 traces.shouldExist {829 it.storeVar(830 "com.agenttest.putObjectField.Main",831 "java.lang.Object",832 "com.agenttest.putObjectField.Main.o",833 null834 )835 }836 }837 @Test838 fun `test put short`(@TempDir tempDir: File) {839 val traces = generateTraces(tempDir, "agent-test_putShortField.jar")840 traces.shouldExist {841 it.storeVar(842 "com.agenttest.putShortField.Main",843 "short",844 "com.agenttest.putShortField.Main.s",845 "12345"846 )847 }848 }849 @Test850 fun `test put string`(@TempDir tempDir: File) {851 val traces = generateTraces(tempDir, "agent-test_putStringField.jar")852 traces.shouldExist {853 it.storeVar(854 "com.agenttest.putStringField.Main",855 "java.lang.String",856 "com.agenttest.putStringField.Main.s",857 "Hello"858 )859 }860 }861 }862 @Nested863 inner class PutStaticFieldTraceTests {864 @Test865 fun `test put boolean`(@TempDir tempDir: File) {866 val traces = generateTraces(tempDir, "agent-test_putBooleanStaticField.jar")867 traces.shouldExist {868 it.storeVar(869 "com.agenttest.putBooleanStaticField.Main",870 "boolean",871 "com.agenttest.putBooleanStaticField.Main.b",872 "true"873 )874 }875 }876 @Test877 fun `test put byte`(@TempDir tempDir: File) {878 val traces = generateTraces(tempDir, "agent-test_putByteStaticField.jar")879 traces.shouldExist {880 it.storeVar(881 "com.agenttest.putByteStaticField.Main",882 "byte",883 "com.agenttest.putByteStaticField.Main.b",884 "250"885 )886 }887 }888 @Test889 fun `test put char`(@TempDir tempDir: File) {890 val traces = generateTraces(tempDir, "agent-test_putCharStaticField.jar")891 traces.shouldExist {892 it.storeVar(893 "com.agenttest.putCharStaticField.Main",894 "char",895 "com.agenttest.putCharStaticField.Main.c",896 "Q"897 )898 }899 }900 @Test901 fun `test put double`(@TempDir tempDir: File) {902 val traces = generateTraces(tempDir, "agent-test_putDoubleStaticField.jar")903 traces.shouldExist {904 it.storeVar(905 "com.agenttest.putDoubleStaticField.Main",906 "double",907 "com.agenttest.putDoubleStaticField.Main.d",908 "4.2"909 )910 }911 }912 @Test913 fun `test put float`(@TempDir tempDir: File) {914 val traces = generateTraces(tempDir, "agent-test_putFloatStaticField.jar")915 traces.shouldExist {916 it.storeVar(917 "com.agenttest.putFloatStaticField.Main",918 "float",919 "com.agenttest.putFloatStaticField.Main.f",920 "2.3"921 )922 }923 }924 @Test925 fun `test put int`(@TempDir tempDir: File) {926 val traces = generateTraces(tempDir, "agent-test_putIntStaticField.jar")927 traces.shouldExist {928 it.storeVar(929 "com.agenttest.putIntStaticField.Main",930 "int",931 "com.agenttest.putIntStaticField.Main.i",932 "7"933 )934 }935 }936 @Test937 fun `test put long`(@TempDir tempDir: File) {938 val traces = generateTraces(tempDir, "agent-test_putLongStaticField.jar")939 traces.shouldExist {940 it.storeVar(941 "com.agenttest.putLongStaticField.Main",942 "long",943 "com.agenttest.putLongStaticField.Main.l",944 "123456789123456789"945 )946 }947 }948 @Test949 fun `test put object`(@TempDir tempDir: File) {950 val traces = generateTraces(tempDir, "agent-test_putObjectStaticField.jar")951 traces.shouldExist {952 it.storeVar(953 "com.agenttest.putObjectStaticField.Main",954 "java.lang.Object",955 "com.agenttest.putObjectStaticField.Main.o",956 null957 )958 }959 }960 @Test961 fun `test put short`(@TempDir tempDir: File) {962 val traces = generateTraces(tempDir, "agent-test_putShortStaticField.jar")963 traces.shouldExist {964 it.storeVar(965 "com.agenttest.putShortStaticField.Main",966 "short",967 "com.agenttest.putShortStaticField.Main.s",968 "12345"969 )970 }971 }972 @Test973 fun `test put string`(@TempDir tempDir: File) {974 val traces = generateTraces(tempDir, "agent-test_putStringStaticField.jar")975 traces.shouldExist {976 it.storeVar(977 "com.agenttest.putStringStaticField.Main",978 "java.lang.String",979 "com.agenttest.putStringStaticField.Main.s",980 "Hello"981 )982 }983 }984 }985 @Test986 fun `read past end of trace`(@TempDir tempDir: File) {987 CompileUtil.checkForAgentTestErrors(988 CompileUtil.runAgentTest("agent-test_noargs.jar", tempDir.toPath())989 )990 val traceFiles = tempDir.listFiles()!!.toList().filter { it.extension == "trace" }991 traceFiles.shouldHaveSize(1)992 TraceIterator(BufferedInputStream(FileInputStream(traceFiles[0]))).use {993 // Go to the end994 while (it.hasNext()) {995 it.next()996 }997 // Past the end998 shouldThrow<NoSuchElementException> { it.next() }999 }1000 }1001 companion object {1002 private val logger = KotlinLogging.logger { }1003 /**1004 * Generate traces by running the Jar under the agent. Asserts that there is only one trace1005 * file.1006 *1007 * @param tempDir The dir to save the trace file into.1008 * @param jarFilename The filename of the Jar to load from1009 * `rootProject/build/agent-test-jars`.1010 * @return The traces.1011 */1012 private fun generateTraces(tempDir: File, jarFilename: String): List<Trace> {1013 CompileUtil.checkForAgentTestErrors(1014 CompileUtil.runAgentTest(jarFilename, tempDir.toPath())1015 )1016 logger.debug {1017 """1018 |Files in temp dir:1019 |${tempDir.walkTopDown().joinToString("\n")}1020 """.trimMargin()1021 }1022 val traceFiles = tempDir.listFiles()!!.filter { it.extension == "trace" }1023 traceFiles.shouldHaveSize(1)1024 return TraceIterator(BufferedInputStream(FileInputStream(traceFiles[0]))).use {1025 it.asSequence().toList()1026 }1027 }1028 /**1029 * Assumes there is a virtual method call and asserts about its receiver type and arguments.1030 * Excludes instance and class initialization methods.1031 *1032 * @param receiverType The expected receiver type.1033 * @param methodName The expected method name.1034 * @param callerClass The class that the method is expected to be called from.1035 * @param args The expected (type, value) pairs for each argument in order.1036 */1037 private fun Trace.virtualMethodCall(1038 receiverType: String,1039 methodName: String,1040 callerClass: String,1041 args: List<Pair<String, String?>> = emptyList()1042 ) = this is MethodTrace &&1043 !isStatic &&1044 methodName != "<init>" &&1045 methodName != "<clinit>" &&1046 hasArguments(listOf(receiverType to null) + args) &&1047 this.methodName == methodName &&1048 this.callerClass == callerClass1049 /**1050 * Assumes there is a static method call and asserts about its arguments. Excludes instance1051 * and class initialization methods.1052 *1053 * @param methodName The expected method name.1054 * @param callerClass The class that the method is expected to be called from.1055 * @param args The expected (type, value) pairs for each argument in order.1056 */1057 private fun Trace.staticMethodCall(1058 methodName: String,1059 callerClass: String,1060 args: List<Pair<String, String?>> = emptyList()1061 ) = this is MethodTrace &&1062 isStatic &&1063 methodName != "<init>" &&1064 methodName != "<clinit>" &&1065 hasArguments(args) &&1066 this.methodName == methodName &&1067 this.callerClass == callerClass1068 /**1069 * Assumes there is an instance initializer method call and asserts about its arguments.1070 *1071 * @param receiverType The expected receiver type.1072 * @param callerClass The class that the method is expected to be called from.1073 * @param args The expected (type, value) pairs for each argument in order.1074 */1075 private fun Trace.constructorCall(1076 receiverType: String,1077 callerClass: String,1078 args: List<Pair<String, String?>> = emptyList()1079 ) = this is MethodTrace &&1080 !isStatic &&1081 methodName == "<init>" &&1082 hasArguments(listOf(receiverType to null) + args) &&1083 this.callerClass == callerClass1084 /**1085 * Checks there is a store with a value of [value] into a variable of type [varType].1086 *1087 * @param containingClass The class the store happens in.1088 * @param varType The type of the variable the value was stored in.1089 * @param name The name of the variable.1090 * @param value The value that was stored. Set to null if you don't care about the value.1091 */1092 private fun Trace.storeVar(1093 containingClass: String,1094 varType: String,1095 name: String,1096 value: String?1097 ) = this is StoreTrace &&1098 callerClass == containingClass &&1099 typeValuePair.type == varType &&1100 variableName == name &&1101 value?.let { typeValuePair.value == it } ?: true1102 /**1103 * Checks there is a store with a value of [value] into an array of type [varType] at index1104 * [arrayIndex].1105 *1106 * @param containingClass The class the store happens in.1107 * @param varType The type of the variable the value was stored in.1108 * @param arrayIndex The index in the array the value was stored in.1109 * @param value The value that was stored. Set to null if you don't care about the value.1110 */1111 private fun Trace.storeArray(1112 containingClass: String,1113 varType: String,1114 arrayIndex: Int,1115 value: String?1116 ) = this is ArrayStoreTrace &&1117 callerClass == containingClass &&1118 this.arrayIndex == arrayIndex &&1119 typeValuePair.type == varType &&1120 value?.let { typeValuePair.value == it } ?: true1121 private fun MethodTrace.hasArguments(args: List<Pair<String, String?>>) =1122 args.foldIndexed(true) { index, acc, (type, value) ->1123 if (value == null) {1124 // Null means we don't care about the value1125 acc && hasArgumentType(index, type)1126 } else {1127 acc && hasArgument(index, type, value)1128 }1129 }1130 private fun MethodTrace.hasArgumentType(index: Int, type: String) =1131 arguments[index].type == type1132 private fun MethodTrace.hasArgument(index: Int, type: String, value: String) =1133 arguments[index].type == type && arguments[index].value == value1134 }1135}...
matchers.kt
Source:matchers.kt
1package io.kotest.matchers.collections2import io.kotest.assertions.show.show3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.neverNullMatcher6import io.kotest.matchers.should7import io.kotest.matchers.shouldHave8import io.kotest.matchers.shouldNot9import kotlin.jvm.JvmName10fun <T> Iterable<T>.shouldContainOnlyNulls() = toList().shouldContainOnlyNulls()11fun <T> Array<T>.shouldContainOnlyNulls() = asList().shouldContainOnlyNulls()12fun <T> Collection<T>.shouldContainOnlyNulls() = this should containOnlyNulls()13fun <T> Iterable<T>.shouldNotContainOnlyNulls() = toList().shouldNotContainOnlyNulls()14fun <T> Array<T>.shouldNotContainOnlyNulls() = asList().shouldNotContainOnlyNulls()15fun <T> Collection<T>.shouldNotContainOnlyNulls() = this shouldNot containOnlyNulls()16fun <T> containOnlyNulls() = object : Matcher<Collection<T>> {17 override fun test(value: Collection<T>) =18 MatcherResult(19 value.all { it == null },20 "Collection should contain only nulls",21 "Collection should not contain only nulls"22 )23}24fun <T> Iterable<T>.shouldContainNull() = toList().shouldContainNull()25fun <T> Array<T>.shouldContainNull() = asList().shouldContainNull()26fun <T> Collection<T>.shouldContainNull() = this should containNull()27fun <T> Iterable<T>.shouldNotContainNull() = toList().shouldNotContainNull()28fun <T> Array<T>.shouldNotContainNull() = asList().shouldNotContainNull()29fun <T> Collection<T>.shouldNotContainNull() = this shouldNot containNull()30fun <T> containNull() = object : Matcher<Collection<T>> {31 override fun test(value: Collection<T>) =32 MatcherResult(33 value.any { it == null },34 "Collection should contain at least one null",35 "Collection should not contain any nulls"36 )37}38fun <T> Iterable<T>.shouldHaveElementAt(index: Int, element: T) = toList().shouldHaveElementAt(index, element)39fun <T> Array<T>.shouldHaveElementAt(index: Int, element: T) = asList().shouldHaveElementAt(index, element)40fun <T> List<T>.shouldHaveElementAt(index: Int, element: T) = this should haveElementAt(index, element)41fun <T> Iterable<T>.shouldNotHaveElementAt(index: Int, element: T) = toList().shouldNotHaveElementAt(index, element)42fun <T> Array<T>.shouldNotHaveElementAt(index: Int, element: T) = asList().shouldNotHaveElementAt(index, element)43fun <T> List<T>.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)44fun <T, L : List<T>> haveElementAt(index: Int, element: T) = object : Matcher<L> {45 override fun test(value: L) =46 MatcherResult(47 value[index] == element,48 { "Collection should contain ${element.show().value} at index $index" },49 { "Collection should not contain ${element.show().value} at index $index" }50 )51}52fun <T> Iterable<T>.shouldContainNoNulls() = toList().shouldContainNoNulls()53fun <T> Array<T>.shouldContainNoNulls() = asList().shouldContainNoNulls()54fun <T> Collection<T>.shouldContainNoNulls() = this should containNoNulls()55fun <T> Iterable<T>.shouldNotContainNoNulls() = toList().shouldNotContainNoNulls()56fun <T> Array<T>.shouldNotContainNoNulls() = asList().shouldNotContainNoNulls()57fun <T> Collection<T>.shouldNotContainNoNulls() = this shouldNot containNoNulls()58fun <T> containNoNulls() = object : Matcher<Collection<T>> {59 override fun test(value: Collection<T>) =60 MatcherResult(61 value.all { it != null },62 { "Collection should not contain nulls" },63 { "Collection should have at least one null" }64 )65}66infix fun <T> Array<T>.shouldNotContainExactlyInAnyOrder(expected: Array<T>) =67 asList().shouldNotContainExactlyInAnyOrder(expected.asList())68infix fun <T, C : Collection<T>> C?.shouldNotContainExactlyInAnyOrder(expected: C) =69 this shouldNot containExactlyInAnyOrder(expected)70fun <T, C : Collection<T>> C?.shouldNotContainExactlyInAnyOrder(vararg expected: T) =71 this shouldNot containExactlyInAnyOrder(*expected)72infix fun <T> Array<T>.shouldContainExactlyInAnyOrder(expected: Array<T>) =73 asList().shouldContainExactlyInAnyOrder(expected.asList())74infix fun <T, C : Collection<T>> C?.shouldContainExactlyInAnyOrder(expected: C) =75 this should containExactlyInAnyOrder(expected)76fun <T, C : Collection<T>> C?.shouldContainExactlyInAnyOrder(vararg expected: T) =77 this should containExactlyInAnyOrder(*expected)78fun <T> containExactlyInAnyOrder(vararg expected: T): Matcher<Collection<T>?> =79 containExactlyInAnyOrder(expected.asList())80/** Assert that a collection contains exactly the given values and nothing else, in any order. */81fun <T, C : Collection<T>> containExactlyInAnyOrder(expected: C): Matcher<C?> = neverNullMatcher { value ->82 val valueGroupedCounts: Map<T, Int> = value.groupBy { it }.mapValues { it.value.size }83 val expectedGroupedCounts: Map<T, Int> = expected.groupBy { it }.mapValues { it.value.size }84 val passed = expectedGroupedCounts.size == valueGroupedCounts.size85 && expectedGroupedCounts.all { valueGroupedCounts[it.key] == it.value }86 MatcherResult(87 passed,88 "Collection should contain ${expected.show().value} in any order, but was ${value.show().value}",89 "Collection should not contain exactly ${expected.show().value} in any order"90 )91}92infix fun <T : Comparable<T>> Iterable<T>.shouldHaveUpperBound(t: T) = toList().shouldHaveUpperBound(t)93infix fun <T : Comparable<T>> Array<T>.shouldHaveUpperBound(t: T) = asList().shouldHaveUpperBound(t)94infix fun <T : Comparable<T>, C : Collection<T>> C.shouldHaveUpperBound(t: T) = this should haveUpperBound(t)95fun <T : Comparable<T>, C : Collection<T>> haveUpperBound(t: T) = object : Matcher<C> {96 override fun test(value: C) = MatcherResult(97 value.all { it <= t },98 "Collection should have upper bound $t",99 "Collection should not have upper bound $t"100 )101}102infix fun <T : Comparable<T>> Iterable<T>.shouldHaveLowerBound(t: T) = toList().shouldHaveLowerBound(t)103infix fun <T : Comparable<T>> Array<T>.shouldHaveLowerBound(t: T) = asList().shouldHaveLowerBound(t)104infix fun <T : Comparable<T>, C : Collection<T>> C.shouldHaveLowerBound(t: T) = this should haveLowerBound(t)105fun <T : Comparable<T>, C : Collection<T>> haveLowerBound(t: T) = object : Matcher<C> {106 override fun test(value: C) = MatcherResult(107 value.all { t <= it },108 "Collection should have lower bound $t",109 "Collection should not have lower bound $t"110 )111}112fun <T> Iterable<T>.shouldBeUnique() = toList().shouldBeUnique()113fun <T> Array<T>.shouldBeUnique() = asList().shouldBeUnique()114fun <T> Collection<T>.shouldBeUnique() = this should beUnique()115fun <T> Iterable<T>.shouldNotBeUnique() = toList().shouldNotBeUnique()116fun <T> Array<T>.shouldNotBeUnique() = asList().shouldNotBeUnique()117fun <T> Collection<T>.shouldNotBeUnique() = this shouldNot beUnique()118fun <T> beUnique() = object : Matcher<Collection<T>> {119 override fun test(value: Collection<T>) = MatcherResult(120 value.toSet().size == value.size,121 "Collection should be Unique",122 "Collection should contain at least one duplicate element"123 )124}125fun <T> Iterable<T>.shouldContainDuplicates() = toList().shouldContainDuplicates()126fun <T> Array<T>.shouldContainDuplicates() = asList().shouldContainDuplicates()127fun <T> Collection<T>.shouldContainDuplicates() = this should containDuplicates()128fun <T> Iterable<T>.shouldNotContainDuplicates() = toList().shouldNotContainDuplicates()129fun <T> Array<T>.shouldNotContainDuplicates() = asList().shouldNotContainDuplicates()130fun <T> Collection<T>.shouldNotContainDuplicates() = this shouldNot containDuplicates()131fun <T> containDuplicates() = object : Matcher<Collection<T>> {132 override fun test(value: Collection<T>) = MatcherResult(133 value.toSet().size < value.size,134 "Collection should contain duplicates",135 "Collection should not contain duplicates"136 )137}138fun <T> beSortedWith(comparator: Comparator<in T>): Matcher<List<T>> = sortedWith(comparator)139fun <T> beSortedWith(cmp: (T, T) -> Int): Matcher<List<T>> = sortedWith(cmp)140fun <T> sortedWith(comparator: Comparator<in T>): Matcher<List<T>> = sortedWith { a, b ->141 comparator.compare(a, b)142}143fun <T> sortedWith(cmp: (T, T) -> Int): Matcher<List<T>> = object : Matcher<List<T>> {144 override fun test(value: List<T>): MatcherResult {145 val failure = value.withIndex().firstOrNull { (i, it) -> i != value.lastIndex && cmp(it, value[i + 1]) > 0 }146 val snippet = value.joinToString(",", limit = 10)147 val elementMessage = when (failure) {148 null -> ""149 else -> ". Element ${failure.value} at index ${failure.index} shouldn't precede element ${value[failure.index + 1]}"150 }151 return MatcherResult(152 failure == null,153 "List [$snippet] should be sorted$elementMessage",154 "List [$snippet] should not be sorted"155 )156 }157}158fun <T : Comparable<T>> Iterable<T>.shouldBeSorted() = toList().shouldBeSorted()159fun <T : Comparable<T>> Array<T>.shouldBeSorted() = asList().shouldBeSorted()160fun <T : Comparable<T>> List<T>.shouldBeSorted() = this should beSorted<T>()161fun <T : Comparable<T>> Iterable<T>.shouldNotBeSorted() = toList().shouldNotBeSorted()162fun <T : Comparable<T>> Array<T>.shouldNotBeSorted() = asList().shouldNotBeSorted()163fun <T : Comparable<T>> List<T>.shouldNotBeSorted() = this shouldNot beSorted<T>()164infix fun <T> Iterable<T>.shouldBeSortedWith(comparator: Comparator<in T>) = toList().shouldBeSortedWith(comparator)165infix fun <T> Array<T>.shouldBeSortedWith(comparator: Comparator<in T>) = asList().shouldBeSortedWith(comparator)166infix fun <T> List<T>.shouldBeSortedWith(comparator: Comparator<in T>) = this should beSortedWith(comparator)167infix fun <T> Iterable<T>.shouldNotBeSortedWith(comparator: Comparator<in T>) = toList().shouldNotBeSortedWith(comparator)168infix fun <T> Array<T>.shouldNotBeSortedWith(comparator: Comparator<in T>) = asList().shouldNotBeSortedWith(comparator)169infix fun <T> List<T>.shouldNotBeSortedWith(comparator: Comparator<in T>) = this shouldNot beSortedWith(comparator)170infix fun <T> Iterable<T>.shouldBeSortedWith(cmp: (T, T) -> Int) = toList().shouldBeSortedWith(cmp)171infix fun <T> Array<T>.shouldBeSortedWith(cmp: (T, T) -> Int) = asList().shouldBeSortedWith(cmp)172infix fun <T> List<T>.shouldBeSortedWith(cmp: (T, T) -> Int) = this should beSortedWith(cmp)173infix fun <T> Iterable<T>.shouldNotBeSortedWith(cmp: (T, T) -> Int) = toList().shouldNotBeSortedWith(cmp)174infix fun <T> Array<T>.shouldNotBeSortedWith(cmp: (T, T) -> Int) = asList().shouldNotBeSortedWith(cmp)175infix fun <T> List<T>.shouldNotBeSortedWith(cmp: (T, T) -> Int) = this shouldNot beSortedWith(cmp)176fun <T : Comparable<T>> Iterable<T>.shouldBeMonotonicallyIncreasing() = toList().shouldBeMonotonicallyIncreasing()177fun <T : Comparable<T>> Array<T>.shouldBeMonotonicallyIncreasing() = asList().shouldBeMonotonicallyIncreasing()178fun <T : Comparable<T>> List<T>.shouldBeMonotonicallyIncreasing() = this should beMonotonicallyIncreasing<T>()179fun <T : Comparable<T>> Iterable<T>.shouldNotBeMonotonicallyIncreasing() = toList().shouldNotBeMonotonicallyIncreasing()180fun <T : Comparable<T>> Array<T>.shouldNotBeMonotonicallyIncreasing() = asList().shouldNotBeMonotonicallyIncreasing()181fun <T : Comparable<T>> List<T>.shouldNotBeMonotonicallyIncreasing() = this shouldNot beMonotonicallyIncreasing<T>()182fun <T> List<T>.shouldBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =183 this should beMonotonicallyIncreasingWith(comparator)184fun <T> Iterable<T>.shouldBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =185 toList().shouldBeMonotonicallyIncreasingWith(comparator)186fun <T> Array<T>.shouldBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =187 asList().shouldBeMonotonicallyIncreasingWith(comparator)188fun <T> List<T>.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =189 this shouldNot beMonotonicallyIncreasingWith(comparator)190fun <T> Iterable<T>.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =191 toList().shouldNotBeMonotonicallyIncreasingWith(comparator)192fun <T> Array<T>.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator<in T>) =193 asList().shouldNotBeMonotonicallyIncreasingWith(comparator)194fun <T : Comparable<T>> Iterable<T>.shouldBeMonotonicallyDecreasing() = toList().shouldBeMonotonicallyDecreasing()195fun <T : Comparable<T>> Array<T>.shouldBeMonotonicallyDecreasing() = asList().shouldBeMonotonicallyDecreasing()196fun <T : Comparable<T>> List<T>.shouldBeMonotonicallyDecreasing() = this should beMonotonicallyDecreasing<T>()197fun <T : Comparable<T>> Iterable<T>.shouldNotBeMonotonicallyDecreasing() = toList().shouldNotBeMonotonicallyDecreasing()198fun <T : Comparable<T>> Array<T>.shouldNotBeMonotonicallyDecreasing() = asList().shouldNotBeMonotonicallyDecreasing()199fun <T : Comparable<T>> List<T>.shouldNotBeMonotonicallyDecreasing() = this shouldNot beMonotonicallyDecreasing<T>()200fun <T> List<T>.shouldBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =201 this should beMonotonicallyDecreasingWith(comparator)202fun <T> Iterable<T>.shouldBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =203 toList().shouldBeMonotonicallyDecreasingWith(comparator)204fun <T> Array<T>.shouldBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =205 asList().shouldBeMonotonicallyDecreasingWith(comparator)206fun <T> List<T>.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =207 this shouldNot beMonotonicallyDecreasingWith(comparator)208fun <T> Iterable<T>.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =209 toList().shouldNotBeMonotonicallyDecreasingWith(comparator)210fun <T> Array<T>.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator<in T>) =211 asList().shouldNotBeMonotonicallyDecreasingWith(comparator)212fun <T : Comparable<T>> Iterable<T>.shouldBeStrictlyIncreasing() = toList().shouldBeStrictlyIncreasing()213fun <T : Comparable<T>> Array<T>.shouldBeStrictlyIncreasing() = asList().shouldBeStrictlyIncreasing()214fun <T : Comparable<T>> List<T>.shouldBeStrictlyIncreasing() = this should beStrictlyIncreasing<T>()215fun <T : Comparable<T>> Iterable<T>.shouldNotBeStrictlyIncreasing() = toList().shouldNotBeStrictlyIncreasing()216fun <T : Comparable<T>> Array<T>.shouldNotBeStrictlyIncreasing() = asList().shouldNotBeStrictlyIncreasing()217fun <T : Comparable<T>> List<T>.shouldNotBeStrictlyIncreasing() = this shouldNot beStrictlyIncreasing<T>()218fun <T> List<T>.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T>) =219 this should beStrictlyIncreasingWith(comparator)220fun <T> Iterable<T>.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T>) =221 toList().shouldBeStrictlyIncreasingWith(comparator)222fun <T> Array<T>.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T>) =223 asList().shouldBeStrictlyIncreasingWith(comparator)224fun <T> List<T>.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<in T>) =225 this shouldNot beStrictlyIncreasingWith(comparator)226fun <T> Iterable<T>.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<in T>) =227 toList().shouldNotBeStrictlyIncreasingWith(comparator)228fun <T> Array<T>.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<in T>) =229 asList().shouldNotBeStrictlyIncreasingWith(comparator)230fun <T : Comparable<T>> Iterable<T>.shouldBeStrictlyDecreasing() = toList().shouldBeStrictlyDecreasing()231fun <T : Comparable<T>> List<T>.shouldBeStrictlyDecreasing() = this should beStrictlyDecreasing<T>()232fun <T : Comparable<T>> Iterable<T>.shouldNotBeStrictlyDecreasing() = toList().shouldNotBeStrictlyDecreasing()233fun <T : Comparable<T>> List<T>.shouldNotBeStrictlyDecreasing() = this shouldNot beStrictlyDecreasing<T>()234fun <T> List<T>.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T>) =235 this should beStrictlyDecreasingWith(comparator)236fun <T> Iterable<T>.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T>) =237 toList().shouldBeStrictlyDecreasingWith(comparator)238fun <T> Array<T>.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T>) =239 asList().shouldBeStrictlyDecreasingWith(comparator)240fun <T> List<T>.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<in T>) =241 this shouldNot beStrictlyDecreasingWith(comparator)242fun <T> Iterable<T>.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<in T>) =243 toList().shouldNotBeStrictlyDecreasingWith(comparator)244fun <T> Array<T>.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<in T>) =245 asList().shouldNotBeStrictlyDecreasingWith(comparator)246infix fun <T> Iterable<T>.shouldHaveSingleElement(t: T) = toList().shouldHaveSingleElement(t)247infix fun <T> Array<T>.shouldHaveSingleElement(t: T) = asList().shouldHaveSingleElement(t)248infix fun <T> Iterable<T>.shouldHaveSingleElement(p: (T) -> Boolean) = toList().shouldHaveSingleElement(p)249infix fun <T> Array<T>.shouldHaveSingleElement(p: (T) -> Boolean) = asList().shouldHaveSingleElement(p)250infix fun <T> Collection<T>.shouldHaveSingleElement(t: T) = this should singleElement(t)251infix fun <T> Collection<T>.shouldHaveSingleElement(p: (T) -> Boolean) = this should singleElement(p)252infix fun <T> Iterable<T>.shouldNotHaveSingleElement(t: T) = toList().shouldNotHaveSingleElement(t)253infix fun <T> Array<T>.shouldNotHaveSingleElement(t: T) = asList().shouldNotHaveSingleElement(t)254infix fun <T> Collection<T>.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)255infix fun <T> Iterable<T>.shouldHaveSize(size: Int) = toList().shouldHaveSize(size)256infix fun <T> Array<T>.shouldHaveSize(size: Int) = asList().shouldHaveSize(size)257infix fun <T> Collection<T>.shouldHaveSize(size: Int) = this should haveSize(size = size)258infix fun <T> Iterable<T>.shouldNotHaveSize(size: Int) = toList().shouldNotHaveSize(size)259infix fun <T> Array<T>.shouldNotHaveSize(size: Int) = asList().shouldNotHaveSize(size)260infix fun <T> Collection<T>.shouldNotHaveSize(size: Int) = this shouldNot haveSize(size)261/**262 * Verifies this collection contains only one element263 *264 * This assertion is an alias to `collection shouldHaveSize 1`. This will pass if the collection have exactly one element265 * (definition of a Singleton Collection)266 *267 * ```268 * listOf(1).shouldBeSingleton() // Assertion passes269 * listOf(1, 2).shouldBeSingleton() // Assertion fails270 * ```271 *272 * @see [shouldHaveSize]273 * @see [shouldNotBeSingleton]274 * @see [shouldHaveSingleElement]275 */276fun <T> Collection<T>.shouldBeSingleton() = this shouldHaveSize 1277fun <T> Iterable<T>.shouldBeSingleton() = toList().shouldBeSingleton()278fun <T> Array<T>.shouldBeSingleton() = asList().shouldBeSingleton()279inline fun <T> Collection<T>.shouldBeSingleton(fn: (T) -> Unit) {280 this.shouldBeSingleton()281 fn(this.first())282}283inline fun <T> Iterable<T>.shouldBeSingleton(fn: (T) -> Unit) {284 toList().shouldBeSingleton(fn)285}286inline fun <T> Array<T>.shouldBeSingleton(fn: (T) -> Unit) {287 asList().shouldBeSingleton(fn)288}289/**290 * Verifies this collection doesn't contain only one element291 *292 * This assertion is an alias to `collection shouldNotHaveSize 1`. This will pass if the collection doesn't have exactly one element293 * (definition of a Singleton Collection)294 *295 * ```296 * listOf(1, 2).shouldNotBeSingleton() // Assertion passes297 * listOf<Int>().shouldNotBeSingleton() // Assertion passes298 * listOf(1).shouldNotBeSingleton() // Assertion fails299 * ```300 *301 * @see [shouldNotHaveSize]302 * @see [shouldBeSingleton]303 * @see [shouldNotHaveSingleElement]304 */305fun <T> Collection<T>.shouldNotBeSingleton() = this shouldNotHaveSize 1306fun <T> Iterable<T>.shouldNotBeSingleton() = toList().shouldNotBeSingleton()307fun <T> Array<T>.shouldNotBeSingleton() = asList().shouldNotBeSingleton()308infix fun <T, U> Iterable<T>.shouldBeLargerThan(other: Collection<U>) = toList().shouldBeLargerThan(other)309infix fun <T, U> Array<T>.shouldBeLargerThan(other: Collection<U>) = asList().shouldBeLargerThan(other)310infix fun <T, U> Iterable<T>.shouldBeLargerThan(other: Iterable<U>) = toList().shouldBeLargerThan(other.toList())311infix fun <T, U> Array<T>.shouldBeLargerThan(other: Array<U>) = asList().shouldBeLargerThan(other.asList())312infix fun <T, U> Collection<T>.shouldBeLargerThan(other: Collection<U>) = this should beLargerThan(other)313fun <T, U> beLargerThan(other: Collection<U>) = object : Matcher<Collection<T>> {314 override fun test(value: Collection<T>) = MatcherResult(315 value.size > other.size,316 "Collection of size ${value.size} should be larger than collection of size ${other.size}",317 "Collection of size ${value.size} should not be larger than collection of size ${other.size}"318 )319}320infix fun <T, U> Iterable<T>.shouldBeSmallerThan(other: Collection<U>) = toList().shouldBeSmallerThan(other)321infix fun <T, U> Array<T>.shouldBeSmallerThan(other: Collection<U>) = asList().shouldBeSmallerThan(other)322infix fun <T, U> Iterable<T>.shouldBeSmallerThan(other: Iterable<U>) = toList().shouldBeSmallerThan(other.toList())323infix fun <T, U> Array<T>.shouldBeSmallerThan(other: Array<U>) = asList().shouldBeSmallerThan(other.asList())324infix fun <T, U> Collection<T>.shouldBeSmallerThan(other: Collection<U>) = this should beSmallerThan(other)325fun <T, U> beSmallerThan(other: Collection<U>) = object : Matcher<Collection<T>> {326 override fun test(value: Collection<T>) = MatcherResult(327 value.size < other.size,328 "Collection of size ${value.size} should be smaller than collection of size ${other.size}",329 "Collection of size ${value.size} should not be smaller than collection of size ${other.size}"330 )331}332infix fun <T, U> Iterable<T>.shouldBeSameSizeAs(other: Collection<U>) = toList().shouldBeSameSizeAs(other)333infix fun <T, U> Array<T>.shouldBeSameSizeAs(other: Collection<U>) = asList().shouldBeSameSizeAs(other)334infix fun <T, U> Iterable<T>.shouldBeSameSizeAs(other: Iterable<U>) = toList().shouldBeSameSizeAs(other.toList())335infix fun <T, U> Array<T>.shouldBeSameSizeAs(other: Array<U>) = asList().shouldBeSameSizeAs(other.asList())336infix fun <T, U> Collection<T>.shouldBeSameSizeAs(other: Collection<U>) = this should beSameSizeAs(other)337fun <T, U> beSameSizeAs(other: Collection<U>) = object : Matcher<Collection<T>> {338 override fun test(value: Collection<T>) = MatcherResult(339 value.size == other.size,340 "Collection of size ${value.size} should be the same size as collection of size ${other.size}",341 "Collection of size ${value.size} should not be the same size as collection of size ${other.size}"342 )343}344infix fun <T> Iterable<T>.shouldHaveAtLeastSize(n: Int) = toList().shouldHaveAtLeastSize(n)345infix fun <T> Array<T>.shouldHaveAtLeastSize(n: Int) = asList().shouldHaveAtLeastSize(n)346infix fun <T> Collection<T>.shouldHaveAtLeastSize(n: Int) = this shouldHave atLeastSize(n)347fun <T> atLeastSize(n: Int) = object : Matcher<Collection<T>> {348 override fun test(value: Collection<T>) = MatcherResult(349 value.size >= n,350 "Collection should contain at least $n elements",351 "Collection should contain less than $n elements"352 )353}354infix fun <T> Iterable<T>.shouldHaveAtMostSize(n: Int) = toList().shouldHaveAtMostSize(n)355infix fun <T> Array<T>.shouldHaveAtMostSize(n: Int) = asList().shouldHaveAtMostSize(n)356infix fun <T> Collection<T>.shouldHaveAtMostSize(n: Int) = this shouldHave atMostSize(n)357fun <T> atMostSize(n: Int) = object : Matcher<Collection<T>> {358 override fun test(value: Collection<T>) = MatcherResult(359 value.size <= n,360 "Collection should contain at most $n elements",361 "Collection should contain more than $n elements"362 )363}364infix fun <T> Iterable<T>.shouldExist(p: (T) -> Boolean) = toList().shouldExist(p)365infix fun <T> Array<T>.shouldExist(p: (T) -> Boolean) = asList().shouldExist(p)366infix fun <T> Collection<T>.shouldExist(p: (T) -> Boolean) = this should exist(p)367fun <T> exist(p: (T) -> Boolean) = object : Matcher<Collection<T>> {368 override fun test(value: Collection<T>) = MatcherResult(369 value.any { p(it) },370 "Collection should contain an element that matches the predicate $p",371 "Collection should not contain an element that matches the predicate $p"372 )373}374fun <T> Iterable<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = toList().shouldExistInOrder(ps.toList())375fun <T> Array<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = asList().shouldExistInOrder(ps.toList())376fun <T> List<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = this.shouldExistInOrder(ps.toList())377infix fun <T> Iterable<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = toList().shouldExistInOrder(expected)378infix fun <T> Array<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldExistInOrder(expected)379infix fun <T> List<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = this should existInOrder(expected)380infix fun <T> Iterable<T>.shouldNotExistInOrder(expected: Iterable<(T) -> Boolean>) = toList().shouldNotExistInOrder(expected.toList())381infix fun <T> Array<T>.shouldNotExistInOrder(expected: Array<(T) -> Boolean>) = asList().shouldNotExistInOrder(expected.asList())382infix fun <T> Iterable<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = toList().shouldNotExistInOrder(expected)383infix fun <T> Array<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldNotExistInOrder(expected)384infix fun <T> List<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = this shouldNot existInOrder(expected)385fun <T> Iterable<T>.shouldBeEmpty() = toList().shouldBeEmpty()386fun <T> Array<T>.shouldBeEmpty() = asList().shouldBeEmpty()387fun <T> Collection<T>.shouldBeEmpty() = this should beEmpty()388fun <T> Iterable<T>.shouldNotBeEmpty() = toList().shouldNotBeEmpty()389fun <T> Array<T>.shouldNotBeEmpty() = asList().shouldNotBeEmpty()390fun <T> Collection<T>.shouldNotBeEmpty() = this shouldNot beEmpty()391fun <T> Iterable<T>.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(ts)392fun <T> Array<T>.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(ts)393fun <T> Collection<T>.shouldContainAnyOf(vararg ts: T) = this should containAnyOf(ts.asList())394fun <T> Iterable<T>.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(ts)395fun <T> Array<T>.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(ts)396fun <T> Collection<T>.shouldNotContainAnyOf(vararg ts: T) = this shouldNot containAnyOf(ts.asList())397infix fun <T> Iterable<T>.shouldContainAnyOf(ts: Collection<T>) = toList().shouldContainAnyOf(ts)398infix fun <T> Array<T>.shouldContainAnyOf(ts: Collection<T>) = asList().shouldContainAnyOf(ts)399infix fun <T> Collection<T>.shouldContainAnyOf(ts: Collection<T>) = this should containAnyOf(ts)400infix fun <T> Iterable<T>.shouldNotContainAnyOf(ts: Collection<T>) = toList().shouldNotContainAnyOf(ts)401infix fun <T> Array<T>.shouldNotContainAnyOf(ts: Collection<T>) = asList().shouldNotContainAnyOf(ts)402infix fun <T> Collection<T>.shouldNotContainAnyOf(ts: Collection<T>) = this shouldNot containAnyOf(ts)403fun <T> containAnyOf(ts: Collection<T>) = object : Matcher<Collection<T>> {404 override fun test(value: Collection<T>): MatcherResult {405 if (ts.isEmpty()) throwEmptyCollectionError()406 return MatcherResult(407 ts.any { it in value },408 { "Collection should contain any of ${ts.joinToString(separator = ", ", limit = 10) { it.show().value }}" },409 { "Collection should not contain any of ${ts.joinToString(separator = ", ", limit = 10) { it.show().value }}" }410 )411 }412}413/**414 * Verifies that this instance is in [collection]415 *416 * Assertion to check that this instance is in [collection]. This assertion checks by reference, and not by value,417 * therefore the exact instance must be in [collection], or this will fail.418 *419 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]420 *421 * @see [shouldNotBeOneOf]422 * @see [beOneOf]423 */424infix fun <T> T.shouldBeOneOf(collection: Collection<T>) = this should beOneOf(collection)425/**426 * Verifies that this instance is NOT in [collection]427 *428 * Assertion to check that this instance is not in [collection]. This assertion checks by reference, and not by value,429 * therefore the exact instance must not be in [collection], or this will fail.430 *431 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]432 *433 * @see [shouldBeOneOf]434 * @see [beOneOf]435 */436infix fun <T> T.shouldNotBeOneOf(collection: Collection<T>) = this shouldNot beOneOf(collection)437/**438 * Verifies that this instance is any of [any]439 *440 * Assertion to check that this instance is any of [any]. This assertion checks by reference, and not by value,441 * therefore the exact instance must be in [any], or this will fail.442 *443 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]444 *445 * @see [shouldNotBeOneOf]446 * @see [beOneOf]447 */448fun <T> T.shouldBeOneOf(vararg any: T) = this should beOneOf(any.toList())449/**450 * Verifies that this instance is NOT any of [any]451 *452 * Assertion to check that this instance is not any of [any]. This assertion checks by reference, and not by value,453 * therefore the exact instance must not be in [any], or this will fail.454 *455 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]456 *457 * @see [shouldNotBeOneOf]458 * @see [beOneOf]459 */460fun <T> T.shouldNotBeOneOf(vararg any: T) = this shouldNot beOneOf(any.toList())461/**462 * Matcher that verifies that this instance is in [collection]463 *464 * Assertion to check that this instance is in [collection]. This matcher checks by reference, and not by value,465 * therefore the exact instance must be in [collection], or this will fail.466 *467 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]468 *469 * @see [shouldBeOneOf]470 * @see [shouldNotBeOneOf]471 */472fun <T> beOneOf(collection: Collection<T>) = object : Matcher<T> {473 override fun test(value: T): MatcherResult {474 if (collection.isEmpty()) throwEmptyCollectionError()475 val match = collection.any { it === value }476 return MatcherResult(477 match,478 "Collection should contain the instance of value, but doesn't.",479 "Collection should not contain the instance of value, but does."480 )481 }482}483/**484 * Verifies that this element is in [collection] by comparing value485 *486 * Assertion to check that this element is in [collection]. This assertion checks by value, and not by reference,487 * therefore even if the exact instance is not in [collection] but another instance with same value is present, the488 * test will pass.489 *490 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]491 *492 * @see [shouldNotBeIn]493 * @see [beIn]494 */495infix fun <T> T.shouldBeIn(collection: Collection<T>) = this should beIn(collection)496/**497 * Verifies that this element is NOT any of [collection]498 *499 * Assertion to check that this element is not any of [collection]. This assertion checks by value, and not by reference,500 * therefore any instance with same value must not be in [collection], or this will fail.501 *502 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]503 *504 * @see [shouldNotBeIn]505 * @see [beIn]506 */507infix fun <T> T.shouldNotBeIn(collection: Collection<T>) = this shouldNot beIn(collection.toList())508/**509 * Verifies that this element is any of [any] by comparing value510 *511 * Assertion to check that this element is any of [any]. This assertion checks by value, and not by reference,512 * therefore even if the exact instance is not any of [any] but another instance with same value is present, the513 * test will pass.514 *515 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]516 *517 * @see [shouldNotBeIn]518 * @see [beIn]519 */520fun <T> T.shouldBeIn(vararg any: T) = this should beIn(any.toList())521/**522 * Verifies that this element is NOT any of [any]523 *524 * Assertion to check that this element is not any of [any]. This assertion checks by value, and not by reference,525 * therefore any instance with same value must not be in [any], or this will fail.526 *527 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]528 *529 * @see [shouldNotBeIn]530 * @see [beIn]531 */532fun <T> T.shouldNotBeIn(vararg any: T) = this shouldNot beIn(any.toList())533/**534 * Verifies that this element is in [array] by comparing value535 *536 * Assertion to check that this element is in [array]. This assertion checks by value, and not by reference,537 * therefore even if the exact instance is not in [array] but another instance with same value is present, the538 * test will pass.539 *540 * An empty array will always fail. If you need to check for empty array, use [Array.shouldBeEmpty]541 *542 * @see [shouldNotBeIn]543 * @see [beIn]544 */545@JvmName("shouldBeInArray")546infix fun <T> T.shouldBeIn(array: Array<T>) = this should beIn(array.toList())547/**548 * Verifies that this element is NOT any of [array]549 *550 * Assertion to check that this element is not any of [array]. This assertion checks by value, and not by reference,551 * therefore any instance with same value must not be in [array], or this will fail.552 *553 * An empty array will always fail. If you need to check for empty array, use [Array.shouldBeEmpty]554 *555 * @see [shouldNotBeIn]556 * @see [beIn]557 */558@JvmName("shouldNotBeInArray")559infix fun <T> T.shouldNotBeIn(array: Array<T>) = this shouldNot beIn(array.toList())560/**561 * Matcher that verifies that this element is in [collection] by comparing value562 *563 * Assertion to check that this element is in [collection]. This assertion checks by value, and not by reference,564 * therefore even if the exact instance is not in [collection] but another instance with same value is present, the565 * test will pass.566 *567 * An empty collection will always fail. If you need to check for empty collection, use [Collection.shouldBeEmpty]568 *569 * @see [shouldBeOneOf]570 * @see [shouldNotBeOneOf]571 */572fun <T> beIn(collection: Collection<T>) = object : Matcher<T> {573 override fun test(value: T): MatcherResult {574 if (collection.isEmpty()) throwEmptyCollectionError()575 val match = value in collection576 return MatcherResult(577 match,578 "Collection should contain ${value.show().value}, but doesn't. Possible values: ${collection.show().value}",579 "Collection should not contain ${value.show().value}, but does. Forbidden values: ${collection.show().value}"580 )581 }582}583private fun throwEmptyCollectionError(): Nothing {584 throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")585}...
Array.shouldExistInOrder
Using AI Code Generation
1 import io.kotest.matchers.collections.shouldExistInOrder2 import io.kotest.matchers.shouldBe3 import io.kotest.matchers.shouldNotBe4 import io.kotest.matchers.shouldNotBe5 import org.junit.jupiter.api.Test6 import org.junit.jupiter.api.TestInstance7 import org.junit.jupiter.api.TestInstance.Lifecycle8 import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS9 @TestInstance(PER_CLASS)10 class ArrayTest {11 fun `should not be empty`() {12 val arr = arrayOf(1, 2, 3)13 arr shouldNotBe emptyArray<Int>()14 }15 fun `should exist in order`() {16 val arr = arrayOf(1, 2, 3)17 }18 fun `should exist in order with custom message`() {19 val arr = arrayOf(1, 2, 3)20 }21 fun `should not exist in order`() {22 val arr = arrayOf(1, 2, 3)23 }24 fun `should not exist in order with custom message`() {25 val arr = arrayOf(1, 2, 3)26 }27 fun `should exist in order with multiple elements`() {28 val arr = arrayOf(1, 2, 3, 4, 5, 6)29 }30 fun `should exist in order with multiple elements with custom message`() {31 val arr = arrayOf(1, 2, 3, 4, 5, 6)32 }
Array.shouldExistInOrder
Using AI Code Generation
1 import io.kotest.matchers.collections.shouldExistInOrder2 import io.kotest.matchers.shouldBe3 import io.kotest.matchers.shouldNotBe4 import io.kotest.matchers.shouldNotBe5 import org.junit.jupiter.api.Test6 import org.junit.jupiter.api.TestInstance7 import org.junit.jupiter.api.TestInstance.Lifecycle8 import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS9 @TestInstance(PER_CLASS)10 class ArrayTest {11 fun `should not be empty`() {12 val arr = arrayOf(1, 2, 3)13 arr shouldNotBe emptyArray<Int>()14 }15 fun `should exist in order`() {16 val arr = arrayOf(1, 2, 3)17 }18 fun `should exist in order with custom message`() {19 val arr = arrayOf(1, 2, 3)20 }21 fun `should not exist in order`() {22 val arr = arrayOf(1, 2, 3)23 }24 fun `should not exist in order with custom message`() {25 val arr = arrayOf(1, 2, 3)26 }27 fun `should exist in order with multiple elements`() {28 val arr = arrayOf(1, 2, 3, 4, 5, 6)29 }30 fun `should exist in order with multiple elements with custom message`() {31 val arr = arrayOf(1, 2, 3, 4, 5, 6)32 }
Array.shouldExistInOrder
Using AI Code Generation
1array.shouldExistInOrder(3, 4, 5)2array.shouldExistInOrder(2, 4, 5)3array.shouldExistInOrder(2, 3, 5)4array.shouldExistInOrder(2, 3, 4)5array.shouldExistInOrder(3, 5)6array.shouldExistInOrder(2, 5)7array.shouldExistInOrder(2, 4)8array.shouldExistInOrder(4, 5)9array.shouldExistInOrder(3)10array.shouldExistInOrder(2)11array.shouldExistInOrder(5)12array.shouldExistInOrder(4)13array.shouldExistInOrder(3, 4, 5, 6)14array.shouldExistInOrder(2, 4, 5, 6)15array.shouldExistInOrder(2, 3, 5, 6)
Array.shouldExistInOrder
Using AI Code Generation
1 import io.kotest.matchers.collections.shouldExistInOrder2 import io.kotest.matchers.shouldBe3 class ArrayTest {4 fun `test array should exist in order`() {5 val array = arrayOf(1, 2, 3, 4)6 array.shouldExistInOrder(1, 2)7 array.shouldExistInOrder(1, 2, 3, 4)8 array.shouldExistInOrder(1, 2, 3, 4, 5)9 }10 }
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!!