Best Kotest code snippet using io.kotest.equals.Equality.name
predef-test.kt
Source:predef-test.kt
...45}46val singleThreadName = "single"47val single = Resource.singleThreadContext(singleThreadName)48val threadName: suspend () -> String =49 { Thread.currentThread().name }50class NamedThreadFactory(private val mkName: (Int) -> String) : ThreadFactory {51 private val count = atomic(0)52 override fun newThread(r: Runnable): Thread =53 Thread(r, mkName(count.value))54 .apply { isDaemon = true }55}56fun unsafeEquals(other: CancelToken): Matcher<CancelToken> = object : Matcher<CancelToken> {57 val env = Environment(EmptyCoroutineContext)58 override fun test(value: CancelToken): MatcherResult {59 val r1 = env.unsafeRunSync { value.cancel.invoke() }60 val r2 = env.unsafeRunSync { other.cancel.invoke() }61 return MatcherResult(r1 == r2, "Expected: $r2 but found: $r1", "$r2 and $r1 should be equal")62 }63}...
CallUrlInfoTest.kt
Source:CallUrlInfoTest.kt
1/*2 * Copyright @ 2018 Atlassian Pty Ltd3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17package org.jitsi.jibri18import io.kotest.core.spec.IsolationMode19import io.kotest.core.spec.style.ShouldSpec20import io.kotest.data.forAll21import io.kotest.data.headers22import io.kotest.data.row23import io.kotest.data.table24import io.kotest.matchers.shouldBe25import io.kotest.matchers.shouldNotBe26class CallUrlInfoTest : ShouldSpec() {27 override fun isolationMode(): IsolationMode? = IsolationMode.InstancePerLeaf28 init {29 context("creating a CallUrlInfo") {30 context("without url params") {31 val info = CallUrlInfo("baseUrl", "callName")32 should("assign the fields correctly") {33 info.baseUrl shouldBe "baseUrl"34 info.callName shouldBe "callName"35 info.callUrl shouldBe "baseUrl/callName"36 }37 }38 context("with url params") {39 val info = CallUrlInfo("baseUrl", "callName", listOf("one", "two", "three"))40 should("assign the fields correctly") {41 info.baseUrl shouldBe "baseUrl"42 info.callName shouldBe "callName"43 info.callUrl shouldBe "baseUrl/callName#one&two&three"44 }45 }46 }47 context("a nullable CallUrlInfo instance") {48 should("not equal null") {49 val nullableInfo: CallUrlInfo? = CallUrlInfo("baseUrl", "callName")50 nullableInfo shouldNotBe null51 }52 }53 context("equality and hashcode") {54 val info = CallUrlInfo("baseUrl", "callName")55 context("a CallUrlInfo instance") {56 should("not equal another type") {57 @Suppress("ReplaceCallWithBinaryOperator")58 info.equals("string") shouldBe false59 }60 context("when compared to other variations") {61 should("be equal/not equal where appropriate") {62 val duplicateInfo = CallUrlInfo("baseUrl", "callName")63 val differentBaseUrl = CallUrlInfo("differentBaseUrl", "callName")64 val differentCallName = CallUrlInfo("differentUrl", "differentCallName")65 val differentBaseUrlCase = CallUrlInfo("BASEURL", "callName")66 val differentCallNameCase = CallUrlInfo("baseUrl", "CALLNAME")67 val withUrlParams = CallUrlInfo("baseUrl", "callName", listOf("one", "two", "three"))68 val t = table(69 headers("left", "right", "shouldEqual"),70 row(info, info, true),71 row(info, duplicateInfo, true),72 row(info, differentBaseUrl, false),73 row(info, differentCallName, false),74 row(info, differentBaseUrlCase, true),75 row(info, differentCallNameCase, true),76 row(info, withUrlParams, true)77 )78 forAll(t) { left, right, shouldEqual ->79 if (shouldEqual) {80 left shouldBe right81 left.hashCode() shouldBe right.hashCode()82 } else {83 left shouldNotBe right84 left.hashCode() shouldNotBe right.hashCode()85 }86 }87 }88 }89 }90 }91 }92}...
DomainEventSpec.kt
Source:DomainEventSpec.kt
1package com.musinsa.shared.domain.aggregate2import com.musinsa.shared.test.matchers.string.beValidISODateTimeString3import com.musinsa.shared.test.matchers.string.beValidUUIDString4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.inspectors.forAll6import io.kotest.matchers.should7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNotBe9import io.kotest.property.checkAll10import io.kotest.property.exhaustive.exhaustive11import java.util.UUID12private class TestDomainEvent(13 aggregateId: String,14 eventId: String? = null,15 occurredOn: String? = null,16) : DomainEvent(aggregateId, eventId, occurredOn) {17 override fun eventName(): String {18 return "TestDomainEvent"19 }20}21private fun createDomainEvent(22 aggregateId: String = UUID.randomUUID().toString(),23 eventId: String? = null,24 occurredOn: String? = null,25): DomainEvent {26 return TestDomainEvent(aggregateId, eventId, occurredOn)27}28private fun DomainEvent.partialChangedEvents(): List<DomainEvent> {29 return listOf<DomainEvent>(30 createDomainEvent(eventId = eventId, occurredOn = occurredOn),31 createDomainEvent(aggregateId = aggregateId, occurredOn = occurredOn),32 createDomainEvent(aggregateId = aggregateId, eventId = eventId)33 )34}35class DomainEventSpec : DescribeSpec(36 {37 val domainEvents = List(100) {38 createDomainEvent()39 }.exhaustive()40 describe("constructor()") {41 it("should initialize an eventId by default value when passed null") {42 domainEvents.checkAll { domainEvent ->43 domainEvent.eventId should beValidUUIDString<String>()44 }45 }46 it("should initialize an occurredOn by default value when passed null") {47 domainEvents.checkAll { domainEvent ->48 domainEvent.occurredOn should beValidISODateTimeString<String>()49 }50 }51 }52 describe("equals()") {53 it("should be able equivalence comparisons") {54 domainEvents.checkAll { domainEvent ->55 val nullObject = null56 (domainEvent == domainEvent) shouldBe true57 (domainEvent == nullObject) shouldBe false58 (domainEvent.equals(object {})) shouldBe false59 with(domainEvent) {60 val clone = createDomainEvent(aggregateId, eventId, occurredOn)61 (this === clone) shouldBe false62 (this == clone) shouldBe true63 partialChangedEvents().forAll { other ->64 (this == other) shouldBe false65 }66 }67 }68 }69 }70 describe("hashCode()") {71 it("should return same value if some objects are checked for equality") {72 domainEvents.checkAll { domainEvent ->73 with(domainEvent) {74 val clone = createDomainEvent(aggregateId, eventId, occurredOn)75 (this == clone) shouldBe true76 hashCode() shouldBe clone.hashCode()77 partialChangedEvents().forAll { other ->78 hashCode() shouldNotBe other.hashCode()79 }80 }81 }82 }83 }84 describe("toString()") {85 it("should return string of properties") {86 domainEvents.checkAll { domainEvent ->87 with(domainEvent) {88 val expected =89 "TestDomainEvent(aggregateId=$aggregateId, eventId=$eventId, occurredOn=$occurredOn)"90 toString() shouldBe expected91 }92 }93 }94 }95 }96)...
TestUtils.kt
Source:TestUtils.kt
...11import 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 one39 throw AssertionError(e.message!!.replace("expected:", "expected property ${formatName()} to be"))40 }41 throw e42 }43}44/**45 * Extension to add the name of the property to error messages.46 * Use with double colon syntax, eg `it::isIntegerLiteral shouldBe true`.47 * For properties synthesized from Java getters starting with "get", you48 * have to use the name of the getter instead of that of the generated49 * property (with the get prefix).50 *51 * If this conflicts with [io.kotest.matchers.shouldBe], use the equivalent [shouldEqual]52 *53 */54infix fun <N, V : N> KCallable<N>.shouldBe(expected: V?) = this.shouldEqual(expected)55infix fun <T> KCallable<T>.shouldMatch(expected: T.() -> Unit) = assertWrapper(this, expected) { n, v -> n should v }56inline fun <reified T> Any?.shouldBeA(f: (T) -> Unit = {}): T {57 if (this is T) {58 f(this)59 return this60 } else throw AssertionError("Expected an instance of ${T::class.java}, got $this")61}62operator fun <T> List<T>.component6() = get(5)...
GoodsSpec.kt
Source:GoodsSpec.kt
...39 it("should return string of properties") {40 goodsArb.checkAll(100) { goods ->41 with(goods) {42 val expected =43 "Goods(no=$no, seqNo=$seqNo, name=$name, brand=$brand, itemCategory=$itemCategory, " +44 "image=$image, status=$status, price=$price, netPrice=$netPrice, " +45 "discountRate=$discountRate, color=$color, createdAt=$createdAt)"46 toString() shouldBe expected47 }48 }49 }50 }51 }52)...
BrandSpec.kt
Source:BrandSpec.kt
...38 describe("toString()") {39 it("should return string of properties") {40 brandArb.checkAll(100) { brand ->41 with(brand) {42 toString() shouldBe "Brand(code=$code, name=$name)"43 }44 }45 }46 }47 }48)...
contain.kt
Source:contain.kt
...28fun <T, C : Collection<T>> contain(t: T, verifier: Equality<T> = Equality.default()) = object : Matcher<C> {29 override fun test(value: C) = MatcherResult(30 value.any { verifier.verify(it, t).areEqual() },31 {32 "Collection should contain element ${t.print().value} based on ${verifier.name()}; " +33 "but the collection is ${value.print().value}"34 },35 { "Collection should not contain element ${t.print().value} based on ${verifier.name()}" }36 )37}...
ObjectEqualsEquality.kt
Source:ObjectEqualsEquality.kt
...4import io.kotest.equals.Equality5open class ObjectEqualsEquality<T>(6 private val strictNumberEquality: Boolean,7) : Equality<T> {8 override fun name(): String = "object equality"9 override fun verify(actual: T, expected: T): EqualityResult {10 val throwable = eq(actual, expected, strictNumberEquality) ?: return EqualityResult.equal(actual, expected, this)11 return EqualityResult.notEqual(actual, expected, this).let { result ->12 throwable.message?.let { message ->13 result.withDetails { message }14 } ?: result15 }16 }17}18fun <T> Equality.Companion.byObjectEquality(19 strictNumberEquality: Boolean = false,20) = ObjectEqualsEquality<T>(21 strictNumberEquality = strictNumberEquality,22)...
name
Using AI Code Generation
1 import io.kotest.matchers.equality.defaultName2 import io.kotest.matchers.equality.defaultNameOrNull3 import io.kotest.matchers.equality.name4 import io.kotest.matchers.equality.nameOrNull5 import io.kotest.assertions.show.show6 import io.kotest.assertions.show.showOrNull7 import io.kotest.assertions.throwables.throwableName8 import io.kotest.assertions.throwables.throwableNameOrNull
name
Using AI Code Generation
1val equality = Equality.name()2val equality = Equality.all()3val equality = Equality.equals()4val equality = Equality.equalsIgnoreCase()5val equality = Equality.toString()6val equality = Equality.identity()
name
Using AI Code Generation
1val equality = Equality { a, b -> a.name == b.name }2val result = equality.equals(Person("sam"), Person("sam"))3println(result)4val equality = Equality { a, b -> a.name == b.name }5val result = equality.equals(Person("sam"), Person("sam"))6println(result)7val equality = Equality { a, b -> a.name == b.name }8val result = equality.equals(Person("sam"), Person("sam"))9println(result)10val equality = Equality { a, b -> a.name == b.name }11val result = equality.equals(Person("sam"), Person("sam"))12println(result)13val equality = Equality { a, b -> a.name == b.name }14val result = equality.equals(Person("sam"), Person("sam"))15println(result)16val equality = Equality { a, b -> a.name == b.name }17val result = equality.equals(Person("sam"), Person("sam"))18println(result)19val equality = Equality { a, b -> a.name == b.name }20val result = equality.equals(Person("sam"), Person("sam"))21println(result)22val equality = Equality { a, b -> a.name == b.name }23val result = equality.equals(Person("sam"), Person("sam"))24println(result)25val equality = Equality { a, b -> a.name == b.name }26val result = equality.equals(Person("sam"), Person("sam"))27println(result)28val equality = Equality { a, b -> a.name == b.name }29val result = equality.equals(Person("sam"), Person("sam"))30println(result)31val equality = Equality { a, b -> a.name == b.name }32val result = equality.equals(Person("sam"), Person("sam"))
name
Using AI Code Generation
1val equality = Equality.name()2val result = equality.equals("sam", "sam")3println(result)4val equality = Equality.structural()5val result = equality.equals(Person("sam", 20), Person("sam", 20))6println(result)7val equality = Equality.structural()8val result = equality.equals(Person("sam", 20), Person("sam", 20))9println(result)10val equality = Equality.structural()11val result = equality.equals(Person("sam", 20), Person("sam", 20))12println(result)13val equality = Equality.structural()14val result = equality.equals(Person("sam", 20), Person("sam", 20))15println(result)16val equality = Equality.structural()17val result = equality.equals(Person("sam", 20), Person("sam", 20))18println(result)19val equality = Equality.structural()20val result = equality.equals(Person("sam", 20), Person("sam", 20))21println(result)22val equality = Equality.structural()23val result = equality.equals(Person("sam", 20), Person("sam", 20))24println(result)25val equality = Equality.structural()26val result = equality.equals(Person("sam", 20), Person("sam", 20))27println(result)28val equality = Equality.structural()29val result = equality.equals(Person("sam", 20), Person("sam", 20))30println(result)
name
Using AI Code Generation
1val equality = Equality.name()2val equality = Equality.structural()3val equality = Equality.structural()4val equality = Equality.structural()5val equality = Equality.structural()6val equality = Equality.structural()7val equality = Equality.structural()8val equality = Equality.structural()
name
Using AI Code Generation
1 io.kotest.assertions.assertSoftly {2 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")3 }4 io.kotest.assertions.assertSoftly {5 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")6 }7 io.kotest.assertions.assertSoftly {8 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")9 }10 io.kotest.assertions.assertSoftly {11 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")12 }13 io.kotest.assertions.assertSoftly {14 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")15 }16 io.kotest.assertions.assertSoftly {17 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("io.kotest.equals.Equality")18 }19 io.kotest.assertions.assertSoftly {20 io.kotest.assertions.assertThat(io.kotest.equals.equality.name(io.kotest.assertions.eq(a, b))).isEqualTo("
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!!