How to use equalityMatcher method of io.kotest.matchers.should class

Best Kotest code snippet using io.kotest.matchers.should.equalityMatcher

predef-test.kt

Source:predef-test.kt Github

copy

Full Screen

...11import arrow.core.validNel12import io.kotest.assertions.fail13import io.kotest.matchers.Matcher14import io.kotest.matchers.MatcherResult15import io.kotest.matchers.equalityMatcher16import io.kotest.property.Arb17import io.kotest.property.arbitrary.bind18import io.kotest.property.arbitrary.char19import io.kotest.property.arbitrary.choice20import io.kotest.property.arbitrary.choose21import io.kotest.property.arbitrary.constant22import io.kotest.property.arbitrary.int23import io.kotest.property.arbitrary.list24import io.kotest.property.arbitrary.long25import io.kotest.property.arbitrary.map26import io.kotest.property.arbitrary.string27import kotlinx.coroutines.Dispatchers28import kotlinx.coroutines.flow.Flow29import kotlinx.coroutines.flow.asFlow30import kotlin.coroutines.Continuation31import kotlin.coroutines.CoroutineContext32import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED33import kotlin.coroutines.intrinsics.intercepted34import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn35import kotlin.coroutines.resume36import kotlin.coroutines.startCoroutine37import kotlinx.coroutines.channels.Channel38import kotlinx.coroutines.flow.buffer39import kotlinx.coroutines.flow.channelFlow40import kotlinx.coroutines.flow.emptyFlow41public data class SideEffect(var counter: Int = 0) {42 public fun increment() {43 counter++44 }45}46public fun <A> Arb.Companion.flow(arbA: Arb<A>): Arb<Flow<A>> =47 Arb.choose(48 10 to Arb.list(arbA).map { it.asFlow() },49 10 to Arb.list(arbA).map { channelFlow { it.forEach { send(it) } }.buffer(Channel.RENDEZVOUS) },50 1 to Arb.constant(emptyFlow()),51 )52public fun Arb.Companion.throwable(): Arb<Throwable> =53 Arb.string().map(::RuntimeException)54public fun <L, R> Arb.Companion.either(left: Arb<L>, right: Arb<R>): Arb<Either<L, R>> {55 val failure: Arb<Either<L, R>> = left.map { l -> l.left() }56 val success: Arb<Either<L, R>> = right.map { r -> r.right() }57 return Arb.choice(failure, success)58}59public fun <L, R> Arb.Companion.validated(left: Arb<L>, right: Arb<R>): Arb<Validated<L, R>> {60 val failure: Arb<Validated<L, R>> = left.map { l -> l.invalid() }61 val success: Arb<Validated<L, R>> = right.map { r -> r.valid() }62 return Arb.choice(failure, success)63}64public fun <L, R> Arb.Companion.validatedNel(left: Arb<L>, right: Arb<R>): Arb<ValidatedNel<L, R>> {65 val failure: Arb<ValidatedNel<L, R>> = left.map { l -> l.invalidNel() }66 val success: Arb<ValidatedNel<L, R>> = right.map { r -> r.validNel() }67 return Arb.choice(failure, success)68}69public fun Arb.Companion.intRange(min: Int = Int.MIN_VALUE, max: Int = Int.MAX_VALUE): Arb<IntRange> =70 Arb.bind(Arb.int(min, max), Arb.int(min, max)) { a, b ->71 if (a < b) a..b else b..a72 }73public fun Arb.Companion.longRange(min: Long = Long.MIN_VALUE, max: Long = Long.MAX_VALUE): Arb<LongRange> =74 Arb.bind(Arb.long(min, max), Arb.long(min, max)) { a, b ->75 if (a < b) a..b else b..a76 }77public fun Arb.Companion.charRange(): Arb<CharRange> =78 Arb.bind(Arb.char(), Arb.char()) { a, b ->79 if (a < b) a..b else b..a80 }81public fun <O> Arb.Companion.function(arb: Arb<O>): Arb<() -> O> =82 arb.map { { it } }83public fun Arb.Companion.unit(): Arb<Unit> =84 Arb.constant(Unit)85public fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> =86 arb.map { b: B -> { _: A -> b } }87public fun <A> Arb.Companion.nullable(arb: Arb<A>): Arb<A?> =88 Arb.Companion.choice(arb, arb.map { null })89/** Useful for testing success & error scenarios with an `Either` generator **/90public fun <A> Either<Throwable, A>.rethrow(): A =91 fold({ throw it }, ::identity)92public fun <A> Result<A>.toEither(): Either<Throwable, A> =93 fold({ a -> Either.Right(a) }, { e -> Either.Left(e) })94public suspend fun Throwable.suspend(): Nothing =95 suspendCoroutineUninterceptedOrReturn { cont ->96 suspend { throw this }.startCoroutine(97 Continuation(Dispatchers.Default) {98 cont.intercepted().resumeWith(it)99 }100 )101 COROUTINE_SUSPENDED102 }103public suspend fun <A> A.suspend(): A =104 suspendCoroutineUninterceptedOrReturn { cont ->105 suspend { this }.startCoroutine(106 Continuation(Dispatchers.Default) {107 cont.intercepted().resumeWith(it)108 }109 )110 COROUTINE_SUSPENDED111 }112public fun <A> A.suspended(): suspend () -> A =113 suspend { suspend() }114/**115 * Example usage:116 * ```kotlin117 * import arrow.fx.coroutines.assertThrowable118 *119 * fun main() {120 * val exception = assertThrowable<IllegalArgumentException> {121 * throw IllegalArgumentException("Talk to a duck")122 * }123 * require("Talk to a duck" == exception.message)124 * }125 * ```126 * <!--- KNIT example-predef-test-01.kt -->127 * @see Assertions.assertThrows128 */129public inline fun <A> assertThrowable(executable: () -> A): Throwable {130 val a = try {131 executable.invoke()132 } catch (e: Throwable) {133 e134 }135 return if (a is Throwable) a else fail("Expected an exception but found: $a")136}137public suspend fun CoroutineContext.shift(): Unit =138 suspendCoroutineUninterceptedOrReturn { cont ->139 suspend { this }.startCoroutine(140 Continuation(this) {141 cont.resume(Unit)142 }143 )144 COROUTINE_SUSPENDED145 }146public fun leftException(e: Throwable): Matcher<Either<Throwable, *>> =147 object : Matcher<Either<Throwable, *>> {148 override fun test(value: Either<Throwable, *>): MatcherResult =149 when (value) {150 is Either.Left -> when {151 value.value::class != e::class -> MatcherResult(152 false,153 "Expected exception of type ${e::class} but found ${value.value::class}",154 "Should not be exception of type ${e::class}"155 )156 value.value.message != e.message -> MatcherResult(157 false,158 "Expected exception with message ${e.message} but found ${value.value.message}",159 "Should not be exception with message ${e.message}"160 )161 else -> MatcherResult(162 true,163 "Expected exception of type ${e::class} and found ${value.value::class}",164 "Expected exception of type ${e::class} and found ${value.value::class}"165 )166 }167 is Either.Right -> MatcherResult(168 false,169 "Expected Either.Left with exception of type ${e::class} and found Right with ${value.value}",170 "Should not be Either.Left with exception"171 )172 }173 }174public fun <A> either(e: Either<Throwable, A>): Matcher<Either<Throwable, A>> =175 object : Matcher<Either<Throwable, A>> {176 override fun test(value: Either<Throwable, A>): MatcherResult =177 when (value) {178 is Either.Left -> when {179 value.value::class != (e.swap().orNull() ?: Int)::class -> MatcherResult(180 false,181 "Expected $e but found $value",182 "Should not be $e"183 )184 value.value.message != (e.swap().orNull()?.message ?: -1) -> MatcherResult(185 false,186 "Expected $e but found $value",187 "Should not be $e"188 )189 else -> MatcherResult(190 true,191 "Expected exception of type ${e::class} and found ${value.value::class}",192 "Expected exception of type ${e::class} and found ${value.value::class}"193 )194 }195 is Either.Right -> equalityMatcher(e).test(value)196 }197 }...

