How to use Sequence.shouldBeEmpty method of io.kotest.matchers.sequences.matchers class

Best Kotest code snippet using io.kotest.matchers.sequences.matchers.Sequence.shouldBeEmpty

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.sequences2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.neverNullMatcher5import io.kotest.matchers.should6import io.kotest.matchers.shouldHave7import io.kotest.matchers.shouldNot8private fun <T> Sequence<T>.toString(limit: Int = 10) = this.joinToString(", ", limit = limit)9/*10How should infinite sequences be detected, and how should they be dealt with?11Sequence<T>.count() may run through the whole sequence (sequences from `generateSequence()` do so), so isn't always a safe way to detect infinite sequences.12For now, the documentation should mention that infinite sequences will cause these matchers never to halt.13*/14fun <T> Sequence<T>.shouldContainOnlyNulls() = this should containOnlyNulls()15fun <T> Sequence<T>.shouldNotContainOnlyNulls() = this shouldNot containOnlyNulls()16fun <T> containOnlyNulls() = object : Matcher<Sequence<T>> {17 override fun test(value: Sequence<T>) =18 MatcherResult(19 value.all { it == null },20 "Sequence should contain only nulls",21 "Sequence should not contain only nulls"22 )23}24fun <T> Sequence<T>.shouldContainNull() = this should containNull()25fun <T> Sequence<T>.shouldNotContainNull() = this shouldNot containNull()26fun <T> containNull() = object : Matcher<Sequence<T>> {27 override fun test(value: Sequence<T>) =28 MatcherResult(29 value.any { it == null },30 "Sequence should contain at least one null",31 "Sequence should not contain any nulls"32 )33}34fun <T> Sequence<T>.shouldHaveElementAt(index: Int, element: T) = this should haveElementAt(index, element)35fun <T> Sequence<T>.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)36fun <T, S : Sequence<T>> haveElementAt(index: Int, element: T) = object : Matcher<S> {37 override fun test(value: S) =38 MatcherResult(39 value.elementAt(index) == element,40 { "Sequence should contain $element at index $index" },41 { "Sequence should not contain $element at index $index" }42 )43}44fun <T> Sequence<T>.shouldContainNoNulls() = this should containNoNulls()45fun <T> Sequence<T>.shouldNotContainNoNulls() = this shouldNot containNoNulls()46fun <T> containNoNulls() = object : Matcher<Sequence<T>> {47 override fun test(value: Sequence<T>) =48 MatcherResult(49 value.all { it != null },50 { "Sequence should not contain nulls" },51 { "Sequence should have at least one null" }52 )53}54infix fun <T, C : Sequence<T>> C.shouldContain(t: T) = this should contain(t)55infix fun <T, C : Sequence<T>> C.shouldNotContain(t: T) = this shouldNot contain(t)56fun <T, C : Sequence<T>> contain(t: T) = object : Matcher<C> {57 override fun test(value: C) = MatcherResult(58 value.contains(t),59 { "Sequence should contain element $t" },60 { "Sequence should not contain element $t" }61 )62}63infix fun <T, C : Sequence<T>> C?.shouldNotContainExactly(expected: C) = this shouldNot containExactly(expected)64fun <T, C : Sequence<T>> C?.shouldNotContainExactly(vararg expected: T) = this shouldNot containExactly(*expected)65infix fun <T, C : Sequence<T>> C?.shouldContainExactly(expected: C) = this should containExactly(expected)66fun <T, C : Sequence<T>> C?.shouldContainExactly(vararg expected: T) = this should containExactly(*expected)67fun <T> containExactly(vararg expected: T): Matcher<Sequence<T>?> = containExactly(expected.asSequence())68/** Assert that a sequence contains exactly the given values and nothing else, in order. */69fun <T, C : Sequence<T>> containExactly(expected: C): Matcher<C?> = neverNullMatcher { value ->70 var passed: Boolean = value.count() == expected.count()71 var failMessage = "Sequence should contain exactly $expected but was $value"72 if (passed) {73 val diff = value.zip(expected) { a, b -> Triple(a, b, a == b) }.withIndex().find { !it.value.third }74 if (diff != null) {75 passed = false76 failMessage += " (expected ${diff.value.second} at ${diff.index} but found ${diff.value.first})"77 }78 } else {79 failMessage += " (expected ${expected.count()} elements but found ${value.count()})"80 }81 MatcherResult(82 passed,83 failMessage,84 "Sequence should not be exactly $expected"85 )86}87@Deprecated("use shouldNotContainAllInAnyOrder", ReplaceWith("shouldNotContainAllInAnyOrder"))88infix fun <T, C : Sequence<T>> C?.shouldNotContainExactlyInAnyOrder(expected: C) =89 this shouldNot containAllInAnyOrder(expected)90@Deprecated("use shouldNotContainAllInAnyOrder", ReplaceWith("shouldNotContainAllInAnyOrder"))91fun <T, C : Sequence<T>> C?.shouldNotContainExactlyInAnyOrder(vararg expected: T) =92 this shouldNot containAllInAnyOrder(*expected)93@Deprecated("use shouldContainAllInAnyOrder", ReplaceWith("shouldContainAllInAnyOrder"))94infix fun <T, C : Sequence<T>> C?.shouldContainExactlyInAnyOrder(expected: C) =95 this should containAllInAnyOrder(expected)96@Deprecated("use shouldContainAllInAnyOrder", ReplaceWith("shouldContainAllInAnyOrder"))97fun <T, C : Sequence<T>> C?.shouldContainExactlyInAnyOrder(vararg expected: T) =98 this should containAllInAnyOrder(*expected)99@Deprecated("use containAllInAnyOrder", ReplaceWith("containAllInAnyOrder"))100fun <T> containExactlyInAnyOrder(vararg expected: T): Matcher<Sequence<T>?> =101 containAllInAnyOrder(expected.asSequence())102@Deprecated("use containAllInAnyOrder", ReplaceWith("containAllInAnyOrder"))103/** Assert that a sequence contains the given values and nothing else, in any order. */104fun <T, C : Sequence<T>> containExactlyInAnyOrder(expected: C): Matcher<C?> = containAllInAnyOrder(expected)105infix fun <T, C : Sequence<T>> C?.shouldNotContainAllInAnyOrder(expected: C) =106 this shouldNot containAllInAnyOrder(expected)107fun <T, C : Sequence<T>> C?.shouldNotContainAllInAnyOrder(vararg expected: T) =108 this shouldNot containAllInAnyOrder(*expected)109infix fun <T, C : Sequence<T>> C?.shouldContainAllInAnyOrder(expected: C) =110 this should containAllInAnyOrder(expected)111fun <T, C : Sequence<T>> C?.shouldContainAllInAnyOrder(vararg expected: T) =112 this should containAllInAnyOrder(*expected)113fun <T> containAllInAnyOrder(vararg expected: T): Matcher<Sequence<T>?> =114 containAllInAnyOrder(expected.asSequence())115/** Assert that a sequence contains all the given values and nothing else, in any order. */116fun <T, C : Sequence<T>> containAllInAnyOrder(expected: C): Matcher<C?> = neverNullMatcher { value ->117 val passed = value.count() == expected.count() && expected.all { value.contains(it) }118 MatcherResult(119 passed,120 { "Sequence should contain the values of $expected in any order, but was $value" },121 { "Sequence should not contain the values of $expected in any order" }122 )123}124infix fun <T : Comparable<T>, C : Sequence<T>> C.shouldHaveUpperBound(t: T) = this should haveUpperBound(t)125fun <T : Comparable<T>, C : Sequence<T>> haveUpperBound(t: T) = object : Matcher<C> {126 override fun test(value: C) = MatcherResult(127 (value.maxOrNull() ?: t) <= t,128 { "Sequence should have upper bound $t" },129 { "Sequence should not have upper bound $t" }130 )131}132infix fun <T : Comparable<T>, C : Sequence<T>> C.shouldHaveLowerBound(t: T) = this should haveLowerBound(t)133fun <T : Comparable<T>, C : Sequence<T>> haveLowerBound(t: T) = object : Matcher<C> {134 override fun test(value: C) = MatcherResult(135 (value.minOrNull() ?: t) >= t,136 { "Sequence should have lower bound $t" },137 { "Sequence should not have lower bound $t" }138 )139}140fun <T> Sequence<T>.shouldBeUnique() = this should beUnique()141fun <T> Sequence<T>.shouldNotBeUnique() = this shouldNot beUnique()142fun <T> beUnique() = object : Matcher<Sequence<T>> {143 override fun test(value: Sequence<T>) = MatcherResult(144 value.toSet().size == value.count(),145 { "Sequence should be Unique" },146 { "Sequence should contain at least one duplicate element" }147 )148}149fun <T> Sequence<T>.shouldContainDuplicates() = this should containDuplicates()150fun <T> Sequence<T>.shouldNotContainDuplicates() = this shouldNot containDuplicates()151fun <T> containDuplicates() = object : Matcher<Sequence<T>> {152 override fun test(value: Sequence<T>) = MatcherResult(153 value.toSet().size < value.count(),154 { "Sequence should contain duplicates" },155 { "Sequence should not contain duplicates" }156 )157}158fun <T : Comparable<T>> Sequence<T>.shouldBeSorted() = this should beSorted()159fun <T : Comparable<T>> Sequence<T>.shouldNotBeSorted() = this shouldNot beSorted()160fun <T : Comparable<T>> beSorted(): Matcher<Sequence<T>> = sorted()161fun <T : Comparable<T>> sorted(): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {162 override fun test(value: Sequence<T>): MatcherResult {163 @Suppress("UNUSED_DESTRUCTURED_PARAMETER_ENTRY")164 val failure = value.zipWithNext().withIndex().firstOrNull { (i, it) -> it.first > it.second }165 val snippet = value.joinToString(",", limit = 10)166 val elementMessage = when (failure) {167 null -> ""168 else -> ". Element ${failure.value.first} at index ${failure.index} was greater than element ${failure.value.second}"169 }170 return MatcherResult(171 failure == null,172 { "Sequence $snippet should be sorted$elementMessage" },173 { "Sequence $snippet should not be sorted" }174 )175 }176}177infix fun <T> Sequence<T>.shouldBeSortedWith(comparator: Comparator<in T>) = this should beSortedWith(comparator)178infix fun <T> Sequence<T>.shouldBeSortedWith(cmp: (T, T) -> Int) = this should beSortedWith(cmp)179fun <T> beSortedWith(comparator: Comparator<in T>): Matcher<Sequence<T>> = sortedWith(comparator)180fun <T> beSortedWith(cmp: (T, T) -> Int): Matcher<Sequence<T>> = sortedWith(cmp)181fun <T> sortedWith(comparator: Comparator<in T>): Matcher<Sequence<T>> = sortedWith { a, b ->182 comparator.compare(a, b)183}184fun <T> sortedWith(cmp: (T, T) -> Int): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {185 override fun test(value: Sequence<T>): MatcherResult {186 @Suppress("UNUSED_DESTRUCTURED_PARAMETER_ENTRY")187 val failure = value.zipWithNext().withIndex().firstOrNull { (i, it) -> cmp(it.first, it.second) > 0 }188 val snippet = value.joinToString(",", limit = 10)189 val elementMessage = when (failure) {190 null -> ""191 else -> ". Element ${failure.value.first} at index ${failure.index} shouldn't precede element ${failure.value.second}"192 }193 return MatcherResult(194 failure == null,195 { "Sequence $snippet should be sorted$elementMessage" },196 { "Sequence $snippet should not be sorted" }197 )198 }199}200infix fun <T> Sequence<T>.shouldNotBeSortedWith(comparator: Comparator<in T>) = this shouldNot beSortedWith(comparator)201infix fun <T> Sequence<T>.shouldNotBeSortedWith(cmp: (T, T) -> Int) = this shouldNot beSortedWith(cmp)202infix fun <T> Sequence<T>.shouldHaveSingleElement(t: T) = this should singleElement(t)203infix fun <T> Sequence<T>.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)204fun <T> singleElement(t: T) = object : Matcher<Sequence<T>> {205 override fun test(value: Sequence<T>) = MatcherResult(206 value.count() == 1 && value.first() == t,207 { "Sequence should be a single element of $t but has ${value.count()} elements" },208 { "Sequence should not be a single element of $t" }209 )210}211infix fun <T> Sequence<T>.shouldHaveCount(count: Int) = this should haveCount(count)212infix fun <T> Sequence<T>.shouldNotHaveCount(count: Int) = this shouldNot haveCount(213 count)214infix fun <T> Sequence<T>.shouldHaveSize(size: Int) = this should haveCount(size)215infix fun <T> Sequence<T>.shouldNotHaveSize(size: Int) = this shouldNot haveCount(size)216//fun <T> haveSize(size: Int) = haveCount(size)217fun <T> haveSize(size: Int): Matcher<Sequence<T>> = haveCount(size)218fun <T> haveCount(count: Int): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {219 override fun test(value: Sequence<T>) =220 MatcherResult(221 value.count() == count,222 { "Sequence should have count $count but has count ${value.count()}" },223 { "Sequence should not have count $count" }224 )225}226infix fun <T, U> Sequence<T>.shouldBeLargerThan(other: Sequence<U>) = this should beLargerThan(other)227fun <T, U> beLargerThan(other: Sequence<U>) = object : Matcher<Sequence<T>> {228 override fun test(value: Sequence<T>) = MatcherResult(229 value.count() > other.count(),230 { "Sequence of count ${value.count()} should be larger than sequence of count ${other.count()}" },231 { "Sequence of count ${value.count()} should not be larger than sequence of count ${other.count()}" }232 )233}234infix fun <T, U> Sequence<T>.shouldBeSmallerThan(other: Sequence<U>) = this should beSmallerThan(other)235fun <T, U> beSmallerThan(other: Sequence<U>) = object : Matcher<Sequence<T>> {236 override fun test(value: Sequence<T>) = MatcherResult(237 value.count() < other.count(),238 { "Sequence of count ${value.count()} should be smaller than sequence of count ${other.count()}" },239 { "Sequence of count ${value.count()} should not be smaller than sequence of count ${other.count()}" }240 )241}242infix fun <T, U> Sequence<T>.shouldBeSameCountAs(other: Sequence<U>) = this should beSameCountAs(243 other)244fun <T, U> beSameCountAs(other: Sequence<U>) = object : Matcher<Sequence<T>> {245 override fun test(value: Sequence<T>) = MatcherResult(246 value.count() == other.count(),247 { "Sequence of count ${value.count()} should be the same count as sequence of count ${other.count()}" },248 { "Sequence of count ${value.count()} should not be the same count as sequence of count ${other.count()}" }249 )250}251infix fun <T, U> Sequence<T>.shouldBeSameSizeAs(other: Sequence<U>) = this.shouldBeSameCountAs(other)252infix fun <T> Sequence<T>.shouldHaveAtLeastCount(n: Int) = this shouldHave atLeastCount(n)253fun <T> atLeastCount(n: Int) = object : Matcher<Sequence<T>> {254 override fun test(value: Sequence<T>) = MatcherResult(255 value.count() >= n,256 { "Sequence should contain at least $n elements" },257 { "Sequence should contain less than $n elements" }258 )259}260infix fun <T> Sequence<T>.shouldHaveAtLeastSize(n: Int) = this.shouldHaveAtLeastCount(n)261infix fun <T> Sequence<T>.shouldHaveAtMostCount(n: Int) = this shouldHave atMostCount(n)262fun <T> atMostCount(n: Int) = object : Matcher<Sequence<T>> {263 override fun test(value: Sequence<T>) = MatcherResult(264 value.count() <= n,265 { "Sequence should contain at most $n elements" },266 { "Sequence should contain more than $n elements" }267 )268}269infix fun <T> Sequence<T>.shouldHaveAtMostSize(n: Int) = this shouldHave atMostCount(n)270infix fun <T> Sequence<T>.shouldExist(p: (T) -> Boolean) = this should exist(p)271fun <T> exist(p: (T) -> Boolean) = object : Matcher<Sequence<T>> {272 override fun test(value: Sequence<T>) = MatcherResult(273 value.any { p(it) },274 { "Sequence should contain an element that matches the predicate $p" },275 { "Sequence should not contain an element that matches the predicate $p" }276 )277}278fun <T : Comparable<T>> Sequence<T>.shouldContainInOrder(vararg ts: T) =279 this should containsInOrder(ts.asSequence())280infix fun <T : Comparable<T>> Sequence<T>.shouldContainInOrder(expected: Sequence<T>) =281 this should containsInOrder(expected)282fun <T : Comparable<T>> Sequence<T>.shouldNotContainInOrder(expected: Sequence<T>) =283 this shouldNot containsInOrder(expected)284/** Assert that a sequence contains a given subsequence, possibly with values in between. */285fun <T> containsInOrder(subsequence: Sequence<T>): Matcher<Sequence<T>?> = neverNullMatcher { actual ->286 val subsequenceCount = subsequence.count()287 require(subsequenceCount > 0) { "expected values must not be empty" }288 var subsequenceIndex = 0289 val actualIterator = actual.iterator()290 while (actualIterator.hasNext() && subsequenceIndex < subsequenceCount) {291 if (actualIterator.next() == subsequence.elementAt(subsequenceIndex)) subsequenceIndex += 1292 }293 MatcherResult(294 subsequenceIndex == subsequence.count(),295 { "[$actual] did not contain the elements [$subsequence] in order" },296 { "[$actual] should not contain the elements [$subsequence] in order" }297 )298}299fun <T> Sequence<T>.shouldBeEmpty() = this should beEmpty()300fun <T> Sequence<T>.shouldNotBeEmpty() = this shouldNot beEmpty()301fun <T> beEmpty(): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {302 override fun test(value: Sequence<T>): MatcherResult = MatcherResult(303 value.count() == 0,304 { "Sequence should be empty" },305 { "Sequence should not be empty" }306 )307}308fun <T> Sequence<T>.shouldContainAll(vararg ts: T) = this should containAll(ts.asSequence())309infix fun <T> Sequence<T>.shouldContainAll(ts: Collection<T>) = this should containAll(ts.asSequence())310infix fun <T> Sequence<T>.shouldContainAll(ts: Sequence<T>) = this should containAll(ts)311fun <T> Sequence<T>.shouldNotContainAll(vararg ts: T) = this shouldNot containAll(ts.asSequence())312infix fun <T> Sequence<T>.shouldNotContainAll(ts: Collection<T>) = this shouldNot containAll(ts.asSequence())313infix fun <T> Sequence<T>.shouldNotContainAll(ts: Sequence<T>) = this shouldNot containAll(ts)314fun <T, C : Sequence<T>> containAll(ts: C): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {315 override fun test(value: Sequence<T>): MatcherResult = MatcherResult(316 ts.all { value.contains(it) },317 { "Sequence should contain all of ${value.joinToString(",", limit = 10)}" },318 { "Sequence should not contain all of ${value.joinToString(",", limit = 10)}" }319 )320}...

