Best Kotest code snippet using com.sksamuel.kotest.eq.IterableEqTest.iterator
IterableEqTest.kt
Source:IterableEqTest.kt
...14import kotlin.time.Duration.Companion.seconds15private class BareIterable(size: Int, offset: Int): Iterable<Int> {16 val top = offset+size17 private var count = offset18 override fun iterator() = object : Iterator<Int> {19 override fun hasNext(): Boolean = count < top20 override fun next(): Int = ++count21 }22 override fun toString(): String = "${this::class.simpleName}@{$top,$count}"23}24private class BareRecursiveIterable(size: Int, offset: Int): Iterable<BareRecursiveIterable> {25 val top = offset+size26 private var count = offset27 override fun iterator() = object : Iterator<BareRecursiveIterable> {28 override fun hasNext(): Boolean = count < top29 override fun next(): BareRecursiveIterable = this@BareRecursiveIterable.apply { ++count }30 }31 override fun toString(): String = "${this::class.simpleName}@{$top,$count}"32}33class IterableEqTest : FunSpec({34 test("should give null for two equal sets") {35 IterableEq.equals(setOf(1, 2, 3), setOf(2, 3, 1)).shouldBeNull()36 }37 test("should give error for unequal sets") {38 val error = IterableEq.equals(setOf(1, 2, 3), setOf(2, 3))39 assertSoftly {40 error.shouldNotBeNull()41 error.message shouldBe "expected:<[2, 3]> but was:<[1, 2, 3]>"42 }43 }44 test("should give null for two equal list") {45 IterableEq.equals(listOf(1, 2, 3), listOf(1, 2, 3)).shouldBeNull()46 }47 test("should give error for two unequal list") {48 val error = IterableEq.equals(listOf(3), listOf(1, 2, 3))49 assertSoftly {50 error.shouldNotBeNull()51 error.message shouldBe """Element differ at index: [0]52 |Missing elements from index 153 |expected:<[1, 2, 3]> but was:<[3]>""".trimMargin()54 }55 }56 test("should not give error for kotlin ordered set comparison with list") {57 val error = IterableEq.equals(setOf(1, 2, 3), listOf(1, 2, 3)).shouldBeNull()58 }59 test("should give error for unordered set comparison with list") {60 val hs = HashSet<Int>(3)61 hs.addAll(setOf(1, 2, 3))62 val error = IterableEq.equals(hs, listOf(1, 2, 3))63 assertSoftly {64 error.shouldNotBeNull()65 error.message shouldBe """Disallowed: Set can be compared only to Set66 |May not compare HashSet with ArrayList67 |expected:<*> but was:<*>""".trimMargin()68 }69 }70 test("should give null for java-only TreeSet comparison with list") {71 val hs = TreeSet(setOf(1, 2, 3))72 IterableEq.equals(hs, listOf(1, 2, 3)).shouldBeNull()73 }74 test("should give error for unmatched collection") {75 val error = IterableEq.equals(BareIterable(3,1), listOf(1,2,3))76 assertSoftly {77 error.shouldNotBeNull()78 error.message shouldBe """Disallowed typed contract79 |May not compare BareIterable with ArrayList80 |expected:<*> but was:<*>""".trimMargin()81 }82 }83 test("should give null for matched iterables") {84 IterableEq.equals(Paths.get("foo"), Paths.get("foo")).shouldBeNull()85 IterableEq.equals(BareIterable(3,2), BareIterable(3,2)).shouldBeNull()86 }87 test("should give error for matched but different iterables") {88 val error = IterableEq.equals(BareIterable(2,2), BareIterable(3,2))89 assertSoftly {90 error.shouldNotBeNull()91 error.message shouldBe """Missing elements from index 292 |expected:<[5]> but was:<[]>""".trimMargin()93 }94 }95 test("should give error for promiscuous iterables") {96 val error = IterableEq.equals(Paths.get("foo"), BareIterable(1,0))97 assertSoftly {98 error.shouldNotBeNull()99 error.message shouldBe """Disallowed promiscuous iterators100 |May not compare UnixPath with BareIterable101 |expected:<*> but was:<*>""".trimMargin()102 }103 }104 test("should give error for promiscuous iterables when recursive") {105 val error = IterableEq.equals(Paths.get("foo"), BareRecursiveIterable(1,0))106 assertSoftly {107 error.shouldNotBeNull()108 error.message shouldBe """Disallowed promiscuous iterators109 |May not compare UnixPath with BareRecursiveIterable110 |expected:<*> but was:<*>""".trimMargin()111 }112 }113 test("should give unsupported error for nested iterables") {114 val error = IterableEq.equals(Paths.get("foo"), Paths.get("bar"))115 assertSoftly {116 error.shouldNotBeNull()117 error.message?.startsWith("Disallowed nesting iterator") shouldBe true118 error.message?.endsWith("; (use custom test code instead)") shouldBe true119 }120 }121 test("should give unsupported error for recursive iterables") {122 val error = IterableEq.equals(BareRecursiveIterable(1,1), BareRecursiveIterable(1,1))123 assertSoftly {124 error.shouldNotBeNull()125 error.message?.startsWith("Disallowed nesting iterator") shouldBe true126 error.message?.endsWith("; (use custom test code instead)") shouldBe true127 }128 }129 test("should give null for equal deeply nested arrays") {130 val array1 = arrayOf(1, arrayOf(arrayOf("a", "c"), "b"), mapOf("a" to arrayOf(1, 2, 3)))131 val array2 = arrayOf(1, arrayOf(arrayOf("a", "c"), "b"), mapOf("a" to arrayOf(1, 2, 3)))132 IterableEq.equals(array1.toList(), array2.toList()).shouldBeNull()133 }134 test("should give null for equal iterables nested in ordered collection") {135 val aut1 = listOf(1, arrayOf(BareIterable(3,3), BareIterable(4,4)), mapOf("a" to arrayOf(1, 2, 3)))136 val aut2 = listOf(1, arrayOf(BareIterable(3,3), BareIterable(4,4)), mapOf("a" to arrayOf(1, 2, 3)))137 IterableEq.equals(aut1, aut2).shouldBeNull()138 }139 test("should give error for unequal iterables nested in ordered collection") {140 val aut1 = listOf(1, listOf(BareIterable(3,3), BareIterable(3,4)), mapOf("a" to listOf(1, 2, 3)))141 val aut2 = listOf(1, listOf(BareIterable(3,3), BareIterable(4,4)), mapOf("a" to listOf(1, 2, 3)))142 val error = IterableEq.equals(aut1, aut2)143 assertSoftly {144 error.shouldNotBeNull()145 error.message shouldBe """Element differ at index: [1]146 |expected:<[1, [[], []], [("a", [1, 2, 3])]]> but was:<[1, [[], []], [("a", [1, 2, 3])]]>""".trimMargin()147 }148 }149 test("should give null for equal iterables nested in Set") {150 val aut1 = setOf(1, listOf(BareIterable(3,3), BareIterable(4,4)), mapOf("a" to arrayOf(1, 2, 3)))151 val aut2 = setOf(mapOf("a" to listOf(1, 2, 3)), 1, arrayOf(BareIterable(3,3), BareIterable(4,4)))152 IterableEq.equals(aut1, aut2).shouldBeNull()153 }154 test("should give error for unequal iterables nested in Set") {155 val aut1 = setOf(listOf(BareIterable(3,3), BareIterable(3,4)), mapOf("a" to listOf(1, 2, 3)), 1)156 val aut2 = setOf(1, listOf(BareIterable(3,3), BareIterable(4,4)), mapOf("a" to listOf(1, 2, 3)))157 val error = IterableEq.equals(aut1, aut2)158 assertSoftly {159 error.shouldNotBeNull()160 error.message shouldBe """expected:<[1, [[], []], [("a", [1, 2, 3])]]> but was:<[[[], []], [("a", [1, 2, 3])], 1]>"""161 }162 }163 test("should give error for unequal deeply nested arrays") {164 val array1 = arrayOf(1, arrayOf(arrayOf("a", "c"), "b"), mapOf("a" to arrayOf(1, 2, 3)))165 val array2 = arrayOf(1, arrayOf(arrayOf("a", "e"), "b"), mapOf("a" to arrayOf(1, 2, 3)))166 val error = IterableEq.equals(array1.toList(), array2.toList())167 assertSoftly {168 error.shouldNotBeNull()169 error.message shouldBe """Element differ at index: [1]170 |expected:<[1, [["a", "e"], "b"], [("a", [1, 2, 3])]]> but was:<[1, [["a", "c"], "b"], [("a", [1, 2, 3])]]>""".trimMargin()171 }172 }173 test("should give error for recursive iterables nested in ordered collection") {174 val aut1 = listOf(1, listOf(BareRecursiveIterable(3,3)), mapOf("a" to listOf(1, 2, 3)))175 val aut2 = listOf(1, listOf(BareRecursiveIterable(3,3)), mapOf("a" to listOf(1, 2, 3)))176 val error = IterableEq.equals(aut1, aut2)177 assertSoftly {178 error.shouldNotBeNull()179 error.message?.startsWith("Disallowed nesting iterator") shouldBe true180 error.message?.endsWith("; (use custom test code instead)") shouldBe true181 }182 }183 test("should give error for recursive iterables nested in Set") {184 val aut1 = setOf(1, arrayOf(BareRecursiveIterable(3,3)), mapOf("a" to listOf(1, 2, 3)))185 val aut2 = setOf(1, mapOf("a" to listOf(1, 2, 3)), listOf(BareRecursiveIterable(3,3)))186 val error = IterableEq.equals(aut1, aut2)187 assertSoftly {188 error.shouldNotBeNull()189 error.message?.startsWith("Disallowed nesting iterator") shouldBe true190 error.message?.endsWith("; (use custom test code instead)") shouldBe true191 }192 }193 test("should only perform one iteration") {194 val actual = IterationCountSet(List(50) { it }.toSet())195 val expected = IterationCountSet(List(50) { it }.toSet())196 actual.count shouldBe 0197 expected.count shouldBe 0198 IterableEq.equals(actual, expected).shouldBeNull()199 actual.count shouldBe 50 // one for each element in the Set200 expected.count shouldBe 0201 }202 test("should return true for deeply nested arrays in sets") {203 setOf(204 arrayOf(1, 2, 3),205 1,206 listOf(arrayOf(1, 2, 3))207 ) shouldBe setOf(208 arrayOf(1, 2, 3),209 1,210 listOf(arrayOf(1, 2, 3))211 )212 }213 test("should have linear performance for lists").config(timeout = 5.seconds) {214 val a = List(10000000) { "foo" }215 val b = List(10000000) { "foo" }216 IterableEq.equals(a, b) shouldBe null217 }218 test("should have linear performance for primitive sets").config(timeout = 5.seconds) {219 IterableEq.equals(List(1000) { it }.toSet(), List(1000) { it }.reversed().toSet()) shouldBe null220 }221 test("should have linear performance for string sets").config(timeout = 5.seconds) {222 IterableEq.equals(223 List(1000) { it.toString() }.toSet(),224 List(1000) { it.toString() }.reversed().toSet()225 ) shouldBe null226 }227 test("should work for empty lists") {228 val errorMessage1 = IterableEq.equals(emptyList<Int>(), listOf(1))?.message229 errorMessage1 shouldBe """Missing elements from index 0230 |expected:<[1]> but was:<[]>""".trimMargin()231 val errorMessage2 = IterableEq.equals(listOf(1, 2), emptyList<Int>())?.message232 errorMessage2 shouldBe """Unexpected elements from index 1233 |expected:<[]> but was:<[1, 2]>""".trimMargin()234 }235 test("shouldNotBe should work for empty lists") {236 listOf("hello") shouldNotBe emptyList<String>()237 emptyList<String>() shouldNotBe listOf("hello")238 }239 test("IterableEq unsupported types") {240 listOf(241 sequenceOf(1),242 ).forAll {243 IterableEq.isValidIterable(it) shouldBe false244 }245 }246 test("IterableEq supported types") {247 listOf(248 setOf(1),249 listOf(1),250 java.util.ArrayList(listOf(1)),251 java.util.HashSet(listOf(1)),252 arrayOf(1),253 ConcurrentLinkedQueue<Int>(),254 BareIterable(0,0),255 Paths.get("foo/bar"),256 ).forAll {257 IterableEq.isValidIterable(it) shouldBe true258 }259 }260})261private class IterationCountSet<T>(val delegate: Set<T>) : Set<T> by delegate {262 var count = 0263 override fun iterator(): Iterator<T> {264 return CountingIterator(delegate.iterator())265 }266 inner class CountingIterator(val delegateIterator: Iterator<T>) : Iterator<T> by delegateIterator {267 override fun next(): T {268 count++269 return delegateIterator.next()270 }271 }272}...
iterator
Using AI Code Generation
1val list1 = listOf(1, 2, 3)2val list2 = listOf(1, 2, 3)3list1.shouldBe(list2)4val list1 = listOf(1, 2, 3)5val list2 = listOf(1, 2, 3)6list1.shouldBe(list2)7val list1 = listOf(1, 2, 3)8val list2 = listOf(1, 2, 3)9list1.shouldBe(list2)10val list1 = listOf(1, 2, 3)11val list2 = listOf(1, 2, 3)12list1.shouldBe(list2)13val list1 = listOf(1, 2, 3)14val list2 = listOf(1, 2, 3)15list1.shouldBe(list2)16val list1 = listOf(1, 2, 3)17val list2 = listOf(1, 2, 3)18list1.shouldBe(list2)19val list1 = listOf(1, 2, 3)20val list2 = listOf(1, 2, 3)21list1.shouldBe(list2)22val list1 = listOf(1, 2, 3)23val list2 = listOf(1, 2, 3)24list1.shouldBe(list2)25val list1 = listOf(1, 2, 3)
iterator
Using AI Code Generation
1 val list = listOf("a", "b", "c")2 list.shouldBe(listOf("a", "b", "c"))3 val list = listOf("a", "b", "c")4 list.shouldBe(listOf("a", "b", "c"))5 val list = listOf("a", "b", "c")6 list.shouldBe(listOf("a", "b", "c"))7 val list = listOf("a", "b", "c")8 list.shouldBe(listOf("a", "b", "c"))9 val list = listOf("a", "b", "c")10 list.shouldBe(listOf("a", "b", "c"))11 val list = listOf("a", "b", "c")12 list.shouldBe(listOf("a", "b", "c"))13 val list = listOf("a", "b", "c")14 list.shouldBe(listOf("a", "b", "c"))15 val list = listOf("a", "b", "c")16 list.shouldBe(listOf("a", "b", "c"))17 val list = listOf("a", "b", "c")18 list.shouldBe(listOf("a", "b", "c"))19 val list = listOf("a", "b", "c")20 list.shouldBe(listOf("a", "b", "c"))
iterator
Using AI Code Generation
1@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()2@JvmName("iteratorMethod") fun <T> Array<T>.iteratorMethod(): Iterator<T> = iterator()3@JvmName("iteratorMethod") fun <T> Sequence<T>.iteratorMethod(): Iterator<T> = iterator()4@JvmName("iteratorMethod") fun <T> Iterator<T>.iteratorMethod(): Iterator<T> = this5@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()6@JvmName("iteratorMethod") fun <T> Array<T>.iteratorMethod(): Iterator<T> = iterator()7@JvmName("iteratorMethod") fun <T> Sequence<T>.iteratorMethod(): Iterator<T> = iterator()8@JvmName("iteratorMethod") fun <T> Iterator<T>.iteratorMethod(): Iterator<T> = this9@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()10@JvmName("iteratorMethod") fun <T> Array<T>.iteratorMethod(): Iterator<T> = iterator()11@JvmName("iteratorMethod") fun <T> Sequence<T>.iteratorMethod(): Iterator<T> = iterator()12@JvmName("iteratorMethod") fun <T> Iterator<T>.iteratorMethod(): Iterator<T> = this13@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()14@JvmName("iteratorMethod") fun <T> Array<T>.iteratorMethod(): Iterator<T> = iterator()15@JvmName("iteratorMethod") fun <T> Sequence<T>.iteratorMethod(): Iterator<T> = iterator()16@JvmName("iteratorMethod") fun <T> Iterator<T>.iteratorMethod(): Iterator<T> = this17@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()18@JvmName("iteratorMethod") fun <T> Array<T>.iteratorMethod(): Iterator<T> = iterator()19@JvmName("iteratorMethod") fun <T> Sequence<T>.iteratorMethod(): Iterator<T> = iterator()20@JvmName("iteratorMethod") fun <T> Iterator<T>.iteratorMethod(): Iterator<T> = this21@JvmName("iteratorMethod") fun <T> Iterable<T>.iteratorMethod(): Iterator<T> = iterator()22@JvmName("iteratorMethod") fun <T> Array<T>.iterator
iterator
Using AI Code Generation
1val list = listOf(1, 2, 3, 4)2list.iterator().shouldNotBeEmpty()3list.iterator().shouldBeEmpty()4val list = listOf(1, 2, 3, 4)5list.iterator().shouldNotBeEmpty()6list.iterator().shouldBeEmpty()7val list = listOf(1, 2, 3, 4)8list.iterator().shouldNotBeEmpty()9list.iterator().shouldBeEmpty()10val list = listOf(1, 2, 3, 4)11list.iterator().shouldNotBeEmpty()12list.iterator().shouldBeEmpty()13val list = listOf(1, 2, 3, 4)14list.iterator().shouldNotBeEmpty()15list.iterator().shouldBeEmpty()16val list = listOf(1, 2, 3, 4)17list.iterator().shouldNotBeEmpty()18list.iterator().shouldBeEmpty()19val list = listOf(1, 2, 3, 4)20list.iterator().shouldNotBeEmpty()21list.iterator().shouldBeEmpty()22val list = listOf(1, 2, 3, 4)23list.iterator().shouldNotBeEmpty()24list.iterator().shouldBeEmpty()25val list = listOf(1, 2, 3, 4)26list.iterator().shouldNotBeEmpty()27list.iterator().shouldBeEmpty()28val list = listOf(1, 2, 3, 4)29list.iterator().shouldNotBeEmpty()30list.iterator().shouldBeEmpty()
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!!