Full Screen

Full Screen

TestUtils.kt

Source:TestUtils.kt Github

copy

Full Screen

2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html3 */4package net.sourceforge.pmd.lang.ast.test5import io.kotest.matchers.Matcher6import io.kotest.matchers.equalityMatcher7import io.kotest.matchers.should8import net.sourceforge.pmd.Report9import net.sourceforge.pmd.RuleViolation10import net.sourceforge.pmd.lang.ast.Node11import kotlin.reflect.KCallable12import kotlin.reflect.jvm.isAccessible13import kotlin.test.assertEquals14/**15 * Extension to add the name of a property to error messages.16 *17 * @see [shouldBe].18 */19infix fun <N, V : N> KCallable<N>.shouldEqual(expected: V?) =20 assertWrapper(this, expected) { n, v ->21 // using shouldBe would perform numeric conversion22 // eg (3.0 shouldBe 3L) passes, even though (3.0 != 3L)23 // equalityMatcher doesn't do this conversion24 n.should(equalityMatcher(v) as Matcher<N>)25 }26private fun <N, V> assertWrapper(callable: KCallable<N>, right: V, asserter: (N, V) -> Unit) {27 fun formatName() = "::" + callable.name.removePrefix("get").decapitalize()28 val value: N = try {29 callable.isAccessible = true30 callable.call()31 } catch (e: Exception) {32 throw RuntimeException("Couldn't fetch value for property ${formatName()}", e)33 }34 try {35 asserter(value, right)36 } catch (e: AssertionError) {37 if (e.message?.contains("expected:") == true) {38 // the exception has no path, let's add one...

Full Screen

Full Screen

should.kt

Source:should.kt Github

copy

Full Screen

...24@Suppress("UNCHECKED_CAST")25infix fun <T> T.shouldNotBe(any: Any?) {26 when (any) {27 is Matcher<*> -> shouldNot(any as Matcher<T>)28 else -> shouldNot(equalityMatcher(any))29 }30}31infix fun <T> T.shouldHave(matcher: Matcher<T>) = should(matcher)32infix fun <T> T.should(matcher: Matcher<T>) {33 assertionCounter.inc()34 val result = matcher.test(this)35 if (!result.passed()) {36 errorCollector.collectOrThrow(failure(result.failureMessage()))37 }38}39infix fun <T> T.shouldNotHave(matcher: Matcher<T>) = shouldNot(matcher)40infix fun <T> T.shouldNot(matcher: Matcher<T>) = should(matcher.invert())41infix fun <T> T.should(matcher: (T) -> Unit) = matcher(this)42fun <T> be(expected: T) = equalityMatcher(expected)43fun <T> equalityMatcher(expected: T) = object : Matcher<T> {44 override fun test(value: T): MatcherResult {45 val t = if (value == null && expected == null) {46 null47 } else if (value == null && expected != null) {48 actualIsNull(expected)49 } else if (value != null && expected == null) {50 expectedIsNull(value)51 } else {52 eq(value, expected)53 }54 return MatcherResult(55 t == null,56 {57 val e = Expected(expected.show())...

Full Screen

Full Screen

WordSpecExt.kt

Source:WordSpecExt.kt Github

copy

Full Screen

...7import com.github.h0tk3y.betterParse.parser.tryParseToEnd8import io.kotest.assertions.throwables.shouldNotThrowAny9import io.kotest.assertions.throwables.shouldThrow10import io.kotest.core.spec.style.scopes.WordSpecShouldContainerScope11import io.kotest.matchers.equalityMatcher12import io.kotest.matchers.should13import io.kotest.matchers.shouldBe14import io.kotest.matchers.shouldNotHave15import ru.tesserakt.kodept.core.AST16import ru.tesserakt.kodept.core.convert17import ru.tesserakt.kodept.lexer.Lexer18suspend fun <T : Any, U : T> WordSpecShouldContainerScope.test(parser: Parser<T>, element: String, shouldParse: U?) =19 element.let {20 if (it.length > 20) "${it.take(20)}..." else it21 }.invoke {22 val lexer = Lexer()23 val tokens = lexer.tokenize(element)24 tokens shouldNotHave equalityMatcher(noneMatched)25 parser.tryParseToEnd(tokens, 0) should {26 when (shouldParse) {27 null -> shouldThrow<ParseException> { it.toParsedOrThrow() }28 else -> shouldNotThrowAny { it.toParsedOrThrow() }.value shouldBe shouldParse29 }30 }31 }32suspend fun <T : RLT.Node, V : AST.Node> WordSpecShouldContainerScope.test(33 parser: Parser<T>,34 element: String,35 shouldParse: V?,36) = test(parser.map(RLT.Node::convert).map { node ->37 fun AST.Node.dfs(f: (AST.Node) -> Unit) {38 val nodeList = ArrayDeque(listOf(this))...

Full Screen

Full Screen

zoneddatetime.kt

Source:zoneddatetime.kt Github

copy

Full Screen

1package io.kotest.matchers.date2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.equalityMatcher5import io.kotest.matchers.should6import io.kotest.matchers.shouldNot7import java.time.ZonedDateTime8fun beInTodayZDT() = object : Matcher<ZonedDateTime> {9 override fun test(value: ZonedDateTime): MatcherResult {10 val passed = value.toLocalDate() == ZonedDateTime.now().toLocalDate()11 return MatcherResult(12 passed,13 { "$value should be today" },14 {15 "$value should not be today"16 })17 }18}19/**20 * Asserts that the ZonedDateTime does not have a date component of today21 *22 * ```23 * ZonedDateTime.of(2009, Month.APRIL, 2,2,2).shouldNotBeToday() // Assertion passes24 * ```25 */26fun ZonedDateTime.shouldNotBeToday() = this shouldNot beInTodayZDT()27/**28 * Asserts that the ZonedDateTime has a date component of today29 *30 * ```31 * ZonedDateTime.now().shouldBeToday() // Assertion passes32 * ```33 */34fun ZonedDateTime.shouldBeToday() = this should beInTodayZDT()35/**36 * Matcher that uses `actual` timezone on the `expected` ZonedDateTime37 *38 *39 * ```40 * ZonedDateTime.of(2019, 12, 10, 10, 0, 0, 0, ZoneOffset.UTC) shouldBe41 * ZonedDateTime.of(2019, 12, 10, 4, 0, 0, 0, ZoneId.of("America/Chicago")).atSameZone() // Assertion passes42 * ```43 */44fun ZonedDateTime.atSameZone() = object : Matcher<ZonedDateTime> {45 override fun test(value: ZonedDateTime): MatcherResult = equalityMatcher(withZoneSameInstant(value.zone)).test(value)46}...

Full Screen

Full Screen

equalityMatcher

Using AI Code Generation

copy

Full Screen

1 "list should be equal" {2 val list1 = listOf(1, 2, 3)3 val list2 = listOf(1, 2, 3)4 list1 should equalityMatcher(list2)5 }6 "list should be equal" {7 val list1 = listOf(1, 2, 3)8 val list2 = listOf(1, 2, 3)9 list1 should equalityMatcher(list2)10 }11}

Full Screen

Full Screen

equalityMatcher

Using AI Code Generation

copy

Full Screen

1a should equal (b)2a should beGreaterThanOrEqual (b)3a should beLessThanOrEqual (b)4a should beInRange (1..3)5a should beOdd ()6a should beEven ()7a should beZero ()8a should bePositive ()9a should beNegative ()10a should beOneOf (1, 2, 3)11a should beOneOf (1..3)12a should beOneOf (listOf(1, 2, 3))13a should beOneOf (arrayOf(1, 2, 3))14a should beOneOf (setOf(1, 2, 3))15a should beOneOf (sequenceOf(1, 2, 3))16a should beOneOf (intArrayOf(1, 2, 3))

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.

Run Kotest automation tests on LambdaTest cloud grid

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

Most used method in should

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful