From ccd585fef6066180226fbd937fe7fcfea6234d05 Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Wed, 8 Jan 2025 19:37:38 +0100 Subject: [PATCH] Adds Kotlin tests --- README.md | 1 + equalsverifier-test-kotlin/pom.xml | 117 ++++++++++++++++++ .../KotlinCompilerGeneratedAnnotationTest.kt | 31 +++++ .../kotlin/KotlinFieldIterableTest.kt | 38 ++++++ .../kotlin/KotlinIntegrationTest.kt | 39 ++++++ pom.xml | 2 + 6 files changed, 228 insertions(+) create mode 100644 equalsverifier-test-kotlin/pom.xml create mode 100644 equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinCompilerGeneratedAnnotationTest.kt create mode 100644 equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinFieldIterableTest.kt create mode 100644 equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinIntegrationTest.kt diff --git a/README.md b/README.md index 7958b7efe..4de8638df 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Here's a description of the modules: | equalsverifier-release-main | release assembly for jar with dependencies | | equalsverifier-release-nodep | release assembly for fat jar (with dependencies shaded in) | | equalsverifier-release-verify | validation tests for the releases | +| equalsverifier-test-kotlin | tests for Kotlin classes | ## Signed JAR diff --git a/equalsverifier-test-kotlin/pom.xml b/equalsverifier-test-kotlin/pom.xml new file mode 100644 index 000000000..a30177dc2 --- /dev/null +++ b/equalsverifier-test-kotlin/pom.xml @@ -0,0 +1,117 @@ + + + + 4.0.0 + + nl.jqno.equalsverifier + equalsverifier-parent + 3.18.1-SNAPSHOT + + jar + + equalsverifier-test-kotlin + EqualsVerifier | test Kotlin + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${version.kotlin} + + + compile + compile + + compile + + + + src/main/kotlin + target/generated-sources/annotations + + + + + test-compile + test-compile + + test-compile + + + + src/test/kotlin + target/generated-test-sources/test-annotations + + + + + + 1.8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + none + + + default-testCompile + none + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + + + + + + + + nl.jqno.equalsverifier + equalsverifier-core + ${project.version} + test + + + org.junit.jupiter + junit-jupiter + ${version.junit-jupiter} + test + + + org.assertj + assertj-core + ${version.assertj} + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${version.kotlin} + test + + + org.jetbrains.kotlin + kotlin-reflect + ${version.kotlin} + test + + + + diff --git a/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinCompilerGeneratedAnnotationTest.kt b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinCompilerGeneratedAnnotationTest.kt new file mode 100644 index 000000000..cd4fa2033 --- /dev/null +++ b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinCompilerGeneratedAnnotationTest.kt @@ -0,0 +1,31 @@ +package nl.jqno.equalsverifier.kotlin + +import nl.jqno.equalsverifier.EqualsVerifier +import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache +import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCacheBuilder +import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class KotlinCompilerGeneratedAnnotationTest { + + val cache = AnnotationCache() + val cacheBuilder = AnnotationCacheBuilder(SupportedAnnotations.values(), HashSet()) + + @Test + fun `Kotlin classes are recognised as such`() { + assertThat(checkAnnotationFor(KotlinClass::class.java)).isTrue() + } + + @Test + fun `Java classes are not recognised as Kotlin classes`() { + assertThat(checkAnnotationFor(EqualsVerifier::class.java)).isFalse() + } + + private fun checkAnnotationFor(type: Class<*>): Boolean { + cacheBuilder.build(type, cache) + return cache.hasClassAnnotation(type, SupportedAnnotations.KOTLIN) + } +} + +class KotlinClass diff --git a/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinFieldIterableTest.kt b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinFieldIterableTest.kt new file mode 100644 index 000000000..272b904d1 --- /dev/null +++ b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinFieldIterableTest.kt @@ -0,0 +1,38 @@ +package nl.jqno.equalsverifier.kotlin + +import org.junit.jupiter.api.Test +import java.lang.reflect.Field +import nl.jqno.equalsverifier.internal.reflection.FieldIterable +import org.assertj.core.api.Assertions.assertThat + +class KotlinFieldIterableTest { + val actual = HashSet() + + @Test + fun `FieldIterable ofKotlin ignores superclass backing fields`() { + actual.addAll(FieldIterable.ofKotlin(ImplementingDataClass::class.java).map { it.getField() }) + + assertThat(actual) + .isEqualTo(setOf( + Base::class.java.getDeclaredField("base"), + ImplementingDataClass::class.java.getDeclaredField("toOverride"))) + } + + @Test + fun `FieldIterable of does not ignore superclass backing fields`() { + actual.addAll(FieldIterable.of(ImplementingDataClass::class.java).map { it.getField() }) + + assertThat(actual) + .isEqualTo(setOf( + Base::class.java.getDeclaredField("base"), + Base::class.java.getDeclaredField("toOverride"), + ImplementingDataClass::class.java.getDeclaredField("toOverride"))) + } + + sealed class Base( + internal open val base: Int, + internal open val toOverride: Int, + ) + + data class ImplementingDataClass(override val toOverride: Int) : Base(42, toOverride) +} diff --git a/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinIntegrationTest.kt b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinIntegrationTest.kt new file mode 100644 index 000000000..071ba26fd --- /dev/null +++ b/equalsverifier-test-kotlin/src/test/kotlin/nl/jqno/equalsverifier/kotlin/KotlinIntegrationTest.kt @@ -0,0 +1,39 @@ +package nl.jqno.equalsverifier.kotlin + +import java.util.Objects +import nl.jqno.equalsverifier.EqualsVerifier +import org.junit.jupiter.api.Test + +class KotlinIntegrationTest { + @Test + fun `super's backing field does not affect implementing data class`() { + EqualsVerifier.forClass(ImplementingDataClass::class.java) + .withIgnoredFields("base") + .verify() + } + + @Test + fun `super's backing field does not affect implementing regular class`() { + EqualsVerifier.forClass(ImplementingRegularClass::class.java) + .verify() + } + + sealed class Base( + internal open val base: Int, + internal open val toOverride: Int, + ) + + data class ImplementingDataClass(override val toOverride: Int) : Base(42, toOverride) + + class ImplementingRegularClass(override val toOverride: Int) : Base(42, toOverride) { + override fun equals(other: Any?): Boolean { + return other is ImplementingRegularClass + && base == other.base + && toOverride == other.toOverride + } + + override fun hashCode(): Int { + return Objects.hash(base, toOverride) + } + } +} diff --git a/pom.xml b/pom.xml index b7275309a..aa942fdc0 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ 1.0 2.13.0 5.11.4 + 2.1.0 19.0.2.1 2.0.16 @@ -576,6 +577,7 @@ equalsverifier-16 equalsverifier-17 equalsverifier-21 + equalsverifier-test-kotlin equalsverifier-aggregator equalsverifier-release-main equalsverifier-release-nodep