Skip to content

Commit

Permalink
Kotlin spice
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf committed Dec 2, 2024
1 parent b850c10 commit 016ba16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import com.tngtech.archunit.core.domain.JavaClass
import com.tngtech.archunit.lang.ArchCondition
import com.tngtech.archunit.lang.ConditionEvents
import com.tngtech.archunit.lang.SimpleConditionEvent
import com.tngtech.archunit.lang.syntax.elements.ClassesShouldConjunction
import com.tngtech.archunit.lang.syntax.elements.GivenClassesConjunction
import kotlin.reflect.KClass

fun <T : Any> GivenClassesConjunction.equalsExactly(
vararg requiredClasses: KClass<out T>,
): ClassesShouldConjunction {
return should(ArchUnitEqualsClassesExactly(requiredClasses.map { it.java }))
}

class ArchUnitEqualsClassesExactly<T : Any>(
private val requiredClasses: List<Class<out T>>,
) : ArchCondition<JavaClass>("equal exactly the given classes") {
constructor(vararg requiredClasses: Class<out T>) : this(requiredClasses.toList())

constructor(vararg requiredClasses: KClass<out T>) : this(requiredClasses.map { it.java })

private val actualClasses: MutableList<JavaClass> = mutableListOf()

override fun check(item: JavaClass, events: ConditionEvents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ class NadelPseudoSealedTypeKtTest {
.areAssignableTo(GraphQLFieldsContainer::class.java)
.and()
.areNotInterfaces()
.should(
ArchUnitEqualsClassesExactly(
GraphQLInterfaceType::class,
GraphQLObjectType::class,
),
.equalsExactly(
GraphQLInterfaceType::class,
GraphQLObjectType::class,
)
.check(schemaClasses)
}
Expand All @@ -48,17 +46,15 @@ class NadelPseudoSealedTypeKtTest {
.areAssignableTo(GraphQLNamedType::class.java)
.and()
.areNotInterfaces()
.should(
ArchUnitEqualsClassesExactly(
GraphQLEnumType::class,
GraphQLInputObjectType::class,
GraphQLInterfaceType::class,
GraphQLObjectType::class,
GraphQLScalarType::class,
GraphQLUnionType::class,
// Should almost never be used though
GraphQLTypeReference::class,
),
.equalsExactly(
GraphQLEnumType::class,
GraphQLInputObjectType::class,
GraphQLInterfaceType::class,
GraphQLObjectType::class,
GraphQLScalarType::class,
GraphQLUnionType::class,
// Should almost never be used though
GraphQLTypeReference::class,
)
.check(schemaClasses)
}
Expand All @@ -72,14 +68,12 @@ class NadelPseudoSealedTypeKtTest {
.areAssignableTo(GraphQLUnmodifiedType::class.java)
.and()
.areNotInterfaces()
.should(
ArchUnitEqualsClassesExactly(
GraphQLEnumType::class,
GraphQLInterfaceType::class,
GraphQLObjectType::class,
GraphQLScalarType::class,
GraphQLUnionType::class,
),
.equalsExactly(
GraphQLEnumType::class,
GraphQLInterfaceType::class,
GraphQLObjectType::class,
GraphQLScalarType::class,
GraphQLUnionType::class,
)
.check(schemaClasses)
}
Expand All @@ -93,12 +87,10 @@ class NadelPseudoSealedTypeKtTest {
.areAssignableTo(GraphQLUnmodifiedType::class.java)
.and()
.areNotInterfaces()
.should(
ArchUnitEqualsClassesExactly(
GraphQLEnumType::class,
GraphQLInputObjectType::class,
GraphQLScalarType::class,
),
.equalsExactly(
GraphQLEnumType::class,
GraphQLInputObjectType::class,
GraphQLScalarType::class,
)
.check(schemaClasses)
}
Expand Down

0 comments on commit 016ba16

Please sign in to comment.