Best Kotest code snippet using io.kotest.matchers.collections.unique.test
matchers.kt
Source:matchers.kt
1package tutorial.kotest2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.assertions.throwables.shouldThrowExactly5import io.kotest.core.spec.style.DescribeSpec6import io.kotest.core.test.AssertionMode7import io.kotest.matchers.booleans.shouldBeTrue8import io.kotest.matchers.collections.shouldBeIn9import io.kotest.matchers.collections.shouldBeOneOf10import io.kotest.matchers.collections.shouldBeSameSizeAs11import io.kotest.matchers.collections.shouldBeSingleton12import io.kotest.matchers.collections.shouldBeSmallerThan13import io.kotest.matchers.collections.shouldBeSorted14import io.kotest.matchers.collections.shouldBeUnique15import io.kotest.matchers.collections.shouldContain16import io.kotest.matchers.collections.shouldContainAll17import io.kotest.matchers.collections.shouldContainAnyOf18import io.kotest.matchers.collections.shouldContainDuplicates19import io.kotest.matchers.collections.shouldContainExactly20import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder21import io.kotest.matchers.collections.shouldContainInOrder22import io.kotest.matchers.collections.shouldContainNull23import io.kotest.matchers.collections.shouldEndWith24import io.kotest.matchers.collections.shouldHaveAtLeastSize25import io.kotest.matchers.collections.shouldHaveLowerBound26import io.kotest.matchers.collections.shouldHaveSingleElement27import io.kotest.matchers.collections.shouldHaveSize28import io.kotest.matchers.collections.shouldHaveUpperBound29import io.kotest.matchers.collections.shouldNotContainAnyOf30import io.kotest.matchers.collections.shouldNotHaveElementAt31import io.kotest.matchers.collections.shouldStartWith32import io.kotest.matchers.comparables.shouldBeEqualComparingTo33import io.kotest.matchers.comparables.shouldBeLessThanOrEqualTo34import io.kotest.matchers.date.shouldBeToday35import io.kotest.matchers.date.shouldHaveSameHoursAs36import io.kotest.matchers.doubles.Percentage37import io.kotest.matchers.doubles.beNaN38import io.kotest.matchers.doubles.plusOrMinus39import io.kotest.matchers.doubles.shouldBeNaN40import io.kotest.matchers.doubles.shouldNotBeNaN41import io.kotest.matchers.equality.shouldBeEqualToComparingFields42import io.kotest.matchers.equality.shouldBeEqualToComparingFieldsExcept43import io.kotest.matchers.equality.shouldBeEqualToIgnoringFields44import io.kotest.matchers.equality.shouldBeEqualToUsingFields45import io.kotest.matchers.file.shouldBeADirectory46import io.kotest.matchers.file.shouldBeAbsolute47import io.kotest.matchers.file.shouldExist48import io.kotest.matchers.file.shouldNotBeEmpty49import io.kotest.matchers.ints.beOdd50import io.kotest.matchers.ints.shouldBeBetween51import io.kotest.matchers.ints.shouldBeInRange52import io.kotest.matchers.ints.shouldBeLessThan53import io.kotest.matchers.ints.shouldBeLessThanOrEqual54import io.kotest.matchers.ints.shouldBeOdd55import io.kotest.matchers.ints.shouldBePositive56import io.kotest.matchers.ints.shouldBeZero57import io.kotest.matchers.iterator.shouldBeEmpty58import io.kotest.matchers.iterator.shouldHaveNext59import io.kotest.matchers.maps.shouldBeEmpty60import io.kotest.matchers.maps.shouldContain61import io.kotest.matchers.maps.shouldContainAll62import io.kotest.matchers.maps.shouldContainExactly63import io.kotest.matchers.maps.shouldContainKey64import io.kotest.matchers.nulls.shouldBeNull65import io.kotest.matchers.nulls.shouldNotBeNull66import io.kotest.matchers.shouldBe67import io.kotest.matchers.shouldNot68import io.kotest.matchers.shouldNotBe69import io.kotest.matchers.string.beEmpty70import io.kotest.matchers.string.shouldBeBlank71import io.kotest.matchers.string.shouldBeEmpty72import io.kotest.matchers.string.shouldBeEqualIgnoringCase73import io.kotest.matchers.string.shouldBeInteger74import io.kotest.matchers.string.shouldBeLowerCase75import io.kotest.matchers.string.shouldBeUpperCase76import io.kotest.matchers.string.shouldContain77import io.kotest.matchers.string.shouldContainADigit78import io.kotest.matchers.string.shouldContainIgnoringCase79import io.kotest.matchers.string.shouldContainOnlyDigits80import io.kotest.matchers.string.shouldContainOnlyOnce81import io.kotest.matchers.string.shouldEndWith82import io.kotest.matchers.string.shouldHaveLength83import io.kotest.matchers.string.shouldHaveLineCount84import io.kotest.matchers.string.shouldHaveMaxLength85import io.kotest.matchers.string.shouldHaveMinLength86import io.kotest.matchers.string.shouldHaveSameLengthAs87import io.kotest.matchers.string.shouldMatch88import io.kotest.matchers.string.shouldNotBeEmpty89import io.kotest.matchers.string.shouldStartWith90import io.kotest.matchers.throwable.shouldHaveCause91import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf92import io.kotest.matchers.throwable.shouldHaveCauseOfType93import io.kotest.matchers.throwable.shouldHaveMessage94import io.kotest.matchers.types.shouldBeInstanceOf95import io.kotest.matchers.types.shouldBeSameInstanceAs96import io.kotest.matchers.types.shouldBeTypeOf97import io.kotest.matchers.uri.shouldHaveHost98import io.kotest.matchers.uri.shouldHavePort99import io.kotest.matchers.uri.shouldHaveScheme100import java.io.File101import java.net.URI102import java.time.LocalDate103import java.time.LocalTime104// https://kotest.io/docs/assertions/core-matchers.html105class MatchersTest : DescribeSpec({106 describe("general") {107 it("basics") {108 (1 == 1).shouldBeTrue()109 (2 + 2) shouldBe 4110 val foo: Any = "foobar"111 foo.shouldBeTypeOf<String>() shouldContain "fo"112 "".shouldBeEmpty()113 "x".shouldNot(beEmpty()) // manually negate114 "x".shouldNotBeEmpty() // reusable115 URI("https://tba") shouldHaveHost "tba"116 URI("https://tba:81") shouldHavePort 81117 URI("https://tba") shouldHaveScheme "https"118 File("/").apply {119 shouldExist()120 shouldBeADirectory()121 shouldBeAbsolute()122 shouldNotBeEmpty()123 }124 // executable, hidden, readable, smaller, writeable, containFile, extension, path, ...125 LocalDate.now().shouldBeToday()126 // before/after, within, same, between, have year/month/day/hour/...127 LocalTime.now().shouldHaveSameHoursAs(LocalTime.now())128 // before/after/between, sameMinute/Seconds/Nanos129 }130 it("numbers") {131 1 shouldBeLessThan 2132 1 shouldBeLessThanOrEqual 1 // Int-based; returns this133 1 shouldBeLessThanOrEqualTo 1 // Comparble-based; void134 1 shouldBeEqualComparingTo 1 // Comparable-based135 1.shouldBeBetween(0, 2)136 1 shouldBeInRange 0..2137 0.shouldBeZero()138 1.shouldBePositive()139 1.shouldBeOdd()140 (1.2).shouldBe(1.20001.plusOrMinus(Percentage(20.0)))141 (1.2).shouldNotBeNaN()142 }143 it("strings") {144 // generic: "abc" shouldBe "abc"145 "aBc" shouldBeEqualIgnoringCase "abc"146 "".shouldBeEmpty()147 " ".shouldBeBlank() // empty or whitespace148 "abc" shouldContain ("b")149 "aBc" shouldContainIgnoringCase "bc"150 "x-a-x" shouldContain """\-[a-z]\-""".toRegex()151 "-a-" shouldMatch """\-[a-z]\-""".toRegex()152 "abc" shouldStartWith ("a")153 "abc" shouldEndWith ("c")154 "ab aa" shouldContainOnlyOnce "aa"155 "abc".shouldBeLowerCase()156 "ABC".shouldBeUpperCase()157 "abc" shouldHaveLength 3158 "a\nb" shouldHaveLineCount 2159 "ab" shouldHaveMinLength 1 shouldHaveMaxLength 3160 "abc" shouldHaveSameLengthAs "foo"161 "1".shouldBeInteger()162 "12".shouldContainOnlyDigits()163 "abc1".shouldContainADigit() // at least one164 }165 it("types") {166 @Connotation167 open class SuperType()168 class SubType : SuperType()169 val sameRef = SuperType()170 sameRef.shouldBeSameInstanceAs(sameRef)171 val superType: SuperType = SubType()172 superType.shouldBeTypeOf<SubType>() // exact runtime match (SuperType won't work!)173 superType.shouldBeInstanceOf<SuperType>() // T or below174// SubType().shouldHaveAnnotation(Connotation::class)175 val nullable: String? = null176 nullable.shouldBeNull()177 }178 it("collections") {179 emptyList<Int>().iterator().shouldBeEmpty()180 listOf(1).iterator().shouldHaveNext()181 listOf(1, 2) shouldContain 1 // at least182 listOf(1, 2) shouldContainExactly listOf(1, 2) // in-order; not more183 listOf(1, 2) shouldContainExactlyInAnyOrder listOf(2, 1) // out-order; not more184 listOf(0, 3, 0, 4, 0).shouldContainInOrder(3, 4) // possible items in between185 listOf(1) shouldNotContainAnyOf listOf(2, 3) // black list186 listOf(1, 2, 3) shouldContainAll listOf(3, 2) // out-order; more187 listOf(1, 2, 3).shouldBeUnique() // no duplicates188 listOf(1, 2, 2).shouldContainDuplicates() // at least one duplicate189 listOf(1, 2).shouldNotHaveElementAt(1, 3)190 listOf(1, 2) shouldStartWith 1191 listOf(1, 2) shouldEndWith 2192 listOf(1, 2) shouldContainAnyOf listOf(2, 3)193 val x = SomeType(1)194 x shouldBeOneOf listOf(x) // by reference/instance195 x shouldBeIn listOf(SomeType(1)) // by equality/structural196 listOf(1, 2, null).shouldContainNull()197 listOf(1) shouldHaveSize 1198 listOf(1).shouldBeSingleton() // size == 1199 listOf(1).shouldBeSingleton {200 it.shouldBeOdd()201 }202 listOf(1).shouldHaveSingleElement {203 beOdd().test(it).passed() // have to return a boolean here :-/204 }205 listOf(2, 3) shouldHaveLowerBound 1 shouldHaveUpperBound 4206 listOf(1) shouldBeSmallerThan listOf(1, 2)207 listOf(1) shouldBeSameSizeAs listOf(2)208 listOf(1, 2) shouldHaveAtLeastSize 1209 listOf(1, 2).shouldBeSorted()210 mapOf(1 to "a").shouldContain(1, "a") // at least this211 mapOf(1 to "a") shouldContainAll mapOf(1 to "a") // at least those212 mapOf(1 to "a") shouldContainExactly mapOf(1 to "a") // not more213 mapOf(1 to "a") shouldContainKey 1214 emptyMap<Any, Any>().shouldBeEmpty()215 }216 it("exceptions") {217 class SubException() : Exception()...
StringIT.kt
Source:StringIT.kt
1package io.github.serpro69.kfaker.provider2import io.github.serpro69.kfaker.faker3import io.github.serpro69.kfaker.provider.misc.StringProvider4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.core.test.TestCase6import io.kotest.matchers.collections.shouldNotContainAnyOf7import io.kotest.matchers.ints.shouldBeLessThan8import io.kotest.matchers.should9import io.kotest.matchers.shouldBe10import io.kotest.matchers.string.beLowerCase11import io.kotest.matchers.string.beUpperCase12import io.kotest.matchers.string.shouldContain13import io.kotest.matchers.string.shouldNotContain14class StringIT : DescribeSpec() {15 private val faker = faker {16 fakerConfig {17 uniqueGeneratorRetryLimit = 100018 }19 }20 private val sourceString = "foo###bar???"21 override fun beforeEach(testCase: TestCase) {22 faker.unique.clearAll()23 faker.string.unique.clearAll()24 }25 init {26 describe("String Provider") {27 context("numerify") {28 it("should NOT contain # chars") {29 faker.string.numerify(sourceString) shouldNotContain "#"30 faker.string.numerify(sourceString) shouldContain "?"31 }32 it("can have duplicates") {33 val list = List(1000) { faker.string.numerify(sourceString) }34 list.distinct().size shouldBeLessThan 100035 }...
PasswordGeneratorSpec.kt
Source:PasswordGeneratorSpec.kt
...13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package br.com.colman.password17import io.kotest.assertions.throwables.shouldNotThrowAny18import io.kotest.core.spec.style.FunSpec19import io.kotest.inspectors.forOne20import io.kotest.matchers.char.shouldBeInRange21import io.kotest.matchers.collections.shouldBeIn22import io.kotest.matchers.collections.shouldBeUnique23import io.kotest.matchers.shouldBe24import io.kotest.matchers.string.shouldHaveLength25import io.kotest.matchers.string.shouldMatch26import kotlin.random.Random27private val oracleAcceptedSpecialCharacters = """@%+\/'!#$^?:,(){?[]~`-_.""".toList()28class PasswordGeneratorSpec : FunSpec({29 val target = PasswordGenerator()30 test("Sanity check") {31 val myRandom = Random(19980209)32 val generator = PasswordGenerator(myRandom)33 generator.generate() shouldBe "9Lrwc'iy"34 generator.generate() shouldBe "f{uzP9pc"35 generator.generate() shouldBe "aQ)owQ1J"36 }37 context("Default generator") {38 val defaultGenerator = PasswordGenerator.Default39 test("Uses AZ az characters") {40 val pw = defaultGenerator.generate(includeSpecial = false, includeNumber = false)41 pw shouldMatch Regex("[a-zA-Z]+")42 }43 test("Includes a special characters") {44 val pw = defaultGenerator.generate(includeSpecial = true, includeNumber = false)45 shouldNotThrowAny {46 pw.single { it in oracleAcceptedSpecialCharacters }47 }48 }49 test("Includes a number") {50 val pw = defaultGenerator.generate(includeSpecial = false, includeNumber = true)51 pw.forOne { it shouldBeInRange '0'..'9' }52 }53 }54 test("Generates password with the desired size") {55 target.generate(size = 12) shouldHaveLength 1256 target.generate(size = 25) shouldHaveLength 2557 }58 test("Defaults to size 8") {59 target.generate() shouldHaveLength 860 }61 test("Generates random, different passwords") {62 val passwords = List(100) { target.generate() }63 passwords.shouldBeUnique()64 }65})...
FindPairWithSumSpec.kt
Source:FindPairWithSumSpec.kt
1package day120202import seriesSum3import io.kotest.core.spec.style.FunSpec4import io.kotest.datatest.withData5import io.kotest.matchers.collections.shouldBeEmpty6import io.kotest.matchers.collections.shouldBeIn7import io.kotest.matchers.collections.shouldBeUnique8import io.kotest.matchers.shouldBe9import io.kotest.property.Arb10import io.kotest.property.arbitrary.*11import io.kotest.property.checkAll12import io.kotest.property.forAll13val rangeAtLeastTwoNumbersLong = Arb.int(min = 1).map { (0..1).toList() }14val pairOfPositiveInts: Arb<Pair<Int, Int>> =15 Arb.bind(Arb.positiveInt(), Arb.positiveInt()) { x, y -> Pair(x, y) }16class FindPairWithSumSpec : FunSpec({17 context("returns null with less than 2 elements") {18 withData(listOf(), listOf(0)) { rows -> Unit19 checkAll<Int> { i ->20 rows.pairsWithSum(i).shouldBeEmpty()21 }22 }23 }24 test("findPairWithSum returns pair of numbers that sum to specified amount") {25 checkAll(Arb.list(Arb.int()), pairOfPositiveInts) { otherNumbers, pair ->26 val sum = pair.first + pair.second27 pair shouldBeIn (listOf(pair.first, pair.second) + otherNumbers).pairsWithSum(sum)28 }29 }30 context("pairPermutations") {31 context("properties") {32 test("list with size of 2 is a single pair") {33 checkAll<Int, Int> { x, y ->34 listOf(x, y).pairPermutations shouldBe listOf(Pair(x, y))35 }36 }37 test("number of pairs is fibonacci of collection size - 1") {38 checkAll(rangeAtLeastTwoNumbersLong) { xs ->39 xs.pairPermutations.size shouldBe (xs.size - 1).seriesSum40 }41 }42 test("all pairs unique") {43 checkAll(rangeAtLeastTwoNumbersLong) { xs ->44 xs.pairPermutations.shouldBeUnique()45 }46 }47 test("all elements present") {48 forAll(rangeAtLeastTwoNumbersLong) { xs ->49 val permutations = xs.pairPermutations50 xs.all { x ->51 permutations.find { pair -> x == pair.first || x == pair.second } != null52 }53 }54 }55 }56 }57})...
CollectionsTest.kt
Source:CollectionsTest.kt
1package dev.madetobuild.typedconfig.test2import dev.madetobuild.typedconfig.runtime.key.keyWithName3import dev.madetobuild.typedconfig.runtime.source.Source4import io.kotest.matchers.nulls.beNull5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.mockk.every8import io.mockk.impl.annotations.MockK9import io.mockk.junit5.MockKExtension10import io.mockk.verify11import io.mockk.verifyAll12import org.junit.jupiter.api.Test13import org.junit.jupiter.api.extension.ExtendWith14@ExtendWith(MockKExtension::class)15class CollectionsTest {16 @MockK17 lateinit var source: Source18 @Test19 fun requiredStringList() {20 every { source.getList(any()) } returns listOf("root", "me")...
DiscoveryRoundTripTest.kt
Source:DiscoveryRoundTripTest.kt
...14 * You should have received a copy of the GNU Lesser General Public License15 * along with bowler-kernel. If not, see <https://www.gnu.org/licenses/>.16 */17package com.commonwealthrobotics.bowlerkernel.kerneldiscovery18import io.kotest.matchers.booleans.shouldBeFalse19import io.kotest.matchers.collections.shouldBeEmpty20import io.kotest.matchers.collections.shouldContainExactly21import io.kotest.matchers.comparables.shouldNotBeEqualComparingTo22import io.kotest.matchers.shouldBe23import io.kotest.matchers.string.shouldStartWith24import org.junit.jupiter.api.Test25internal class DiscoveryRoundTripTest {26 @Test27 fun `scan the local host with no servers running`() {28 NameClient.scan().shouldBeEmpty()29 }30 @Test31 fun `scan the local host with one server running`() {32 val name = "kernel"33 val grpcPort = 5392934 val ns = NameServer(name, getGrpcPort = { grpcPort })35 ns.ensureStarted()36 ns.name.shouldBe(name)37 while (!ns.isRunning.get()) { Thread.sleep(10) }...
DailyBalancesPropKotestTest.kt
Source:DailyBalancesPropKotestTest.kt
1import io.kotest.core.spec.style.DescribeSpec2import io.kotest.matchers.booleans.shouldBeFalse3import io.kotest.matchers.collections.shouldBeStrictlyIncreasingWith4import io.kotest.matchers.collections.shouldContainAll5import io.kotest.matchers.shouldBe6import io.kotest.property.Arb7import io.kotest.property.PropTestConfig8import io.kotest.property.arbitrary.*9import io.kotest.property.checkAll10import java.time.LocalDate11class DailyBalancesPropKotestTest : DescribeSpec({12 describe("properties") {13 val arbBalance = Arb.bind(Arb.localDate(), Arb.bigDecimal()) { d, a -> Balance(d, a) }14 fun arbUniqueList(minLength: Int = 0) = Arb.list(arbBalance, minLength..100).map { it.distinctBy { it.date } }15 it("contains all original elements") {16 checkAll(PropTestConfig(outputClassifications = true), arbUniqueList()) { list ->17 val result = list.expandToDaily()18 result.shouldContainAll(list)19 }20 }21 it("result is strictly increasing by the balance date") {22 checkAll(arbUniqueList()) { list ->23 val result = list.expandToDaily()24 result.shouldBeStrictlyIncreasingWith(compareBy { it.date })25 }...
TokenCreatorTest.kt
Source:TokenCreatorTest.kt
1package io.github.servb.eShop.auth2import io.github.servb.eShop.auth.util.TokenCreator3import io.github.servb.eShop.util.kotest.*4import io.github.servb.eShop.util.ktor.withTestApplication5import io.kotest.core.spec.style.BehaviorSpec6import io.kotest.core.spec.style.FreeSpec7import io.kotest.matchers.collections.shouldBeUnique8import io.kotest.matchers.shouldBe9import io.kotest.matchers.string.shouldEndWith10import io.kotest.matchers.string.shouldNotBeEmpty11import io.ktor.application.Application12import io.ktor.http.HttpMethod13import io.ktor.http.HttpStatusCode14import io.ktor.server.testing.handleRequest15class TokenCreatorTest : FreeSpec({16 "test token creator" - {17 "generated token should be non-empty" {18 TokenCreator.createToken().shouldNotBeEmpty()19 }20 "generated tokens should be different" {21 val tokens = (1..10000).map { TokenCreator.createToken() }22 tokens.shouldBeUnique()23 }24 }25})
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!!