test: Equalsのテストを修正

This commit is contained in:
usbharu 2023-12-14 15:50:01 +09:00
parent ec848630ca
commit e8b6b6784c
1 changed files with 67 additions and 11 deletions

View File

@ -8,21 +8,77 @@ import nl.jqno.equalsverifier.Warning
import nl.jqno.equalsverifier.internal.reflection.PackageScanner
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Component
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.RestController
import java.lang.reflect.Modifier
import kotlin.test.assertFails
class EqualsAndToStringTest {
@Test
fun equalsTest() {
assertFails {
EqualsVerifier
.simple()
.suppress(Warning.INHERITED_DIRECTLY_FROM_OBJECT)
.forPackage("dev.usbharu.hideout", true)
.verify()
}
@TestFactory
fun equalsTest(): List<DynamicTest> {
val classes = PackageScanner.getClassesIn("dev.usbharu.hideout", null, true)
return classes
.asSequence()
.filter {
it.getAnnotation(Service::class.java) == null
}
.filter {
it.getAnnotation(Repository::class.java) == null
}
.filter {
it.getAnnotation(Component::class.java) == null
}
.filter {
it.getAnnotation(Controller::class.java) == null
}
.filter {
it.getAnnotation(RestController::class.java) == null
}
.filter {
it.getAnnotation(Configuration::class.java) == null
}
.filterNot {
it.packageName.startsWith("dev.usbharu.hideout.domain.mastodon.model.generated")
}
.filterNot {
Throwable::class.java.isAssignableFrom(it)
}
.filterNot {
Modifier.isAbstract(it.modifiers)
}
.filter {
try {
it.kotlin.objectInstance == null
} catch (_: Exception) {
true
}
}
.filter {
it.superclass == Any::class.java || it.superclass?.packageName?.startsWith("dev.usbharu") ?: true
}
.map {
dynamicTest(it.name) {
if (it.isKotlinClass()) {
println(" at ${it.name}.toString(${it.simpleName}.kt:1)")
}
try {
EqualsVerifier.simple()
.suppress(Warning.INHERITED_DIRECTLY_FROM_OBJECT, Warning.TRANSIENT_FIELDS)
.forClass(it)
.verify()
} catch (e: AssertionError) {
e.printStackTrace()
}
}
}
.toList()
}
@TestFactory