Best Kotest code snippet using com.sksamuel.kotest.matchers.collections.ShouldContainExactlyTest
ShouldContainExactlyTest.kt
Source:ShouldContainExactlyTest.kt
...14import io.kotest.matchers.throwable.shouldHaveMessage15import java.io.File16import java.nio.file.Path17import java.nio.file.Paths18class ShouldContainExactlyTest : WordSpec() {19 init {20 "containExactly" should {21 "test that an array contains given elements exactly" {22 val actual = arrayOf(1, 2, 3)23 actual.shouldContainExactly(1, 2, 3)24 actual shouldContainExactly arrayOf(1, 2, 3)25 actual.shouldNotContainExactly(3, 2, 1)26 actual shouldNotContainExactly arrayOf(3, 2, 1)27 shouldThrow<AssertionError> {28 actual.shouldContainExactly(3, 2, 1)29 }30 shouldThrow<AssertionError> {31 actual shouldContainExactly arrayOf(3, 2, 1)32 }33 shouldThrow<AssertionError> {34 actual.shouldNotContainExactly(1, 2, 3)35 }36 shouldThrow<AssertionError> {37 actual shouldNotContainExactly arrayOf(1, 2, 3)38 }39 val actualNull: Array<Int>? = null40 shouldThrow<AssertionError> {41 actualNull.shouldContainExactly(1, 2, 3)42 }.shouldHaveMessage("Expecting actual not to be null")43 shouldThrow<AssertionError> {44 actualNull.shouldNotContainExactly()45 }.shouldHaveMessage("Expecting actual not to be null")46 }47 "test that a collection contains given elements exactly" {48 val actual = listOf(1, 2, 3)49 emptyList<Int>() should containExactly()50 actual should containExactly(1, 2, 3)51 actual.shouldContainExactly(1, 2, 3)52 actual.toSet().shouldContainExactly(linkedSetOf(1, 2, 3))53 actual shouldNot containExactly(1, 2)54 actual.shouldNotContainExactly(3, 2, 1)55 actual.shouldNotContainExactly(listOf(5, 6, 7))56 shouldThrow<AssertionError> {57 actual should containExactly(1, 2)58 }59 shouldThrow<AssertionError> {60 actual should containExactly(1, 2, 3, 4)61 }62 shouldThrow<AssertionError> {63 actual.shouldContainExactly(3, 2, 1)64 }65 }66 "test contains exactly for byte arrays" {67 listOf("hello".toByteArray()) shouldContainExactly listOf("hello".toByteArray())68 listOf("helloworld".toByteArray()) shouldNotContainExactly listOf("hello".toByteArray())69 }70 "print errors unambiguously" {71 shouldThrow<AssertionError> {72 listOf<Any>(1L, 2L).shouldContainExactly(listOf<Any>(1, 2))73 } shouldHaveMessage74 """75 |Expecting: [1, 2] but was: [1L, 2L]76 |Some elements were missing: [1, 2] and some elements were unexpected: [1L, 2L]77 |expected:<[1, 2]> but was:<[1L, 2L]>78 """.trimMargin()79 }80 "informs user of illegal comparisons" {81 shouldFail {82 HashSet(setOf(1, 2, 3)) shouldContainExactly listOf(1, 2, 3)83 }.message shouldBe """Disallowed: Set can be compared only to Set84 |May not compare HashSet with ArrayList85 |expected:<*> but was:<*>86 |87 |""".trimMargin()88 }89 "print dataclasses" {90 shouldThrow<AssertionError> {91 listOf(92 Blonde("foo", true, 23423, inputPath),93 Blonde("woo", true, 97821, inputPath),94 Blonde("goo", true, 51984, inputPath)95 ).shouldContainExactly(96 Blonde("foo", true, 23423, inputPath),97 Blonde("woo", true, 97821, inputPath)98 )99 }.message?.trim() shouldBe100 """101 |Expecting: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath)] but was: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath), Blonde(a=goo, b=true, c=51984, p=$expectedPath)]102 |Some elements were unexpected: [Blonde(a=goo, b=true, c=51984, p=$expectedPath)]103 |expected:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath)]> but was:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath), Blonde(a=goo, b=true, c=51984, p=$expectedPath)]>104 """.trimMargin()105 }106 "include extras when too many" {107 shouldThrow<AssertionError> {108 listOf(109 Blonde("foo", true, 23423, inputPath)110 ).shouldContainExactly(111 Blonde("foo", true, 23423, inputPath),112 Blonde("woo", true, 97821, inputPath)113 )114 }.message?.trim() shouldBe115 """116 |Expecting: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath)] but was: [Blonde(a=foo, b=true, c=23423, p=$expectedPath)]117 |Some elements were missing: [Blonde(a=woo, b=true, c=97821, p=$expectedPath)]118 |expected:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=woo, b=true, c=97821, p=$expectedPath)]> but was:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath)]>119 """.trimMargin()120 }121 "include missing when too few" {122 shouldThrow<AssertionError> {123 listOf(124 Blonde("foo", true, 23423, inputPath),125 Blonde("hoo", true, 96915, inputPath)126 ).shouldContainExactly(127 Blonde("woo", true, 97821, inputPath)128 )129 }.message?.trim() shouldBe130 """131 |Expecting: [Blonde(a=woo, b=true, c=97821, p=$expectedPath)] but was: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]132 |Some elements were missing: [Blonde(a=woo, b=true, c=97821, p=$expectedPath)] and some elements were unexpected: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]133 |expected:<[Blonde(a=woo, b=true, c=97821, p=$expectedPath)]> but was:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]>134 """.trimMargin()135 }136 "include missing and extras when not the right amount" {137 shouldThrow<AssertionError> {138 listOf(139 Blonde("foo", true, 23423, inputPath),140 Blonde("hoo", true, 96915, inputPath)141 ).shouldContainExactly(142 Blonde("woo", true, 97821, inputPath),143 Blonde("goo", true, 51984, inputPath)144 )145 }.message?.trim() shouldBe146 """147 |Expecting: [Blonde(a=woo, b=true, c=97821, p=$expectedPath), Blonde(a=goo, b=true, c=51984, p=$expectedPath)] but was: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]148 |Some elements were missing: [Blonde(a=woo, b=true, c=97821, p=$expectedPath), Blonde(a=goo, b=true, c=51984, p=$expectedPath)] and some elements were unexpected: [Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]149 |expected:<[Blonde(a=woo, b=true, c=97821, p=$expectedPath), Blonde(a=goo, b=true, c=51984, p=$expectedPath)]> but was:<[Blonde(a=foo, b=true, c=23423, p=$expectedPath), Blonde(a=hoo, b=true, c=96915, p=$expectedPath)]>150 """.trimMargin()151 }152 }153 "containExactlyInAnyOrder" should {154 "test that a collection contains given elements exactly in any order" {155 val actual = listOf(1, 2, 3)156 actual should containExactlyInAnyOrder(1, 2, 3)157 actual.shouldContainExactlyInAnyOrder(3, 2, 1)158 actual.shouldContainExactlyInAnyOrder(linkedSetOf(2, 1, 3))159 actual shouldNot containExactlyInAnyOrder(1, 2)160 actual.shouldNotContainExactlyInAnyOrder(1, 2, 3, 4)161 actual.shouldNotContainExactlyInAnyOrder(listOf(5, 6, 7))162 actual.shouldNotContainExactlyInAnyOrder(1, 1, 1)163 actual.shouldNotContainExactlyInAnyOrder(listOf(2, 2, 3))164 actual.shouldNotContainExactlyInAnyOrder(listOf(1, 1, 2, 3))165 val actualDuplicates = listOf(1, 1, 2)166 actualDuplicates.shouldContainExactlyInAnyOrder(1, 2, 1)167 actualDuplicates.shouldContainExactlyInAnyOrder(2, 1, 1)168 actualDuplicates.shouldNotContainExactlyInAnyOrder(1, 2)169 actualDuplicates.shouldNotContainExactlyInAnyOrder(1, 2, 2)170 actualDuplicates.shouldNotContainExactlyInAnyOrder(1, 1, 2, 2)171 actualDuplicates.shouldNotContainExactlyInAnyOrder(1, 2, 7)172 shouldThrow<AssertionError> {173 actual should containExactlyInAnyOrder(1, 2)174 }175 shouldThrow<AssertionError> {176 actual should containExactlyInAnyOrder(1, 2, 3, 4)177 }178 shouldThrow<AssertionError> {179 actual should containExactlyInAnyOrder(1, 1, 1)180 }181 shouldThrow<AssertionError> {182 actual should containExactlyInAnyOrder(1, 1, 2, 3)183 }184 shouldThrow<AssertionError> {185 actualDuplicates should containExactlyInAnyOrder(1, 2, 2)186 }187 }188 "print errors unambiguously" {189 shouldThrow<AssertionError> {190 listOf<Any>(1L, 2L).shouldContainExactlyInAnyOrder(listOf<Any>(1, 2))191 }.shouldHaveMessage("Collection should contain [1, 2] in any order, but was [1L, 2L]")192 }193 "disambiguate when using optional expected value" {194 val actual: List<String> = listOf("A", "B", "C")195 val expected: List<String>? = listOf("A", "B", "C")196 actual.shouldContainExactlyInAnyOrder(expected)197 }198 }199 }200 companion object {201 /** Note: [Path.toString] is platform-dependent, as the path separator is202 * `\` on Windows or `/` on Unix. There's no easy way to configure this.203 *204 * Tests in [ShouldContainExactlyTest] depends on result of `toString()`,205 * so use [expectedPath] instead of expecting a raw String `a/b/c` (this206 * will fail on Windows).207 */208 val inputPath: Path = Paths.get("a/b/c")209 /** The expected result of [inputPath]`.toString()` (with Windows or Unix path separators) */210 val expectedPath = listOf("a", "b", "c").joinToString(File.separator)211 }212}213data class Blonde(val a: String, val b: Boolean, val c: Int, val p: Path)...
ShouldContainExactlyTest
Using AI Code Generation
1import com.sksamuel.kotest.matchers.collections.shouldContainExactly2import com.sksamuel.kotest.matchers.collections.shouldContainExactly3import com.sksamuel.kotest.matchers.collections.shouldContainExactly4import com.sksamuel.kotest.matchers.collections.shouldContainExactly5import com.sksamuel.kotest.matchers.collections.shouldContainExactly6import com.sksamuel.kotest.matchers.collections.shouldContainExactly7import com.sksamuel.kotest.matchers.collections.shouldContainExactly8import com.sksamuel.kotest.matchers.collections.shouldContainExactly9import com.sksamuel.kotest.matchers.collections.shouldContainExactly10import com.sksamuel.kotest.matchers.collections.shouldContainExactly11import com.sksamuel.kotest.matchers.collections.shouldContainExactly12import com.sksamuel.kotest.matchers.collections.shouldContainExactly13import com.sksamuel.kotest.matchers.collections.shouldContainExactly14import com.sksamuel.kotest.matchers.collections.shouldContainExactly
ShouldContainExactlyTest
Using AI Code Generation
1val list = listOf(1, 2, 3, 4, 5, 6)2list.shouldContainExactly(1, 2, 3, 4, 5, 6)3val list = listOf(1, 2, 3, 4, 5, 6)4list.shouldContainExactlyInAnyOrder(1, 2, 3, 4, 5, 6)5val list = listOf(1, 2, 3, 4, 5, 6)6list.shouldContainExactlyInAnyOrder(6, 5, 4, 3, 2, 1)7val list = listOf(1, 2, 3, 4, 5, 6)8list.shouldContainExactly(1, 2, 3, 4, 5, 6)9val list = listOf(1, 2, 3, 4, 5, 6)10list.shouldContainExactlyInAnyOrder(1, 2, 3, 4, 5, 6)
ShouldContainExactlyTest
Using AI Code Generation
1 import com.sksamuel.kotest.matchers.collections.shouldContainExactly2 import com.sksamuel.kotest.matchers.types.shouldBe3 import com.sksamuel.kotest.matchers.types.shouldBeInstanceOf4 import com.sksamuel.kotest.matchers.types.shouldBeSameInstanceAs5 import com.sksamuel.kotest.matchers.collections.shouldContain6 import com.sksamuel.kotest.matchers.collections.shouldContainAll7 import com.sksamuel.kotest.matchers.numerics.shouldBeGreaterThan8 import com.sksamuel.kotest.matchers.numerics.shouldBeGreaterThanOrEqual9 import com.sksamuel.kotest.matchers.numerics.shouldBeLessThan10 import com.sksamuel.kotest.matchers.numerics.shouldBeLessThanOrEqual11 import com.sksamuel.kotest.matchers.numerics.shouldBeBetween12 import com.sksamuel.kotest.matchers.numerics.shouldBeBetweenInclusive
ShouldContainExactlyTest
Using AI Code Generation
1import com.sksamuel.kotest.matchers.collections.shouldContainExactly2import io.kotest.core.spec.style.StringSpec3class ShouldContainExactlyTest : StringSpec() {4 init {5 "should contain exactly test" {6 val list = listOf(1, 2, 3, 4, 5)7 list shouldContainExactly listOf(1, 2, 3, 4, 5)8 }9 }10}
ShouldContainExactlyTest
Using AI Code Generation
1import io.kotest.matchers.Matcher2import io.kotest.matchers.MatcherResult3import io.kotest.matchers.should4import io.kotest.matchers.shouldNot5infix fun <T> List<T>.shouldContainExactly(other: List<T>) = this should containExactly(other)6infix fun <T> List<T>.shouldNotContainExactly(other: List<T>) = this shouldNot containExactly(other)7fun <T> containExactly(other: List<T>): Matcher<List<T>> = object : Matcher<List<T>> {8 override fun test(value: List<T>): MatcherResult {9 val missing = other.filter { !value.contains(it) }10 val extra = value.filter { !other.contains(it) }11 return MatcherResult(12 missing.isEmpty() && extra.isEmpty(),
ShouldContainExactlyTest
Using AI Code Generation
1class Person ( val name : String , val age : Int )2fun main () {3 val people = listOf ( Person ( "sammy" , 30 ), Person ( "joe" , 20 ))4 people shouldContainExactly listOf ( Person ( "joe" , 20 ), Person ( "sammy" , 30 ))5}6java.lang.AssertionError: [ Person ( name = sammy , age = 30 ) , Person ( name = joe , age = 20 ) ] should contain exactly [ Person ( name = joe , age = 20 ) , Person ( name = sammy , age = 30 ) ] but had extra elements [ Person ( name = sammy , age = 30 ) ] and missing elements [ Person ( name = sammy , age = 30 ) ]7java.lang.AssertionError: [ Person ( name = sammy , age = 30 ) , Person ( name = joe , age = 20 ) ] should contain exactly [ Person ( name = joe , age = 20 ) , Person ( name = sammy , age = 30 ) ] but had extra elements [ Person ( name = joe , age = 20 ) ] and missing elements [ Person ( name = joe , age = 20 ) ]8java.lang.AssertionError: [ Person ( name = sammy , age = 30 ) , Person ( name = joe , age = 20 ) ] should contain exactly [ Person ( name = joe , age = 20 ) , Person ( name = sammy , age = 30 ) ] but had extra elements [ Person ( name = sammy , age = 30 ) ] and missing elements [ Person ( name = joe , age = 20 ) ]9java.lang.AssertionError: [ Person ( name = sammy , age = 30 ) , Person ( name = joe , age = 20 ) ] should contain exactly [ Person (
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!!