Skip to content

Commit

Permalink
Don't consider kotlinx and javax types to be builtin (#3899)
Browse files Browse the repository at this point in the history
Fixes #3855

---------

Co-authored-by: Sam <sam@sksamuel.com>
  • Loading branch information
Kantis and sksamuel committed Mar 11, 2024
1 parent 0660eb3 commit 23b0629
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
Expand Up @@ -161,7 +161,7 @@ internal fun useEq(
val typeIsJavaOrKotlinBuiltIn by lazy {
val bestName = typeName.bestName()
bestName.startsWith("kotlin") ||
bestName.startsWith("java") ||
(bestName.startsWith("java") && !bestName.startsWith("javax.xml.bind.JAXBElement")) ||
builtins.contains(bestName)
}
val expectedOrActualIsEnum = actual is Enum<*>
Expand Down
@@ -0,0 +1,31 @@
package io.kotest.matchers.equality

/**
* This is currently unused but will form the basis of reflection based checks in 6.0
*/
internal fun <T> T.isBuiltInType() =
this is Number
|| this is UInt
|| this is ULong
|| this is UByte
|| this is UShort
|| this is String
|| this is Boolean
|| this is Char
|| this is IntArray
|| this is CharArray
|| this is LongArray
|| this is ShortArray
|| this is ByteArray
|| this is DoubleArray
|| this is FloatArray
|| this is BooleanArray
|| this is UIntArray
|| this is ULongArray
|| this is UByteArray
|| this is UShortArray
|| this is Array<*>
|| this is List<*>
|| this is Set<*>
|| this is Map<*, *>
|| this is Sequence<*>
Expand Up @@ -620,7 +620,7 @@ private fun requiresUseOfDefaultEq(
useDefaultEqualForFields: List<String>
): Boolean {
val expectedOrActualIsNull = actual == null || expected == null
val typeIsJavaOrKotlinBuiltIn by lazy { typeName.startsWith("kotlin") || typeName.startsWith("java") }
val typeIsJavaOrKotlinBuiltIn by lazy { typeName.startsWith("kotlin.") || typeName.startsWith("java.") }
val expectedOrActualIsEnum = actual is Enum<*>
|| expected is Enum<*>
|| (actual != null && actual::class.java.isEnum)
Expand Down
2 changes: 2 additions & 0 deletions kotest-tests/kotest-tests-core/build.gradle.kts
Expand Up @@ -11,6 +11,8 @@ kotlin {
implementation(projects.kotestAssertions.kotestAssertionsCore)
// this is here to test that the intellij marker 'dummy' test doesn't appear in intellij
implementation(libs.junit.jupiter.engine)
// We want to test that JAXBElement is compared properly
implementation("javax.xml.bind:jaxb-api:2.3.1")
}
}
}
Expand Down
@@ -0,0 +1,29 @@
package com.sksamuel.kotest

import io.kotest.assertions.shouldFail
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.equality.shouldBeEqualToComparingFields
import io.kotest.matchers.string.shouldContain
import javax.xml.bind.JAXBElement
import javax.xml.namespace.QName

class JaxbElementTest : FunSpec({

context("Comparing JAXBElement reflectively works as expected") {

val jaxbElement = JAXBElement(QName.valueOf("name"), Int::class.java, 123)
val otherJaxbElement = JAXBElement(QName.valueOf("name"), Int::class.java, 124)

test("!should pass when comparing equal elements") {
jaxbElement.shouldBeEqualToComparingFields(jaxbElement)
}

test("!should fail when comparing different elements") {
shouldFail {
jaxbElement.shouldBeEqualToComparingFields(otherJaxbElement)
}.message.shouldContain(
"""Value differ at:\s+1\) value:\s+expected:<124> but was:<123>""".toRegex()
)
}
}
})

0 comments on commit 23b0629

Please sign in to comment.