Best Kotest code snippet using io.kotest.matchers.optional.matchers.Optional.shouldNotBeEmpty
ArgumentsTest.kt
Source:ArgumentsTest.kt
1package de.qualersoft.robotframework.gradleplugin.utils2import io.kotest.assertions.assertSoftly3import io.kotest.core.spec.style.AnnotationSpec4import io.kotest.matchers.Matcher5import io.kotest.matchers.MatcherResult6import io.kotest.matchers.collections.shouldHaveElementAt7import io.kotest.matchers.collections.shouldHaveSize8import io.kotest.matchers.should9import io.kotest.matchers.shouldNot10import java.io.File11class ArgumentsTest : AnnotationSpec() {12 private lateinit var sut: Arguments13 @BeforeEach14 fun setupTest() {15 sut = Arguments()16 }17 @Test18 fun givenNewArgumentsThenResultIsEmpty() {19 sut.shouldBeEmpty()20 }21 @Test22 fun whenAddingAnOptionalFileOfNullThenResultIsEmpty() {23 sut.addOptionalFile(null, "a")24 sut.shouldBeEmpty()25 }26 @Test27 fun whenAddingAnOptionalNonNullFileThenResultIsNotEmpty() {28 sut.addOptionalFile(File("./test"), "a")29 assertSoftly {30 sut.shouldNotBeEmpty()31 val arr = sut.toArray()32 arr.shouldHaveSize(2)33 arr.shouldHaveElementAt(0, "a")34 arr.shouldHaveElementAt(1, File("./test").path)35 }36 }37 @Test38 fun whenAddingNullFileThenItsConvertedToNone() {39 sut.addFileToArguments(null, "f")40 assertSoftly {41 sut.shouldNotBeEmpty()42 val arr = sut.toArray()43 arr.shouldHaveSize(2)44 arr.shouldHaveElementAt(0, "f")45 arr.shouldHaveElementAt(1, "NONE")46 }47 }48 @Test49 fun whenAddingNonEmptyFileThenItWillBeInResult() {50 sut.addFileToArguments(File("./test"), "f")51 assertSoftly {52 sut.shouldNotBeEmpty()53 val arr = sut.toArray()54 arr.shouldHaveSize(2)55 arr.shouldHaveElementAt(0, "f")56 arr.shouldHaveElementAt(1, File("./test").path)57 }58 }59 @Test60 fun whenAddingEmptyFileThenItsNotInResult() {61 sut.addFileToArguments(File(""), "f")62 sut.shouldBeEmpty()63 }64 @Test65 fun whenAddingNullStringThenItsNotInResult() {66 sut.addNonEmptyStringToArguments(null, "s")67 sut.shouldBeEmpty()68 }69 @Test70 fun whenAddingEmptyStringThenItsNotInResult() {71 sut.addNonEmptyStringToArguments("", "s")72 sut.shouldBeEmpty()73 }74 @Test75 fun whenAddingNonEmptyStringThenItsInResult() {76 sut.addNonEmptyStringToArguments("notEmpty", "s")77 assertSoftly {78 sut.shouldNotBeEmpty()79 val arr = sut.toArray()80 arr.shouldHaveSize(2)81 arr.shouldHaveElementAt(0, "s")82 arr.shouldHaveElementAt(1, "notEmpty")83 }84 }85 @Test86 fun whenAddingEmptyMapThenItsNotInResult() {87 sut.addMapToArguments(mapOf(), "m")88 sut.shouldBeEmpty()89 }90 @Test91 fun whenAddingMapThenItsInResult() {92 sut.addMapToArguments(mapOf("key" to "val"), "m")93 assertSoftly {94 sut.shouldNotBeEmpty()95 val arr = sut.toArray()96 arr.shouldHaveSize(2)97 arr.shouldHaveElementAt(0, "m")98 arr.shouldHaveElementAt(1, "key:val")99 }100 }101 @Test102 fun whenAddingMultiMapThenItsInResult() {103 sut.addMapToArguments(mapOf("key1" to "val1", "key2" to "val2"), "m")104 assertSoftly {105 sut.shouldNotBeEmpty()106 val arr = sut.toArray()107 arr.shouldHaveSize(4)108 arr.shouldHaveElementAt(0, "m")109 arr.shouldHaveElementAt(1, "key1:val1")110 arr.shouldHaveElementAt(2, "m")111 arr.shouldHaveElementAt(3, "key2:val2")112 }113 }114 @Test115 fun whenAddingNullFlagThenItsNotInResult() {116 sut.addFlagToArguments(null, "b")117 sut.shouldBeEmpty()118 }119 @Test120 fun whenAddingFalseFlagThenItsNotInResult() {121 sut.addFlagToArguments(false, "b")122 sut.shouldBeEmpty()123 }124 @Test125 fun whenAddingTrueFlagThenItsInResult() {126 sut.addFlagToArguments(true, "b")127 assertSoftly {128 sut.shouldNotBeEmpty()129 val arr = sut.toArray()130 arr.shouldHaveSize(1)131 arr.shouldHaveElementAt(0, "b")132 }133 }134 @Test135 fun whenAddingOptionalNullStringThenItsNotInResult() {136 sut.addStringToArguments(null, "s")137 sut.shouldBeEmpty()138 }139 @Test140 fun whenAddingOptionalEmptyStringThenItsInResult() {141 sut.addStringToArguments("", "s")142 assertSoftly {143 sut.shouldNotBeEmpty()144 val arr = sut.toArray()145 arr.shouldHaveSize(2)146 arr.shouldHaveElementAt(0, "s")147 arr.shouldHaveElementAt(1, "")148 }149 }150 @Test151 fun whenAddingOptionalNonEmptyStringThenItsInResult() {152 sut.addStringToArguments("NotEmpty", "s")153 assertSoftly {154 sut.shouldNotBeEmpty()155 val arr = sut.toArray()156 arr.shouldHaveSize(2)157 arr.shouldHaveElementAt(0, "s")158 arr.shouldHaveElementAt(1, "NotEmpty")159 }160 }161 @Test162 fun whenAddNullStringListThenItsNotInResult() {163 sut.addListToArguments(null as String?, "s")164 sut.shouldBeEmpty()165 }166 @Test167 fun whenAddStringListThenItsInResult() {168 sut.addListToArguments("aString", "s")169 assertSoftly {170 sut.shouldNotBeEmpty()171 val arr = sut.toArray()172 arr.shouldHaveSize(2)173 arr.shouldHaveElementAt(0, "s")174 arr.shouldHaveElementAt(1, "aString")175 }176 }177 @Test178 fun whenAddingMultiStringListThenEachIsInResult() {179 sut.addListToArguments("str1, str2", "s")180 assertSoftly {181 sut.shouldNotBeEmpty()182 val arr = sut.toArray()183 arr.shouldHaveSize(4)184 arr.shouldHaveElementAt(0, "s")185 arr.shouldHaveElementAt(1, "str1")186 arr.shouldHaveElementAt(2, "s")187 arr.shouldHaveElementAt(3, "str2")188 }189 }190 @Test191 fun whenAddingNullListThenItsNotInResult() {192 sut.addListToArguments(null as List<String?>?, "ls")193 sut.shouldBeEmpty()194 }195 @Test196 fun whenAddingEmptyListThenItsNotInResult() {197 sut.addListToArguments(listOf(), "ls")198 sut.shouldBeEmpty()199 }200 @Test201 fun whenAddingListWithNullThenItsNotInResult() {202 sut.addListToArguments(listOf<String?>(null), "ls")203 sut.shouldBeEmpty()204 }205 @Test206 fun whenAddingListWithEmptyThenItsNotInResult() {207 sut.addListToArguments(listOf(""), "ls")208 sut.shouldBeEmpty()209 }210 @Test211 fun whenAddingListThenItsInResult() {212 sut.addListToArguments(listOf("str"), "ls")213 assertSoftly {214 sut.shouldNotBeEmpty()215 val arr = sut.toArray()216 arr.shouldHaveSize(2)217 arr.shouldHaveElementAt(0, "ls")218 arr.shouldHaveElementAt(1, "str")219 }220 }221 @Test222 fun whenAddingListWithMoreElemsThenEachIsInResult() {223 sut.addListToArguments(listOf("str1", "str2"), "ls")224 assertSoftly {225 sut.shouldNotBeEmpty()226 val arr = sut.toArray()227 arr.shouldHaveSize(4)228 arr.shouldHaveElementAt(0, "ls")229 arr.shouldHaveElementAt(1, "str1")230 arr.shouldHaveElementAt(2, "ls")231 arr.shouldHaveElementAt(3, "str2")232 }233 }234 @Test235 fun whenAddingNullFileListThenItsNotInResult() {236 sut.addFileListToArguments(null, "fl")237 sut.shouldBeEmpty()238 }239 @Test240 fun whenAddingEmptyFileListThenItsNotInResult() {241 sut.addFileListToArguments(listOf(), "fl")242 sut.shouldBeEmpty()243 }244 @Test245 fun whenAddingFileListWithEmptyFileThenItsNotInResult() {246 sut.addFileListToArguments(listOf(File("")), "fl")247 sut.shouldBeEmpty()248 }249 @Test250 fun whenAddingFileListWithFileThenItsInResult() {251 sut.addFileListToArguments(listOf(File("./test")), "fl")252 assertSoftly {253 sut.shouldNotBeEmpty()254 val arr = sut.toArray()255 arr.shouldHaveSize(2)256 arr.shouldHaveElementAt(0, "fl")257 arr.shouldHaveElementAt(1, File("./test").path)258 }259 }260 // <editor-fold desc="Helper extensions">261 private fun beEmpty() = object : Matcher<Arguments> {262 override fun test(value: Arguments) = MatcherResult(263 value.toArray().isEmpty(),264 "Arguments $value should be empty",265 "String $value should not be empty"266 )267 }268 private fun Arguments.shouldBeEmpty() = this should beEmpty()269 private fun Arguments.shouldNotBeEmpty() = this shouldNot beEmpty()270 // </editor-fold>271}...
OptionalMatchersTest.kt
Source:OptionalMatchersTest.kt
1package com.sksamuel.kotest.matchers.optional2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.ShouldSpec4import io.kotest.matchers.optional.beEmpty5import io.kotest.matchers.optional.bePresent6import io.kotest.matchers.optional.shouldBeEmpty7import io.kotest.matchers.optional.shouldBePresent8import io.kotest.matchers.optional.shouldNotBeEmpty9import io.kotest.matchers.optional.shouldNotBePresent10import io.kotest.matchers.should11import io.kotest.matchers.shouldBe12import io.kotest.matchers.shouldNot13import io.kotest.matchers.throwable.shouldHaveMessage14import java.util.Optional15class OptionalMatchersTest : ShouldSpec({16 context("Empty optional") {17 val optional = Optional.empty<Any>()18 should("Be empty") {19 optional.shouldBeEmpty()20 optional should beEmpty()21 }22 should("Not be present") {23 optional.shouldNotBePresent()24 optional shouldNot bePresent()25 }26 should("Fail to be notEmpty") {27 shouldThrow<AssertionError> { optional.shouldNotBeEmpty() }28 shouldThrow<AssertionError> { optional shouldNot beEmpty() }29 }30 should("Fail to be present") {31 shouldThrow<AssertionError> { optional.shouldBePresent() }32 shouldThrow<AssertionError> { optional should bePresent() }33 }34 }35 context("Present optional") {36 val optional = Optional.of("A")37 should("Be present") {38 optional.shouldBePresent()39 optional should bePresent()40 }41 42 should("Return the present value for usage in more assertions") {43 optional.shouldBePresent() shouldBe "A"44 } 45 should("Allow matchers with present value as a receiver") {46 optional shouldBePresent {47 this shouldBe "A"48 }49 }50 51 should("Allow matchers with present value as parameter") {52 optional shouldBePresent {53 it shouldBe "A"54 }55 }56 57 should("Execute code inside the receiver") {58 shouldThrow<RuntimeException> {59 optional shouldBePresent {60 throw RuntimeException("Ensuring the block is actually executed")61 }62 } shouldHaveMessage "Ensuring the block is actually executed"63 64 shouldThrow<AssertionError> {65 optional shouldBePresent {66 this shouldBe "B"67 }68 }69 }70 }71}) ...
matchers.kt
Source:matchers.kt
1package io.kotest.matchers.optional2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6import java.util.Optional7/**8 * Asserts that a [Optional] is empty9 *10 * ```11 * Optional.empty().shouldBeEmpty() // Assertion passes12 *13 * Optional.of("A").shouldBeEmpty() // Assertion fails14 * ```15 */16fun <T> Optional<T>.shouldBeEmpty() = this should beEmpty()17/**18 * Asserts that a [Optional] is not empty19 *20 * ```21 * Optional.of("A").shouldNotBeEmpty() // Assertion passes22 *23 * Optional.empty().shouldNotBeEmpty() // Assertion fails24 * ```25 */26fun <T> Optional<T>.shouldNotBeEmpty() = this shouldNot beEmpty()27/**28 * Matcher to verify whether an [Optional] is empty or not29 */30fun <T> beEmpty() = object : Matcher<Optional<T>> {31 override fun test(value: Optional<T>) = MatcherResult(32 !value.isPresent,33 { "Expected optional to be empty, but instead was ${value.get()}" },34 { "Expected optional to not be empty, but was" }35 )36}37/**38 * Verifies if this [Optional] is present then execute [block] with it's value39 *40 * ```41 * val optional = Optional.of("A")42 *43 * optional shouldBePresent {44 * it shouldBe "A"45 * }46 *47 * ```48 */49infix fun <T> Optional<T>.shouldBePresent(block: T.(value: T) -> Unit): T {50 shouldBePresent()51 get().block(get())52 return get()53}54/**55 * Verifies if this [Optional] is present56 *57 * ```58 * val optional = Optional.of("A")59 *60 * optional.shouldBePresent()61 *62 * ```63 *64 * Further assertions can be made using the returned value65 *66 * ```67 * val optional = Optional.of("A")68 *69 * val present = optional.shouldBePresent()70 * present shouldBe "A"71 * ```72 *73 */74fun <T> Optional<T>.shouldBePresent(): T {75 this should bePresent()76 return get()77}78/**79 * Verifies t hat this Optional contains no value80 */81fun <T> Optional<T>.shouldNotBePresent() = this shouldNot bePresent()82/**83 * Matcher to verify whether a matcher contains a value or not84 */85fun <T> bePresent() = object : Matcher<Optional<T>> {86 override fun test(value: Optional<T>) = MatcherResult(87 value.isPresent,88 { "Expected optional to be present, but was empty instead" },89 { "Expected optional to not be present, but was ${value.get()}" }90 )91}...
Optional.shouldNotBeEmpty
Using AI Code Generation
1import io.kotest.matchers.optional.shouldNotBeEmpty2import io.kotest.matchers.optional.shouldNotBeNull3import io.kotest.matchers.optional.shouldNotBePresent4import io.kotest.matchers.optional.shouldNotBePresent5import io.kotest.matchers.optional.shouldNotContain6import io.kotest.matchers.optional.shouldNotContain7import io.kotest.matchers.optional.shouldNotContain8import io.kotest.matchers.optional.shouldNotContain9import io.kotest.matchers.optional.shouldNotContain10import io.kotest.matchers.optional.shouldNotContain11import io.kotest.matchers.optional.shouldNotContain12import io.kotest.matchers.optional.shouldNotContain13import io.kotest.matchers.optional.shouldNotContain14import io.kotest.matchers.optional.shouldNotContain15import io.kotest.matchers.optional.shouldNotContain
Optional.shouldNotBeEmpty
Using AI Code Generation
1val optional : Optional < String > = Optional . empty ()2optional . shouldNotBeEmpty ()3val optional : Optional < String > = Optional . empty ()4optional . shouldNotBeNull ()5val optional : Optional < String > = Optional . empty ()6optional . shouldNotBePresent ()7val optional : Optional < String > = Optional . empty ()8optional . shouldNotContain ( "value" )9val optional : Optional < String > = Optional . empty ()10optional . shouldNotContainAny ( "value" , "value2" )11val optional : Optional < String > = Optional . empty ()12optional . shouldNotContainAll ( "value" , "value2" )13val optional : Optional < String > = Optional . empty ()14optional . shouldNotContainNull ()15val optional : Optional < String > = Optional . empty ()16optional . shouldNotHaveValue ( "value" )17val optional : Optional < String > = Optional . empty ()18optional . shouldNotHaveValueSatisfying { it . length > 0 }19val optional : Optional < String > = Optional . empty ()20optional . shouldNotHaveValueInstanceOf ( String :: class )21val optional : Optional < String > = Optional . empty ()22optional . shouldNotHaveValueSameInstanceAs ( "value" )
Optional.shouldNotBeEmpty
Using AI Code Generation
1val optional = Optional.of(1)2optional.shouldNotBeEmpty()3val optional = Optional.empty()4optional.shouldBeEmpty()5val optional = Optional.of(1)6optional.shouldHaveValue(1)7val optional = Optional.empty()8optional.shouldHaveValue(1)9val optional = Optional.of(1)10optional.shouldContain(1)11val optional = Optional.empty()12optional.shouldContain(1)13val optional = Optional.of(1)14optional.shouldBePresent()15val optional = Optional.empty()16optional.shouldBePresent()17val optional = Optional.of(1)18optional.shouldBePresent { it shouldBe 1 }19val optional = Optional.empty()20optional.shouldBePresent { it shouldBe 1 }21val optional = Optional.of(1)22optional.shouldBePresent { it shouldBe 2 }23val optional = Optional.empty()24optional.shouldBePresent { it shouldBe 2 }25val optional = Optional.of(1)26optional.shouldBePresent { it shouldBe 2 }27val optional = Optional.empty()28optional.shouldBePresent { it shouldBe 2 }
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!!