Full Screen

Full Screen

ReplTests.kt

Source:ReplTests.kt Github

copy

Full Screen

1package org.jetbrains.kotlinx.jupyter.test.repl2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.matchers.booleans.shouldBeTrue5import io.kotest.matchers.collections.shouldBeEmpty6import io.kotest.matchers.collections.shouldHaveAtLeastSize7import io.kotest.matchers.nulls.shouldNotBeNull8import io.kotest.matchers.sequences.shouldBeEmpty9import io.kotest.matchers.sequences.shouldHaveSize10import io.kotest.matchers.shouldBe11import io.kotest.matchers.string.shouldContain12import io.kotest.matchers.string.shouldNotContain13import io.kotest.matchers.types.shouldBeInstanceOf14import jupyter.kotlin.JavaRuntime15import kotlinx.coroutines.runBlocking16import kotlinx.serialization.json.Json17import kotlinx.serialization.serializer18import org.jetbrains.kotlinx.jupyter.OutputConfig19import org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException20import org.jetbrains.kotlinx.jupyter.exceptions.ReplEvalRuntimeException21import org.jetbrains.kotlinx.jupyter.generateDiagnostic22import org.jetbrains.kotlinx.jupyter.generateDiagnosticFromAbsolute23import org.jetbrains.kotlinx.jupyter.repl.CompletionResult24import org.jetbrains.kotlinx.jupyter.repl.ListErrorsResult25import org.jetbrains.kotlinx.jupyter.test.getOrFail26import org.jetbrains.kotlinx.jupyter.withPath27import org.junit.jupiter.api.Test28import java.io.File29import java.nio.file.Path30import kotlin.script.experimental.api.SourceCode31class ReplTests : AbstractSingleReplTest() {32 override val repl = makeSimpleRepl()33 @Test34 fun testRepl() {35 eval("val x = 3")36 val res = eval("x*2")37 res.resultValue shouldBe 638 }39 @Test40 fun testPropertiesGeneration() {41 // Note, this test should actually fail with ReplEvalRuntimeException, but 'cause of eval/compile42 // histories are out of sync, it fails with another exception. This test shows the wrong behavior and43 // should be fixed after fixing https://youtrack.jetbrains.com/issue/KT-3639744 // In fact, this shouldn't compile, but because of bug in compiler it fails in runtime45 shouldThrow<ReplCompilerException> {46 eval(47 """48 fun stack(vararg tup: Int): Int = tup.sum()49 val X = 150 val x = stack(1, X)51 """.trimIndent()52 )53 print("")54 }55 }56 @Test57 fun `compilation error`() {58 val ex = shouldThrow<ReplCompilerException> {59 eval(60 """61 val foobar = 7862 val foobaz = "dsdsda"63 val ddd = ppp64 val ooo = foobar65 """.trimIndent()66 )67 }68 val diag = ex.firstError69 val location = diag?.location70 val message = ex.message71 val expectedLocation = SourceCode.Location(SourceCode.Position(3, 11), SourceCode.Position(3, 14))72 val expectedMessage = "Line_0.${repl.fileExtension} (3:11 - 14) Unresolved reference: ppp"73 location shouldBe expectedLocation74 message shouldBe expectedMessage75 }76 @Test77 fun `runtime execution error`() {78 val ex = shouldThrow<ReplEvalRuntimeException> {79 eval(80 """81 try {82 (null as String)83 } catch(e: NullPointerException) {84 throw RuntimeException("XYZ", e)85 }86 """.trimIndent()87 )88 }89 with(ex.render()) {90 shouldContain(NullPointerException::class.qualifiedName!!)91 shouldContain("XYZ")92 shouldContain("""at Line_\d+.<init>\(Line_\d+\.jupyter-kts:2\)""".toRegex())93 shouldNotContain(ReplEvalRuntimeException::class.simpleName!!)94 }95 }96 @Test97 fun testDependsOnAnnotation() {98 eval("@file:DependsOn(\"de.erichseifert.gral:gral-core:0.11\")")99 }100 @Test101 fun testImportResolutionAfterFailure() {102 val errorsRes = repl.listErrorsBlocking("import net.pearx.kasechange.*")103 errorsRes.errors shouldHaveSize 1104 val res = eval(105 """106 @file:DependsOn("net.pearx.kasechange:kasechange-jvm:1.3.0")107 import net.pearx.kasechange.*108 1109 """.trimIndent()110 )111 res.resultValue shouldBe 1112 }113 @Test114 fun testDependsOnAnnotationCompletion() {115 eval(116 """117 @file:Repository("https://repo1.maven.org/maven2/")118 @file:DependsOn("com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3")119 """.trimIndent()120 )121 val res = repl.completeBlocking("import com.github.", 18)122 res.shouldBeInstanceOf<CompletionResult.Success>()123 res.sortedMatches().contains("doyaaaaaken")124 }125 @Test126 fun testExternalStaticFunctions() {127 val res = eval(128 """129 @file:DependsOn("src/test/testData/kernelTestPackage-1.0.jar")130 import pack.*131 func()132 """.trimIndent()133 )134 res.resultValue shouldBe 42135 }136 @Test137 fun testScriptIsolation() {138 shouldThrowAny {139 eval("org.jetbrains.kotlinx.jupyter.ReplLineMagics.use")140 }141 }142 @Test143 fun testDependsOnAnnotations() {144 val res = eval(145 """146 @file:DependsOn("de.erichseifert.gral:gral-core:0.11")147 @file:Repository("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")148 @file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.2")149 """.trimIndent()150 )151 val newClasspath = res.metadata.newClasspath152 newClasspath shouldHaveAtLeastSize 2153 val htmlLibPath = "org/jetbrains/kotlinx/kotlinx-html-jvm/0.7.2/kotlinx-html-jvm".replace('/', File.separatorChar)154 newClasspath.any { htmlLibPath in it }.shouldBeTrue()155 }156 @Test157 fun testCompletionSimple() {158 eval("val foobar = 42")159 eval("var foobaz = 43")160 val result = repl.completeBlocking("val t = foo", 11)161 result.getOrFail().sortedMatches() shouldBe arrayListOf("foobar", "foobaz")162 }163 @Test164 fun testNoCompletionAfterNumbers() {165 val result = repl.completeBlocking("val t = 42", 10)166 result.getOrFail().sortedMatches().shouldBeEmpty()167 }168 @Test169 fun testCompletionForImplicitReceivers() {170 eval(171 """172 class AClass(val c_prop_x: Int) {173 fun filter(xxx: (AClass).() -> Boolean): AClass {174 return this175 }176 }177 val AClass.c_prop_y: Int178 get() = c_prop_x * c_prop_x179 180 fun AClass.c_meth_z(v: Int) = v * c_prop_y181 val df = AClass(10)182 val c_zzz = "some string"183 """.trimIndent()184 )185 val result = repl.completeBlocking("df.filter { c_ }", 14)186 result.getOrFail().sortedMatches() shouldBe arrayListOf("c_meth_z(", "c_prop_x", "c_prop_y", "c_zzz")187 }188 @Test189 fun testParametersCompletion() {190 eval("fun f(xyz: Int) = xyz * 2")191 val result = repl.completeBlocking("val t = f(x", 11)192 result.getOrFail().sortedMatches() shouldBe arrayListOf("xyz = ")193 }194 @Test195 fun testDeprecationCompletion() {196 eval(197 """198 @Deprecated("use id() function instead")199 fun id_deprecated(x: Int) = x200 """.trimIndent()201 )202 runBlocking {203 val result = repl.completeBlocking("val t = id_d", 12)204 result.getOrFail().sortedRaw().any {205 it.text == "id_deprecated(" && it.deprecationLevel == DeprecationLevel.WARNING206 }.shouldBeTrue()207 }208 }209 @Test210 fun testErrorsList() {211 eval(212 """213 data class AClass(val memx: Int, val memy: String)214 data class BClass(val memz: String, val mema: AClass)215 val foobar = 42216 var foobaz = "string"217 val v = BClass("KKK", AClass(5, "25"))218 """.trimIndent()219 )220 val result = repl.listErrorsBlocking(221 """222 val a = AClass("42", 3.14)223 val b: Int = "str"224 val c = foob225 """.trimIndent()226 )227 val actualErrors = result.errors.toList()228 val path = actualErrors.first().sourcePath229 actualErrors shouldBe withPath(230 path,231 listOf(232 generateDiagnostic(1, 16, 1, 20, "Type mismatch: inferred type is String but Int was expected", "ERROR"),233 generateDiagnostic(1, 22, 1, 26, "The floating-point literal does not conform to the expected type String", "ERROR"),234 generateDiagnostic(2, 14, 2, 19, "Type mismatch: inferred type is String but Int was expected", "ERROR"),235 generateDiagnostic(3, 9, 3, 13, "Unresolved reference: foob", "ERROR")236 )237 )238 }239 @Test240 fun testFreeCompilerArg() {241 val res = eval(242 """243 @file:CompilerArgs("-opt-in=kotlin.RequiresOptIn")244 """.trimIndent()245 )246 res.resultValue shouldBe Unit247 val actualErrors = repl.listErrorsBlocking(248 """249 import kotlin.time.*250 @OptIn(ExperimentalTime::class)251 val mark = TimeSource.Monotonic.markNow()252 """.trimIndent()253 ).errors.toList()254 actualErrors.shouldBeEmpty()255 }256 @Test257 fun testErrorsListWithMagic() {258 val result = repl.listErrorsBlocking(259 """260 %use krangl261 262 val x = foobar263 3 * 14264 %trackClasspath265 """.trimIndent()266 )267 val actualErrors = result.errors.toList()268 val path = actualErrors.first().sourcePath269 actualErrors shouldBe withPath(270 path,271 listOf(272 generateDiagnostic(3, 9, 3, 15, "Unresolved reference: foobar", "ERROR")273 )274 )275 }276 @Test277 fun testCompletionWithMagic() {278 eval("val foobar = 42")279 val code =280 """281 282 %trackClasspath283 284 foo285 """.trimIndent()286 val result = repl.completeBlocking(code, code.indexOf("foo") + 3)287 result.shouldBeInstanceOf<CompletionResult.Success>()288 result.sortedMatches() shouldBe arrayListOf("foobar")289 }290 @Test291 fun testCommands() {292 val code1 = ":help"293 val code2 = ":hex "294 runBlocking {295 repl.listErrors(code1) { result ->296 result.code shouldBe code1297 result.errors.shouldBeEmpty()298 }299 repl.listErrors(code2) { result ->300 result.code shouldBe code2301 val expectedList = listOf(generateDiagnosticFromAbsolute(code2, 0, 4, "Unknown command", "ERROR"))302 val actualList = result.errors.toList()303 actualList shouldBe expectedList304 }305 repl.complete(code2, 3) { result ->306 result.shouldBeInstanceOf<CompletionResult.Success>()307 result.sortedMatches() shouldBe listOf("help")308 }309 }310 }311 @Test312 fun testEmptyErrorsListJson() {313 val res = ListErrorsResult("someCode")314 Json.encodeToString(serializer(), res.message) shouldBe """{"code":"someCode","errors":[]}"""315 }316 @Test317 fun testOut() {318 eval("1+1", null, 1)319 val res = eval("Out[1]")320 res.resultValue shouldBe 2321 shouldThrowAny { eval("Out[3]") }322 }323 @Test324 fun testNoHistory() {325 eval("1+1", storeHistory = false)326 shouldThrow<ReplEvalRuntimeException> {327 eval("Out[1]")328 }329 }330 @Test331 fun testOutputMagic() {332 eval("%output --max-cell-size=100500 --no-stdout")333 repl.outputConfig shouldBe OutputConfig(334 cellOutputMaxSize = 100500,335 captureOutput = false336 )337 eval("%output --max-buffer=42 --max-buffer-newline=33 --max-time=2000")338 repl.outputConfig shouldBe OutputConfig(339 cellOutputMaxSize = 100500,340 captureOutput = false,341 captureBufferMaxSize = 42,342 captureNewlineBufferSize = 33,343 captureBufferTimeLimitMs = 2000344 )345 eval("%output --reset-to-defaults")346 repl.outputConfig shouldBe OutputConfig()347 }348 @Test349 fun testJavaRuntimeUtils() {350 val result = eval("JavaRuntimeUtils.version")351 val resultVersion = result.resultValue352 val expectedVersion = JavaRuntime.version353 resultVersion shouldBe expectedVersion354 }355 @Test356 fun testKotlinMath() {357 val result = eval("2.0.pow(2.0)").resultValue358 result shouldBe 4.0359 }360 @Test361 fun testNativeLibrary() {362 val libName = "GraphMolWrap"363 val testDataPath = "src/test/testData/nativeTest"364 val jarPath = "$testDataPath/org.RDKit.jar"365 val res = eval(366 """367 @file:DependsOn("$jarPath")368 import org.RDKit.RWMol369 import org.RDKit.RWMol.MolFromSmiles370 Native.loadLibrary(RWMol::class, "$libName", "$testDataPath")371 MolFromSmiles("c1ccccc1")372 """.trimIndent()373 ).resultValue374 res.shouldNotBeNull()375 res::class.qualifiedName shouldBe "org.RDKit.RWMol"376 }377 @Test378 fun testLambdaRendering() {379 val res = eval(380 """381 val foo: (Int) -> Int = {it + 1}382 foo383 """.trimIndent()384 ).resultValue385 @Suppress("UNCHECKED_CAST")386 (res as (Int) -> Int)(1) shouldBe 2387 }388 @Test389 fun testAnonymousObjectRendering() {390 eval("42")391 eval("val sim = object : ArrayList<String>() {}")392 val res = eval("sim").resultValue393 res.toString() shouldBe "[]"394 }395 @Test396 fun testAnonymousObjectCustomRendering() {397 eval("USE { render<ArrayList<*>> { it.size } }")398 eval(399 """400 val sim = object : ArrayList<String>() {}401 sim.add("42")402 """.trimIndent()403 )404 val res = eval("sim").resultValue405 res shouldBe 1406 }407 @Test408 fun testStdlibJdkExtensionsUsage() {409 eval("USE_STDLIB_EXTENSIONS()")410 val res = eval(411 """412 import kotlin.io.path.*413 import java.nio.file.Path414 415 Path.of(".").absolute()416 """.trimIndent()417 ).resultValue418 res.shouldBeInstanceOf<Path>()419 }420 @Test421 fun testArraysRendering() {422 eval("intArrayOf(1, 2, 3)").resultValue.toString() shouldBe "[1, 2, 3]"423 eval("arrayOf(1 to 2, 3 to 4)").resultValue.toString() shouldBe "[(1, 2), (3, 4)]"424 eval("booleanArrayOf(true, false)").resultValue.toString() shouldBe "[true, false]"425 }426 @Test427 fun testOutVarRendering() {428 eval("Out").resultValue.shouldNotBeNull()429 }430 @Test431 fun testMagicsErrorsReporting() {432 "%us".let { code ->433 listErrors(code).errors.toList() shouldBe listOf(generateDiagnosticFromAbsolute(code, 0, 3, "Unknown magic", "ERROR"))434 }435 "%use kmath".let { code ->436 listErrors(code).errors.toList().shouldBeEmpty()437 }438 }439 @Test440 fun testIssue356() {441 eval(442 """443 sealed class BaseObjClass444 object Obj : BaseObjClass()445 val topLevelSequence = sequence {446 yield(Obj)447 }448 open class Base {449 val iter = topLevelSequence.iterator()450 }451 class Child: Base()452 453 Child::class.simpleName454 """.trimIndent()455 ).resultValue shouldBe "Child"456 }457 @Test458 fun testIssue360() {459 eval("val a = 1")460 eval("fun b() = a")461 eval("b()").resultValue shouldBe 1462 }463}...

Full Screen

Full Screen

SequenceKTest.kt

Source:SequenceKTest.kt Github

copy

Full Screen

1package arrow.core2import arrow.core.test.UnitSpec3import arrow.core.test.generators.option4import arrow.core.test.laws.MonoidLaws5import arrow.typeclasses.Monoid6import arrow.typeclasses.Semigroup7import io.kotest.matchers.sequences.shouldBeEmpty8import io.kotest.property.Arb9import io.kotest.property.checkAll10import io.kotest.matchers.shouldBe11import io.kotest.property.arbitrary.int12import io.kotest.property.arbitrary.list13import io.kotest.property.arbitrary.positiveInts14import io.kotest.property.arbitrary.string15import kotlin.math.max16import kotlin.math.min17class SequenceKTest : UnitSpec() {18 init {19 testLaws(MonoidLaws.laws(Monoid.sequence(), Arb.sequence(Arb.int())) { s1, s2 -> s1.toList() == s2.toList() })20 "traverseEither stack-safe" {21 // also verifies result order and execution order (l to r)22 val acc = mutableListOf<Int>()23 val res = generateSequence(0) { it + 1 }.traverseEither { a ->24 if (a > 20_000) {25 Either.Left(Unit)26 } else {27 acc.add(a)28 Either.Right(a)29 }30 }31 acc shouldBe (0..20_000).toList()32 res shouldBe Either.Left(Unit)33 }34 "traverseOption stack-safe" {35 // also verifies result order and execution order (l to r)36 val acc = mutableListOf<Int>()37 val res = generateSequence(0) { it + 1 }.traverseOption { a ->38 (a <= 20_000).maybe {39 acc.add(a)40 a41 }42 }43 acc shouldBe (0..20_000).toList()44 res shouldBe None45 }46 "traverseValidated stack-safe" {47 // also verifies result order and execution order (l to r)48 val acc = mutableListOf<Int>()49 val res = (0..20_000).asSequence().traverseValidated(Semigroup.string()) {50 acc.add(it)51 Validated.Valid(it)52 }.map { it.toList() }53 res shouldBe Validated.Valid(acc)54 res shouldBe Validated.Valid((0..20_000).toList())55 }56 "traverseValidated acummulates" {57 checkAll(Arb.sequence(Arb.int())) { ints ->58 val res: ValidatedNel<Int, Sequence<Int>> = ints.map { i -> if (i % 2 == 0) i.validNel() else i.invalidNel() }59 .sequenceValidated(Semigroup.nonEmptyList())60 val expected: ValidatedNel<Int, Sequence<Int>> = NonEmptyList.fromList(ints.filterNot { it % 2 == 0 }.toList())61 .fold({ ints.filter { it % 2 == 0 }.validNel() }, { it.invalid() })62 res.map { it.toList() } shouldBe expected.map { it.toList() }63 }64 }65 "zip3" {66 checkAll(Arb.sequence(Arb.int()), Arb.sequence(Arb.int()), Arb.sequence(Arb.int())) { a, b, c ->67 val result = a.zip(b, c, ::Triple)68 val expected = a.zip(b, ::Pair).zip(c) { (a, b), c -> Triple(a, b, c) }69 result.toList() shouldBe expected.toList()70 }71 }72 "zip4" {73 checkAll(74 Arb.sequence(Arb.int()),75 Arb.sequence(Arb.int()),76 Arb.sequence(Arb.int()),77 Arb.sequence(Arb.int())78 ) { a, b, c, d ->79 val result = a.zip(b, c, d, ::Tuple4)80 val expected = a.zip(b, ::Pair)81 .zip(c) { (a, b), c -> Triple(a, b, c) }82 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }83 result.toList() shouldBe expected.toList()84 }85 }86 "zip5" {87 checkAll(88 Arb.sequence(Arb.int()),89 Arb.sequence(Arb.int()),90 Arb.sequence(Arb.int()),91 Arb.sequence(Arb.int()),92 Arb.sequence(Arb.int())93 ) { a, b, c, d, e ->94 val result = a.zip(b, c, d, e, ::Tuple5)95 val expected = a.zip(b, ::Pair)96 .zip(c) { (a, b), c -> Triple(a, b, c) }97 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }98 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }99 result.toList() shouldBe expected.toList()100 }101 }102 "zip6" {103 checkAll(104 Arb.sequence(Arb.int()),105 Arb.sequence(Arb.int()),106 Arb.sequence(Arb.int()),107 Arb.sequence(Arb.int()),108 Arb.sequence(Arb.int()),109 Arb.sequence(Arb.int())110 ) { a, b, c, d, e, f ->111 val result = a.zip(b, c, d, e, f, ::Tuple6)112 val expected = a.zip(b, ::Pair)113 .zip(c) { (a, b), c -> Triple(a, b, c) }114 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }115 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }116 .zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }117 result.toList() shouldBe expected.toList()118 }119 }120 "zip7" {121 checkAll(122 Arb.sequence(Arb.int()),123 Arb.sequence(Arb.int()),124 Arb.sequence(Arb.int()),125 Arb.sequence(Arb.int()),126 Arb.sequence(Arb.int()),127 Arb.sequence(Arb.int()),128 Arb.sequence(Arb.int())129 ) { a, b, c, d, e, f, g ->130 val result = a.zip(b, c, d, e, f, g, ::Tuple7)131 val expected = a.zip(b, ::Pair)132 .zip(c) { (a, b), c -> Triple(a, b, c) }133 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }134 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }135 .zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }136 .zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }137 result.toList() shouldBe expected.toList()138 }139 }140 "zip8" {141 checkAll(142 Arb.sequence(Arb.int()),143 Arb.sequence(Arb.int()),144 Arb.sequence(Arb.int()),145 Arb.sequence(Arb.int()),146 Arb.sequence(Arb.int()),147 Arb.sequence(Arb.int()),148 Arb.sequence(Arb.int()),149 Arb.sequence(Arb.int())150 ) { a, b, c, d, e, f, g, h ->151 val result = a.zip(b, c, d, e, f, g, h, ::Tuple8)152 val expected = a.zip(b, ::Pair)153 .zip(c) { (a, b), c -> Triple(a, b, c) }154 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }155 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }156 .zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }157 .zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }158 .zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }159 result.toList() shouldBe expected.toList()160 }161 }162 "zip9" {163 checkAll(164 Arb.sequence(Arb.int()),165 Arb.sequence(Arb.int()),166 Arb.sequence(Arb.int()),167 Arb.sequence(Arb.int()),168 Arb.sequence(Arb.int()),169 Arb.sequence(Arb.int()),170 Arb.sequence(Arb.int()),171 Arb.sequence(Arb.int()),172 Arb.sequence(Arb.int())173 ) { a, b, c, d, e, f, g, h, i ->174 val result = a.zip(b, c, d, e, f, g, h, i, ::Tuple9)175 val expected = a.zip(b, ::Pair)176 .zip(c) { (a, b), c -> Triple(a, b, c) }177 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }178 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }179 .zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }180 .zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }181 .zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }182 .zip(i) { (a, b, c, d, e, f, g, h), i -> Tuple9(a, b, c, d, e, f, g, h, i) }183 result.toList() shouldBe expected.toList()184 }185 }186 "zip10" {187 checkAll(188 Arb.sequence(Arb.int()),189 Arb.sequence(Arb.int()),190 Arb.sequence(Arb.int()),191 Arb.sequence(Arb.int()),192 Arb.sequence(Arb.int()),193 Arb.sequence(Arb.int()),194 Arb.sequence(Arb.int()),195 Arb.sequence(Arb.int()),196 Arb.sequence(Arb.int()),197 Arb.sequence(Arb.int())198 ) { a, b, c, d, e, f, g, h, i, j ->199 val result = a.zip(b, c, d, e, f, g, h, i, j, ::Tuple10)200 val expected = a.zip(b, ::Pair)201 .zip(c) { (a, b), c -> Triple(a, b, c) }202 .zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }203 .zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }204 .zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }205 .zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }206 .zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }207 .zip(i) { (a, b, c, d, e, f, g, h), i -> Tuple9(a, b, c, d, e, f, g, h, i) }208 .zip(j) { (a, b, c, d, e, f, g, h, i), j -> Tuple10(a, b, c, d, e, f, g, h, i, j) }209 result.toList() shouldBe expected.toList()210 }211 }212 "can align sequences - 1" {213 checkAll(Arb.sequence(Arb.int()), Arb.sequence(Arb.string())) { a, b ->214 a.align(b).toList().size shouldBe max(a.toList().size, b.toList().size)215 }216 }217 "can align sequences - 2" {218 checkAll(Arb.sequence(Arb.int()), Arb.sequence(Arb.string())) { a, b ->219 a.align(b).take(min(a.toList().size, b.toList().size)).forEach {220 it.isBoth shouldBe true221 }222 }223 }224 "can align sequences - 3" {225 checkAll(Arb.sequence(Arb.int()), Arb.sequence(Arb.string())) { a, b ->226 val ls = a.toList()227 val rs = b.toList()228 a.align(b).drop(min(ls.size, rs.size)).forEach {229 if (ls.size < rs.size) {230 it.isRight shouldBe true231 } else {232 it.isLeft shouldBe true233 }234 }235 }236 }237 "align empty sequences" {238 val a = emptyList<String>().asSequence()239 a.align(a).shouldBeEmpty()240 }241 "align infinite sequences" {242 val seq1 = generateSequence("A") { it }243 val seq2 = generateSequence(0) { it + 1 }244 checkAll(10, Arb.positiveInts(max = 10_000)) { idx: Int ->245 val element = seq1.align(seq2).drop(idx).first()246 element shouldBe Ior.Both("A", idx)247 }248 }249 "mapNotNull" {250 checkAll(Arb.sequence(Arb.int())) { a ->251 val result = a.mapNotNull {252 when (it % 2 == 0) {253 true -> it.toString()254 else -> null255 }256 }257 val expected =258 a.toList()259 .mapNotNull {260 when (it % 2 == 0) {261 true -> it.toString()262 else -> null263 }264 }265 .asSequence()266 result.toList() shouldBe expected.toList()267 }268 }269 "filterOption should filter None" {270 checkAll(Arb.list(Arb.option(Arb.int()))) { ints ->271 ints.asSequence().filterOption().toList() shouldBe ints.filterOption()272 }273 }274 }275}...

Full Screen

Full Screen

evalTest.kt

Source:evalTest.kt Github

copy

Full Screen

1package brainfuck2import arrow.core.extensions.id.comonad.extract3import arrow.fx.IO4import arrow.fx.extensions.io.monad.monad5import arrow.fx.fix6import arrow.mtl.State7import arrow.mtl.run8import io.kotest.core.spec.style.StringSpec9import io.kotest.matchers.nulls.shouldNotBeNull10import io.kotest.matchers.sequences.shouldBeEmpty11import io.kotest.matchers.shouldBe12import kotlinx.collections.immutable.persistentListOf13import kotlinx.collections.immutable.toPersistentList14class EvalTest : StringSpec({15 val emptyMemory = List(1024) {0.toByte()}.toPersistentList()16 val helloWorld = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."17 val fibonacci = "+++++++++++\n" +18 ">+>>>>++++++++++++++++++++++++++++++++++++++++++++\n" +19 ">++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>\n" +20 "+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-\n" +21 "<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<\n" +22 "-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]\n" +23 ">[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++\n" +24 "+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++\n" +25 "++++++++++++++++++++++++++++++++++++++++++++.[-]<<\n" +26 "<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<\n" +27 "[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]"28 val head = "+>>>>>>>>>>-[,+[-.----------[[-]>]<->]<]" // taken from http://www.hevanet.com/cristofd/brainfuck/head.b29 "add two numbers in State" {30 val emptyIo = MemIO(emptySequence(), emptyList())31 val machine = Machine(persistentListOf(25, 40), 0, StateMachine)32 val program: Program? = parse("[->+<]>.") // add 25 and 40, then print it33 program.shouldNotBeNull()34 val e = State().monad<MemIO>().eval(machine, program)35 val (ioAfter, machineAfter) = e.run(emptyIo).extract()36 ioAfter.machineOut shouldBe listOf(65.toByte())37 ioAfter.machineIn.shouldBeEmpty()38 machineAfter.memory shouldBe listOf(0,65).map {it.toByte()}39 machineAfter.pointer shouldBe 140 }41 "add two numbers in IO" {42 val machine = Machine(persistentListOf(25, 40), 0, IOMachine.std)43 val program: Program? = parse("[->+<]>.") // add 25 and 40, then print it44 program.shouldNotBeNull()45 val e = IO.monad().eval(machine, program)46 val machineAfter = e.fix().unsafeRunSync()47 machineAfter.memory shouldBe listOf(0,65).map {it.toByte()}48 machineAfter.pointer shouldBe 149 }50 "multiply two numbers in State" {51 val a: Byte = 1052 val b: Byte = 1153 val emptyIo = MemIO(emptySequence(), emptyList())54 val machine = Machine(persistentListOf(a, b, 0, 0), 0, StateMachine)55 // taken from https://www.codingame.com/playgrounds/50426/getting-started-with-brainfuck/multiplication56 val program: Program? = parse("[>[->+>+<<]>[-<+>]<<-]")57 program.shouldNotBeNull()58 val e = State().monad<MemIO>().eval(machine, program)59 val (_, machineAfter) = e.run(emptyIo).extract()60 machineAfter.memory shouldBe listOf(0,b,0,a*b).map {it.toByte()}61 }62 "multiply two numbers in IO" {63 val a: Byte = 1064 val b: Byte = 1165 val machine = Machine(persistentListOf(a, b, 0, 0), 0, IOMachine.std)66 // taken from https://www.codingame.com/playgrounds/50426/getting-started-with-brainfuck/multiplication67 val program: Program? = parse("[>[->+>+<<]>[-<+>]<<-]")68 program.shouldNotBeNull()69 val machineAfter = IO.monad().eval(machine, program).fix().unsafeRunSync()70 println(machineAfter.memory.toList())71 machineAfter.memory shouldBe listOf(0,b,0,a*b).map {it.toByte()}72 }73 "print Hello World in State" {74 val emptyIo = MemIO(emptySequence(), emptyList())75 val machine = Machine(emptyMemory, 0, StateMachine)76 val program: Program? = parse(helloWorld)77 program.shouldNotBeNull()78 val e = State().monad<MemIO>().eval(machine, program)79 val (ioAfter) = e.run(emptyIo).extract()80 ioAfter.printOut() shouldBe "Hello World!\n"81 }82 "print Hello World in IO" {83 val machine = Machine(emptyMemory, 0, IOMachine.std)84 val program: Program? = parse(helloWorld)85 program.shouldNotBeNull()86 val e = IO.monad().eval(machine, program)87 e.fix().unsafeRunSync()88 }89 "print fibonacci in State" {90 val emptyIo = MemIO(emptySequence(), emptyList())91 val machine = Machine(emptyMemory, 0, StateMachine)92 val program: Program? = parse(fibonacci)93 program.shouldNotBeNull()94 val e = State().monad<MemIO>().eval(machine, program)95 val (ioAfter) = e.run(emptyIo).extract()96 ioAfter.printOut() shouldBe "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89"97 }98 "print fibonacci in IO" {99 val machine = Machine(emptyMemory, 0, IOMachine.std)100 val program: Program? = parse(fibonacci)101 program.shouldNotBeNull()102 IO.monad().eval(machine, program).fix().unsafeRunSync()103 }104 "print 10 lines of input in State" {105 val input = (1..20).joinToString(separator = "\n").toByteArray().asSequence()106 val emptyIo = MemIO(input, emptyList())107 val machine = Machine(emptyMemory, 0, StateMachine)108 val program: Program? = parse(head)109 program.shouldNotBeNull()110 val e = State().monad<MemIO>().eval(machine, program)111 val (ioAfter) = e.run(emptyIo).extract()112 ioAfter.printOut() shouldBe (1..10).joinToString(separator = "\n", postfix = "\n")113 }114 "print 10 lines of input in IO" {115 val input = (1..20).joinToString(separator = "\n").toByteArray().toTypedArray()116 val (iomac, output) = IOMachine.memory(input)117 val machine = Machine(emptyMemory, 0, iomac)118 val program: Program? = parse(head)119 program.shouldNotBeNull()120 IO.monad().eval(machine, program).fix().unsafeRunSync()121 val ioAfter = output.toString()122 ioAfter shouldBe (1..10).joinToString(separator = "\n", postfix = "\n")123 }124})...

Full Screen

Full Screen

BeEmptyTest.kt

Source:BeEmptyTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.collections2import io.kotest.assertions.throwables.shouldThrowAny3import io.kotest.core.spec.style.WordSpec4import io.kotest.matchers.collections.shouldBeEmpty5import io.kotest.matchers.collections.shouldHaveSize6import io.kotest.matchers.collections.shouldNotBeEmpty7import io.kotest.matchers.sequences.shouldBeEmpty8import io.kotest.matchers.sequences.shouldNotBeEmpty9import io.kotest.matchers.shouldBe10class BeEmptyTest : WordSpec() {11 init {12 "shouldBeEmpty" should {13 "succeed for empty list" {14 listOf<Int>().shouldBeEmpty()15 }16 "succeed for empty set" {17 setOf<Int>().shouldBeEmpty()18 }19 "succeed for empty sequence" {20 emptySequence<Int>().shouldBeEmpty()21 }22 "succeed for empty array" {23 arrayOf<Int>().shouldBeEmpty()24 }25 "fail for single element list" {26 shouldThrowAny {27 listOf(0).shouldBeEmpty()28 }.message shouldBe "Collection should be empty but contained 0"29 }30 "fail for single element set" {31 shouldThrowAny {32 setOf(0).shouldBeEmpty()33 }.message shouldBe "Collection should be empty but contained 0"34 }35 "fail for single element array" {36 shouldThrowAny {37 arrayOf(0).shouldBeEmpty()38 }.message shouldBe "Array should be empty but contained 0"39 }40 "fail for single element sequence" {41 shouldThrowAny {42 sequenceOf(0).shouldBeEmpty()43 }.message shouldBe "Sequence should be empty"44 }45 "fail for sequence of nulls" {46 shouldThrowAny {47 sequenceOf<Int?>(null, null, null, null).shouldBeEmpty()48 }.message shouldBe "Sequence should be empty"49 }50 "fail for null list reference" {51 val maybeList: List<String>? = null52 shouldThrowAny {53 maybeList.shouldBeEmpty()54 }.message shouldBe "Expected an array collection but was null"55 }56 "fail for null set reference" {57 val maybeSet: Set<String>? = null58 shouldThrowAny {59 maybeSet.shouldBeEmpty()60 }.message shouldBe "Expected an array collection but was null"61 }62 "return non nullable reference" {63 val maybeList: List<Int>? = listOf()64 maybeList.shouldBeEmpty().size65 }66 }67 "shouldNotBeEmpty" should {68 "fail for empty" {69 shouldThrowAny {70 emptySequence<Int>().shouldNotBeEmpty()71 }72 }73 "fail for null reference" {74 val maybeList: List<String>? = null75 shouldThrowAny {76 maybeList.shouldNotBeEmpty()77 }78 }79 "succeed for non-null nullable reference" {80 val maybeList: List<Int>? = listOf(1)81 maybeList.shouldNotBeEmpty()82 }83 "chain for non-null nullable reference" {84 val maybeList: List<Int>? = listOf(1)85 maybeList.shouldNotBeEmpty().shouldHaveSize(1)86 }87 "succeed for single element sequence" {88 sequenceOf(0).shouldNotBeEmpty()89 }90 "succeed for multiple element sequence" {91 sequenceOf(1, 2, 3).shouldNotBeEmpty()92 }93 "succeed for single element list" {94 listOf(0).shouldNotBeEmpty()95 }96 "succeed for multiple element list" {97 listOf(1, 2, 3).shouldNotBeEmpty()98 }99 "succeed for single element set" {100 setOf(0).shouldNotBeEmpty()101 }102 "succeed for multiple element set" {103 setOf(1, 2, 3).shouldNotBeEmpty()104 }105 "succeed for single element array" {106 arrayOf(0).shouldNotBeEmpty()107 }108 "succeed for multiple element array" {109 arrayOf(1, 2, 3).shouldNotBeEmpty()110 }111 }112 }113}...

Full Screen

Full Screen

ListExtensionsTests.kt

Source:ListExtensionsTests.kt Github

copy

Full Screen

1package uk.ac.nott.cs.das.cszgx2import io.kotest.core.spec.style.DescribeSpec3import io.kotest.matchers.nulls.shouldBeNull4import io.kotest.matchers.sequences.shouldBeEmpty5import io.kotest.matchers.sequences.shouldContain6import io.kotest.matchers.sequences.shouldHaveSize7import io.kotest.matchers.shouldBe8class ListExtensionsTests : DescribeSpec({9 describe("List#pairs") {10 it("should return an empty sequence from an empty list") {11 val xs = listOf<Int>()12 xs.pairs().shouldBeEmpty()13 }14 it("should return an empty sequence from a singleton list") {15 val xs = listOf(1)16 xs.pairs().shouldBeEmpty()17 }18 it("should return all the pairs from a non-singleton list") {19 val xs = listOf(1, 2, 3)20 xs.pairs().let {21 it.shouldHaveSize(2)22 it.shouldContain(1 to 2)23 it.shouldContain(2 to 3)24 }25 }26 }27 describe("List#mode") {28 it("should return null from an empty list") {29 val xs = listOf<Int>()30 xs.mode().shouldBeNull()31 }32 it("should return the modal value from a list with a unique mode") {33 val xs = listOf(1,2,2,3,4)34 xs.mode().shouldBe(2)35 }36 it("should return the first encountered modal value from a list with multiple modes") {37 val xs = listOf(1,2,2,3,3,4)38 xs.mode().shouldBe(2)39 }40 }41})...

Full Screen

Full Screen

Sequence.shouldBeEmpty

Using AI Code Generation

copy

Full Screen

1Sequence < Int > (). shouldBeEmpty ()2Sequence < Int > (). shouldNotBeEmpty ()3Sequence < Int > (). shouldContainAll ( 1 , 2 , 3 )4Sequence < Int > (). shouldNotContainAll ( 1 , 2 , 3 )5Sequence < Int > (). shouldContainExactly ( 1 , 2 , 3 )6Sequence < Int > (). shouldContainExactlyInAnyOrder ( 1 , 2 , 3 )7Sequence < Int > (). shouldContainAllInOrder ( 1 , 2 , 3 )8Sequence < Int > (). shouldContainInOrder ( 1 , 2 , 3 )9Sequence < Int > (). shouldContainNone ( 1 , 2 , 3 )10Sequence < Int > (). shouldContainDuplicates ()11Sequence < Int > (). shouldContainNoDuplicates ()12Sequence < Int > (). shouldContainSingleElement ( 1 )13Sequence < Int > (). shouldContainSingleElement ( 1 , 2 , 3 )

Full Screen

Full Screen

Sequence.shouldBeEmpty

Using AI Code Generation

copy

Full Screen

1Sequence.empty().shouldBeEmpty()2listOf(1, 2, 3).asSequence().shouldBeIn(1, 2, 3)3listOf(1, 2, 3).asSequence().shouldBeInstanceOf<Sequence<Int>>()4listOf(1, 2, 3).asSequence().shouldBeOneOf(1, 2, 3)5listOf(1, 2, 3).asSequence().shouldBeSorted()6listOf(1, 2, 3).asSequence().shouldBeSortedBy { it }7listOf(1, 2, 3).asSequence().shouldBeSortedByDescending { it }8listOf(1, 2, 3).asSequence().shouldBeSortedDescending()9listOf(1, 2, 3).asSequence().shouldContain(1)10listOf(1, 2, 3).asSequence().shouldContainAll(1, 2, 3)11listOf(1, 2, 3).asSequence().shouldContainExactly(1, 2, 3)12listOf(1, 2, 3).asSequence().shouldContainInOrder(1, 2, 3

Full Screen

Full Screen

Sequence.shouldBeEmpty

Using AI Code Generation

copy

Full Screen

1 import io.kotest.matchers.sequences.shouldBeEmpty 2 import io.kotest.matchers.sequences.shouldBeEmpty 3 import io.kotest.matchers.sequences.shouldContainAll 4 import io.kotest.matchers.sequences.shouldContainAll 5 import io.kotest.matchers.sequences.shouldContain 6 import io.kotest.matchers.sequences.shouldContain 7 import io.kotest.matchers.sequences.shouldContainExactly 8 import io.kotest.matchers.sequences.shouldContainExactly 9 import io.kotest.matchers.sequences.shouldContainExactlyInAnyOrder 10 import io.kotest.matchers.sequences.shouldContainExactlyInAnyOrder 11 import io.kotest.matchers.sequences.shouldContainInOrder 12 import io.kotest.matchers.sequences.shouldContainInOrder 13 import io.kotest.matchers.sequences.shouldContainSame 14 import io.kotest.matchers.sequences.shouldContainSame 15 import io.kotest.matchers.sequences.shouldHaveAtLeastSize 16 import io.kotest.matchers.sequences.shouldHaveAtLeastSize 17 import io.kotest.matchers.sequences.shouldHaveAtMostSize 18 import io.kotest.matchers.sequences.shouldHaveAtMostSize 19 import io.kotest.matchers.sequences.shouldHaveSize 20 import io.kotest.matchers.sequences.shouldHaveSize 21 import io.kotest.matchers.sequences.shouldNotBeEmpty 22 import io.kotest.matchers.sequences.shouldNotBeEmpty 23 import io.kotest.matchers.sequences.shouldNotContain 24 import io.kotest.matchers.sequences.shouldNotContain 25 import io.kotest.matchers.sequences.shouldNotContainAll 26 import io.kotest.matchers.sequences.shouldNotContainAll 27 import io.kotest.matchers.sequences.shouldNotContainExactly 28 import io.kotest.matchers.sequences.shouldNotContainExactly 29 import io.kotest.matchers.sequences.shouldNotContainExactlyInAnyOrder 30 import io.kotest.matchers.sequences.shouldNotContainExactlyInAnyOrder 31 import io.kotest.matchers.sequences.shouldNotContainInOrder 32 import io.kotest.matchers.sequences.shouldNotContainInOrder 33 import io.kotest.matchers.sequences.shouldNotContainSame 34 import

Full Screen

Full Screen

Sequence.shouldBeEmpty

Using AI Code Generation

copy

Full Screen

1 import io.kotest.matchers.sequences.shouldBeEmpty2 class SequenceTest : StringSpec({3 "empty sequence" {4 sequenceOf<Int>().shouldBeEmpty()5 }6 })

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful