Skip to content

Commit

Permalink
Adds Kotlin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jan 8, 2025
1 parent e06a7b0 commit ccd585f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
117 changes: 117 additions & 0 deletions equalsverifier-test-kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier-parent</artifactId>
<version>3.18.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>equalsverifier-test-kotlin</artifactId>
<name>EqualsVerifier | test Kotlin</name>

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${version.kotlin}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/kotlin</source>
<source>target/generated-test-sources/test-annotations</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit-jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.assertj}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${version.kotlin}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${version.kotlin}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<Field>()

@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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<version.jcip-annotations>1.0</version.jcip-annotations>
<version.joda-time>2.13.0</version.joda-time>
<version.junit-jupiter>5.11.4</version.junit-jupiter>
<version.kotlin>2.1.0</version.kotlin>
<version.openjfx>19.0.2.1</version.openjfx>
<version.slf4j>2.0.16</version.slf4j>

Expand Down Expand Up @@ -576,6 +577,7 @@
<module>equalsverifier-16</module>
<module>equalsverifier-17</module>
<module>equalsverifier-21</module>
<module>equalsverifier-test-kotlin</module>
<module>equalsverifier-aggregator</module>
<module>equalsverifier-release-main</module>
<module>equalsverifier-release-nodep</module>
Expand Down

0 comments on commit ccd585f

Please sign in to comment.