Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ kotlinxBinaryCompatibilityValidator = "0.13.0"
kotlinCompileTesting = "1.5.0"
kotlinPoet = "1.13.0"
ksp = "1.8.0-1.0.9"
okeyDoke = "1.3.3"

[libraries]
autoService-runtime = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
Expand All @@ -36,6 +37,7 @@ squareUp-kotlinPoetMetadata = { module = "com.squareup:kotlinpoet-metadata", ver

ksp-runtime = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
okeyDoke = { module = "com.oneeyedmen:okeydoke", version.ref = "okeyDoke" }

[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,105 +1,8 @@
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.GenSealedEnum
import org.intellij.lang.annotations.Language

sealed class EmptySealedClass {
@GenSealedEnum(generateEnum = true)
companion object
}

@Language("kotlin")
val emptySealedClassGenerated = """
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass

/**
* An isomorphic enum for the sealed class [EmptySealedClass]
*/
public enum class EmptySealedClassEnum()

/**
* The isomorphic [EmptySealedClassEnum] for [this].
*/
public val EmptySealedClass.`enum`: EmptySealedClassEnum
get() = EmptySealedClassSealedEnum.sealedObjectToEnum(this)

/**
* The isomorphic [EmptySealedClass] for [this].
*/
public val EmptySealedClassEnum.sealedObject: EmptySealedClass
get() = EmptySealedClassSealedEnum.enumToSealedObject(this)

/**
* An implementation of [SealedEnum] for the sealed class [EmptySealedClass]
*/
public object EmptySealedClassSealedEnum : SealedEnum<EmptySealedClass>,
SealedEnumWithEnumProvider<EmptySealedClass, EmptySealedClassEnum>,
EnumForSealedEnumProvider<EmptySealedClass, EmptySealedClassEnum> {
public override val values: List<EmptySealedClass> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
emptyList()
}


public override val enumClass: KClass<EmptySealedClassEnum>
get() = EmptySealedClassEnum::class

public override fun ordinalOf(obj: EmptySealedClass): Int = throw
AssertionError("Constructing a EmptySealedClass is impossible, since it has no sealed subclasses")

public override fun nameOf(obj: EmptySealedClass): String = throw
AssertionError("Constructing a EmptySealedClass is impossible, since it has no sealed subclasses")

public override fun valueOf(name: String): EmptySealedClass = throw
IllegalArgumentException(""${'"'}No sealed enum constant ${'$'}name""${'"'})

public override fun sealedObjectToEnum(obj: EmptySealedClass): EmptySealedClassEnum = throw
AssertionError("Constructing a EmptySealedClass is impossible, since it has no sealed subclasses")

public override fun enumToSealedObject(`enum`: EmptySealedClassEnum): EmptySealedClass = throw
AssertionError("Constructing a EmptySealedClass is impossible, since it has no sealed subclasses")
}

/**
* The index of [this] in the values list.
*/
public val EmptySealedClass.ordinal: Int
get() = EmptySealedClassSealedEnum.ordinalOf(this)

/**
* The name of [this] for use with valueOf.
*/
public val EmptySealedClass.name: String
get() = EmptySealedClassSealedEnum.nameOf(this)

/**
* A list of all [EmptySealedClass] objects.
*/
public val EmptySealedClass.Companion.values: List<EmptySealedClass>
get() = EmptySealedClassSealedEnum.values

/**
* Returns an implementation of [SealedEnum] for the sealed class [EmptySealedClass]
*/
public val EmptySealedClass.Companion.sealedEnum: EmptySealedClassSealedEnum
get() = EmptySealedClassSealedEnum

/**
* Returns the [EmptySealedClass] object for the given [name].
*
* If the given name doesn't correspond to any [EmptySealedClass], an [IllegalArgumentException]
* will be thrown.
*/
public fun EmptySealedClass.Companion.valueOf(name: String): EmptySealedClass =
EmptySealedClassSealedEnum.valueOf(name)

""".trimIndent()
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.testing.SealedEnumApprovalsExtension
import com.livefront.sealedenum.testing.assertApprovedGeneratedFile
import com.livefront.sealedenum.testing.assertCompiles
import com.livefront.sealedenum.testing.assertGeneratedFileMatches
import com.livefront.sealedenum.testing.compile
import com.livefront.sealedenum.testing.getCommonSourceFile
import com.oneeyedmen.okeydoke.Approver
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(SealedEnumApprovalsExtension::class)
class EmptySealedClassTests {
@Test
fun `empty sealed class`() {
Expand All @@ -27,10 +31,10 @@ class EmptySealedClassTests {
}

@Test
fun `compilation generates correct code`() {
fun Approver.`compilation generates correct code`() {
val result = compile(getCommonSourceFile("compilation", "basic", "EmptySealedClass.kt"))

assertCompiles(result)
assertGeneratedFileMatches("EmptySealedClass_SealedEnum.kt", emptySealedClassGenerated, result)
assertApprovedGeneratedFile("EmptySealedClass_SealedEnum.kt", result)
}
}
Original file line number Diff line number Diff line change
@@ -1,107 +1,8 @@
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.GenSealedEnum
import org.intellij.lang.annotations.Language

sealed interface EmptySealedInterface {
@GenSealedEnum(generateEnum = true)
companion object
}

@Language("kotlin")
val emptySealedInterfaceGenerated = """
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass

/**
* An isomorphic enum for the sealed class [EmptySealedInterface]
*/
public enum class EmptySealedInterfaceEnum()

/**
* The isomorphic [EmptySealedInterfaceEnum] for [this].
*/
public val EmptySealedInterface.`enum`: EmptySealedInterfaceEnum
get() = EmptySealedInterfaceSealedEnum.sealedObjectToEnum(this)

/**
* The isomorphic [EmptySealedInterface] for [this].
*/
public val EmptySealedInterfaceEnum.sealedObject: EmptySealedInterface
get() = EmptySealedInterfaceSealedEnum.enumToSealedObject(this)

/**
* An implementation of [SealedEnum] for the sealed class [EmptySealedInterface]
*/
public object EmptySealedInterfaceSealedEnum : SealedEnum<EmptySealedInterface>,
SealedEnumWithEnumProvider<EmptySealedInterface, EmptySealedInterfaceEnum>,
EnumForSealedEnumProvider<EmptySealedInterface, EmptySealedInterfaceEnum> {
public override val values: List<EmptySealedInterface> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
emptyList()
}


public override val enumClass: KClass<EmptySealedInterfaceEnum>
get() = EmptySealedInterfaceEnum::class

public override fun ordinalOf(obj: EmptySealedInterface): Int = throw
AssertionError("Constructing a EmptySealedInterface is impossible, since it has no sealed subclasses")

public override fun nameOf(obj: EmptySealedInterface): String = throw
AssertionError("Constructing a EmptySealedInterface is impossible, since it has no sealed subclasses")

public override fun valueOf(name: String): EmptySealedInterface = throw
IllegalArgumentException(""${'"'}No sealed enum constant ${'$'}name""${'"'})

public override fun sealedObjectToEnum(obj: EmptySealedInterface): EmptySealedInterfaceEnum =
throw
AssertionError("Constructing a EmptySealedInterface is impossible, since it has no sealed subclasses")

public override fun enumToSealedObject(`enum`: EmptySealedInterfaceEnum): EmptySealedInterface =
throw
AssertionError("Constructing a EmptySealedInterface is impossible, since it has no sealed subclasses")
}

/**
* The index of [this] in the values list.
*/
public val EmptySealedInterface.ordinal: Int
get() = EmptySealedInterfaceSealedEnum.ordinalOf(this)

/**
* The name of [this] for use with valueOf.
*/
public val EmptySealedInterface.name: String
get() = EmptySealedInterfaceSealedEnum.nameOf(this)

/**
* A list of all [EmptySealedInterface] objects.
*/
public val EmptySealedInterface.Companion.values: List<EmptySealedInterface>
get() = EmptySealedInterfaceSealedEnum.values

/**
* Returns an implementation of [SealedEnum] for the sealed class [EmptySealedInterface]
*/
public val EmptySealedInterface.Companion.sealedEnum: EmptySealedInterfaceSealedEnum
get() = EmptySealedInterfaceSealedEnum

/**
* Returns the [EmptySealedInterface] object for the given [name].
*
* If the given name doesn't correspond to any [EmptySealedInterface], an [IllegalArgumentException]
* will be thrown.
*/
public fun EmptySealedInterface.Companion.valueOf(name: String): EmptySealedInterface =
EmptySealedInterfaceSealedEnum.valueOf(name)

""".trimIndent()
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.livefront.sealedenum.compilation.basic

import com.livefront.sealedenum.testing.SealedEnumApprovalsExtension
import com.livefront.sealedenum.testing.assertApprovedGeneratedFile
import com.livefront.sealedenum.testing.assertCompiles
import com.livefront.sealedenum.testing.assertGeneratedFileMatches
import com.livefront.sealedenum.testing.compile
import com.livefront.sealedenum.testing.getCommonSourceFile
import com.oneeyedmen.okeydoke.Approver
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(SealedEnumApprovalsExtension::class)
class EmptySealedInterfaceTests {
@Test
fun `empty sealed interface`() {
Expand All @@ -27,10 +31,10 @@ class EmptySealedInterfaceTests {
}

@Test
fun `compilation generates correct code`() {
fun Approver.`compilation generates correct code`() {
val result = compile(getCommonSourceFile("compilation", "basic", "EmptySealedInterface.kt"))

assertCompiles(result)
assertGeneratedFileMatches("EmptySealedInterface_SealedEnum.kt", emptySealedInterfaceGenerated, result)
assertApprovedGeneratedFile("EmptySealedInterface_SealedEnum.kt", result)
}
}
Loading