# How to use Exhaustive.cartesian method of io.kotest.property.exhaustive.cartesian class

Best Kotest code snippet using io.kotest.property.exhaustive.cartesian.Exhaustive.cartesian

CartesianTest.kt

Source:CartesianTest.kt

`1package com.sksamuel.kotest.property.exhaustive2import io.kotest.core.Tuple43import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.collections.shouldHaveSize5import io.kotest.matchers.shouldBe6import io.kotest.property.Exhaustive7import io.kotest.property.exhaustive.cartesian8import io.kotest.property.exhaustive.cartesianPairs9import io.kotest.property.exhaustive.exhaustive10import io.kotest.property.exhaustive.of11class CartesianTest : FunSpec() {12 init {13 test("Exhaustive.cartesianPairs") {14 listOf(1, 2, 3).exhaustive().cartesianPairs().values shouldBe listOf(15 Pair(1, 1),16 Pair(1, 2),17 Pair(1, 3),18 Pair(2, 1),19 Pair(2, 2),20 Pair(2, 3),21 Pair(3, 1),22 Pair(3, 2),23 Pair(3, 3),24 )25 }26 test("Exhaustive.cartesian(a,b) arity 2") {27 val e = Exhaustive.cartesian(28 Exhaustive.of(1, 2, 3),29 Exhaustive.of(true, false)30 ) { a, b -> Pair(a, b) }31 e.values.shouldHaveSize(6)32 e.values shouldBe listOf(33 Pair(1, true),34 Pair(1, false),35 Pair(2, true),36 Pair(2, false),37 Pair(3, true),38 Pair(3, false),39 )40 }41 test("a.cartesian(b) arity 2") {42 val e = Exhaustive.of(1, 2, 3).cartesian(Exhaustive.of(true, false)) { a, b -> Pair(a, b) }43 e.values.shouldHaveSize(6)44 e.values shouldBe listOf(45 Pair(1, true),46 Pair(1, false),47 Pair(2, true),48 Pair(2, false),49 Pair(3, true),50 Pair(3, false),51 )52 }53 test("a.cartesianPairs(b) arity 2") {54 val e = Exhaustive.of(1, 2, 3).cartesianPairs(Exhaustive.of(true, false))55 e.values.shouldHaveSize(6)56 e.values shouldBe listOf(57 Pair(1, true),58 Pair(1, false),59 Pair(2, true),60 Pair(2, false),61 Pair(3, true),62 Pair(3, false),63 )64 }65 test("Exhaustive.cartesianPairs(a,b) arity 2") {66 val e = Exhaustive.cartesianPairs(67 Exhaustive.of(1, 2, 3),68 Exhaustive.of(true, false)69 )70 e.values.shouldHaveSize(6)71 e.values shouldBe listOf(72 Pair(1, true),73 Pair(1, false),74 Pair(2, true),75 Pair(2, false),76 Pair(3, true),77 Pair(3, false),78 )79 }80 test("Exhaustive.cartesian arity 3") {81 val e = Exhaustive.cartesian(82 Exhaustive.of(1, 2, 3),83 Exhaustive.of("a", "b", "c"),84 Exhaustive.of(true, false)85 ) { a, b, c -> Triple(a, b, c) }86 e.values.shouldHaveSize(18)87 e.values shouldBe listOf(88 Triple(1, "a", true),89 Triple(1, "a", false),90 Triple(1, "b", true),91 Triple(1, "b", false),92 Triple(1, "c", true),93 Triple(1, "c", false),94 Triple(2, "a", true),95 Triple(2, "a", false),96 Triple(2, "b", true),97 Triple(2, "b", false),98 Triple(2, "c", true),99 Triple(2, "c", false),100 Triple(3, "a", true),101 Triple(3, "a", false),102 Triple(3, "b", true),103 Triple(3, "b", false),104 Triple(3, "c", true),105 Triple(3, "c", false)106 )107 }108 test("Exhaustive.cartesian arity 4") {109 val e = Exhaustive.cartesian(110 Exhaustive.of(1, 2),111 Exhaustive.of("a", "b", "c"),112 Exhaustive.of(true, false),113 Exhaustive.of('p', 'q'),114 ) { a, b, c, d -> Tuple4(a, b, c, d) }115 e.values.shouldHaveSize(24)116 e.values shouldBe listOf(117 Tuple4(a = 1, b = "a", c = true, d = 'p'),118 Tuple4(a = 1, b = "a", c = true, d = 'q'),119 Tuple4(a = 1, b = "a", c = false, d = 'p'),120 Tuple4(a = 1, b = "a", c = false, d = 'q'),121 Tuple4(a = 1, b = "b", c = true, d = 'p'),122 Tuple4(a = 1, b = "b", c = true, d = 'q'),123 Tuple4(a = 1, b = "b", c = false, d = 'p'),124 Tuple4(a = 1, b = "b", c = false, d = 'q'),125 Tuple4(a = 1, b = "c", c = true, d = 'p'),126 Tuple4(a = 1, b = "c", c = true, d = 'q'),127 Tuple4(a = 1, b = "c", c = false, d = 'p'),128 Tuple4(a = 1, b = "c", c = false, d = 'q'),129 Tuple4(a = 2, b = "a", c = true, d = 'p'),130 Tuple4(a = 2, b = "a", c = true, d = 'q'),131 Tuple4(a = 2, b = "a", c = false, d = 'p'),132 Tuple4(a = 2, b = "a", c = false, d = 'q'),133 Tuple4(a = 2, b = "b", c = true, d = 'p'),134 Tuple4(a = 2, b = "b", c = true, d = 'q'),135 Tuple4(a = 2, b = "b", c = false, d = 'p'),136 Tuple4(a = 2, b = "b", c = false, d = 'q'),137 Tuple4(a = 2, b = "c", c = true, d = 'p'),138 Tuple4(a = 2, b = "c", c = true, d = 'q'),139 Tuple4(a = 2, b = "c", c = false, d = 'p'),140 Tuple4(a = 2, b = "c", c = false, d = 'q'),141 )142 }143 }144}...`

ParserTest.kt

Source:ParserTest.kt

`1package dev.cyberdeck.lisp2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.StringSpec4import io.kotest.inspectors.forAll5import io.kotest.inspectors.forExactly6import io.kotest.matchers.collections.shouldHaveSize7import io.kotest.matchers.shouldBe8import io.kotest.matchers.string.shouldNotBeEmpty9import io.kotest.matchers.string.shouldNotContain10import io.kotest.matchers.types.shouldBeInstanceOf11import io.kotest.property.Exhaustive12import io.kotest.property.checkAll13import io.kotest.property.exhaustive.cartesian14import io.kotest.property.exhaustive.exhaustive15import io.kotest.property.exhaustive.merge16class ParserTest: StringSpec({17 val strings = listOf("\"hello\"", "\"hello world\"", "\"\"", "\")(\"", "\"()\"").exhaustive()18 val symbols = listOf("abc", "x", "x1", " a", " b").exhaustive()19 val atoms = symbols.merge(listOf("-1", "1", "1.0", "-1.0").exhaustive())20 val lists = Exhaustive.cartesian(atoms, atoms) { a, b -> listOf(a, b) }21 "parse token" {22 checkAll(symbols) { s ->23 val result = tokenize(s)24 result.forAll {25 it.shouldNotContain(" ")26 it.shouldNotBeEmpty()27 }28 result.forExactly(1) { it.shouldBe(s.trim()) }29 }30 }31 "parse with parens" {32 checkAll(symbols) { s ->33 val result = tokenize("(\$s)")34 result.forAll {35 it.shouldNotContain(" ")36 it.shouldNotBeEmpty()37 }38 result.forExactly(1) { it.shouldBe(s.trim()) }39 }40 }41 "parse with unbalanced parens" {42 checkAll(symbols) { s ->43 shouldThrow<SyntaxErr> {44 readFromTokens(tokenize("(\$s \$s (\$s (\$s \$s))")) // no closing paren45 }46 }47 }48 "parse strings" {49 val exp = readFromTokens(tokenize("\"well this is silly\""))50 exp.shouldBe(LString("well this is silly"))51 }52 "parse lists of strings" {53 checkAll(strings) { str ->54 val bare = str.substring(1, str.length - 1)55 val exp = readFromTokens(tokenize("(\$str (\$str \$str))"))56 exp.shouldBe(listExp(LString(bare), listExp(LString(bare), LString(bare))))57 }58 }59 "parse complex expressions" {60 val tokens = tokenize("(begin (define r 10.0) (* pi (* r r)))")61 tokens.shouldBe(listOf("(", "begin", "(", "define", "r", "10.0", ")", "(", "*", "pi", "(", "*", "r", "r", ")", ")", ")"))62 }63 "readFromTokens fails if empty" {64 shouldThrow<SyntaxErr> {65 readFromTokens(emptyList())66 }67 }68 "readFromTokens returns int" {69 checkAll<Int> { i ->70 readFromTokens(listOf(i.toString())).shouldBe(Num(i))71 }72 }73 "readFromTokens returns float" {74 checkAll<Float> { f ->75 readFromTokens(listOf(f.toString())).shouldBe(Num(f))76 }77 }78 "readFromTokens returns boolean" {79 checkAll<Boolean> { b ->80 readFromTokens(listOf(b.toString())).shouldBe(Bool(b))81 }82 }83 "readFromTokens returns string" {84 checkAll(strings) { str ->85 readFromTokens(listOf(str)).shouldBe(LString(str.substring(1, str.length - 1)))86 }87 }88 "readFromTokens returns symbol" {89 checkAll(symbols) { s ->90 readFromTokens(listOf(s)).shouldBe(Symbol(s))91 }92 }93 "readFromTokens fails on unmatched closing paren" {94 shouldThrow<SyntaxErr> {95 readFromTokens(listOf(")"))96 }97 }98 "readFromTokens returns list of atoms" {99 checkAll(lists) { list ->100 readFromTokens(listOf("(") + list + ")")101 .shouldBeInstanceOf<L>()102 .list.shouldHaveSize(list.size)103 .forAll { it.shouldBeInstanceOf<Atom>() }104 }105 }106})...`

cartesian.kt

Source:cartesian.kt

`1package io.kotest.property.exhaustive2import io.kotest.property.Exhaustive3fun <A, B, C> Exhaustive<A>.cartesian(other: Exhaustive<B>, f: (A, B) -> C): Exhaustive<C> {4 val cs = values.flatMap { _a ->5 other.values.map { _b ->6 f(_a, _b)7 }8 }9 return cs.exhaustive()10}11fun <A, B> Exhaustive<A>.cartesianPairs(other: Exhaustive<B>): Exhaustive<Pair<A, B>> {12 val pairs = values.flatMap { _a ->13 other.values.map { _b ->14 Pair(_a, _b)15 }16 }17 return pairs.exhaustive()18}19/**20 * Returns the cartesian join of this exhaustive with itself, with the results as pairs.21 */22fun <A> Exhaustive<A>.cartesianPairs(): Exhaustive<Pair<A, A>> {23 val cs = values.flatMap { _a ->24 values.map { _b ->25 Pair(_a, _b)26 }27 }28 return cs.exhaustive()29}30fun <A, B, C> Exhaustive.Companion.cartesian(a: Exhaustive<A>, b: Exhaustive<B>, f: (A, B) -> C): Exhaustive<C> {31 val cs = a.values.flatMap { _a ->32 b.values.map { _b ->33 f(_a, _b)34 }35 }36 return cs.exhaustive()37}38fun <A, B> Exhaustive.Companion.cartesianPairs(a: Exhaustive<A>, b: Exhaustive<B>): Exhaustive<Pair<A, B>> {39 val pairs = a.values.flatMap { _a ->40 b.values.map { _b ->41 Pair(_a, _b)42 }43 }44 return pairs.exhaustive()45}46fun <A, B, C, D> Exhaustive.Companion.cartesian(47 a: Exhaustive<A>,48 b: Exhaustive<B>,49 c: Exhaustive<C>,50 f: (A, B, C) -> D51): Exhaustive<D> {52 val ds = a.values.flatMap { _a ->53 b.values.flatMap { _b ->54 c.values.map { _c ->55 f(_a, _b, _c)56 }57 }58 }59 return ds.exhaustive()60}61fun <A, B, C, D, E> Exhaustive.Companion.cartesian(62 a: Exhaustive<A>,63 b: Exhaustive<B>,64 c: Exhaustive<C>,65 d: Exhaustive<D>,66 f: (A, B, C, D) -> E67): Exhaustive<E> {68 val es = a.values.flatMap { _a ->69 b.values.flatMap { _b ->70 c.values.flatMap { _c ->71 d.values.map { _d ->72 f(_a, _b, _c, _d)73 }74 }75 }76 }77 return es.exhaustive()78}79fun <A, B, C, D, E, F> Exhaustive.Companion.cartesian(80 a: Exhaustive<A>,81 b: Exhaustive<B>,82 c: Exhaustive<C>,83 d: Exhaustive<D>,84 e: Exhaustive<E>,85 f: (A, B, C, D, E) -> F86): Exhaustive<F> {87 val fs = a.values.flatMap { _a ->88 b.values.flatMap { _b ->89 c.values.flatMap { _c ->90 d.values.flatMap { _d ->91 e.values.map { _e ->92 f(_a, _b, _c, _d, _e)93 }94 }95 }96 }97 }98 return fs.exhaustive()99}...`

## 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:

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

## Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

## Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!