-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...common/test/kotlin/com/livefront/sealedenum/compilation/basic/TwoDataObjectSealedClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.livefront.sealedenum.compilation.basic | ||
|
||
import com.livefront.sealedenum.GenSealedEnum | ||
import org.intellij.lang.annotations.Language | ||
|
||
sealed class TwoDataObjectSealedClass { | ||
data object FirstObject : TwoDataObjectSealedClass() | ||
|
||
data object SecondObject : TwoDataObjectSealedClass() | ||
|
||
@GenSealedEnum(generateEnum = true) | ||
companion object | ||
} | ||
|
||
@Language("kotlin") | ||
val twoDataObjectSealedClassGenerated = """ | ||
package com.livefront.sealedenum.compilation.basic | ||
import com.livefront.sealedenum.EnumForSealedEnumProvider | ||
import com.livefront.sealedenum.SealedEnum | ||
import com.livefront.sealedenum.SealedEnumWithEnumProvider | ||
import java.lang.Class | ||
import kotlin.Int | ||
import kotlin.String | ||
import kotlin.collections.List | ||
/** | ||
* An isomorphic enum for the sealed class [TwoDataObjectSealedClass] | ||
*/ | ||
public enum class TwoDataObjectSealedClassEnum() { | ||
TwoDataObjectSealedClass_FirstObject, | ||
TwoDataObjectSealedClass_SecondObject, | ||
} | ||
/** | ||
* The isomorphic [TwoDataObjectSealedClassEnum] for [this]. | ||
*/ | ||
public val TwoDataObjectSealedClass.`enum`: TwoDataObjectSealedClassEnum | ||
get() = TwoDataObjectSealedClassSealedEnum.sealedObjectToEnum(this) | ||
/** | ||
* The isomorphic [TwoDataObjectSealedClass] for [this]. | ||
*/ | ||
public val TwoDataObjectSealedClassEnum.sealedObject: TwoDataObjectSealedClass | ||
get() = TwoDataObjectSealedClassSealedEnum.enumToSealedObject(this) | ||
/** | ||
* An implementation of [SealedEnum] for the sealed class [TwoDataObjectSealedClass] | ||
*/ | ||
public object TwoDataObjectSealedClassSealedEnum : SealedEnum<TwoDataObjectSealedClass>, | ||
SealedEnumWithEnumProvider<TwoDataObjectSealedClass, TwoDataObjectSealedClassEnum>, | ||
EnumForSealedEnumProvider<TwoDataObjectSealedClass, TwoDataObjectSealedClassEnum> { | ||
public override val values: List<TwoDataObjectSealedClass> = listOf( | ||
TwoDataObjectSealedClass.FirstObject, | ||
TwoDataObjectSealedClass.SecondObject | ||
) | ||
public override val enumClass: Class<TwoDataObjectSealedClassEnum> | ||
get() = TwoDataObjectSealedClassEnum::class.java | ||
public override fun ordinalOf(obj: TwoDataObjectSealedClass): Int = when (obj) { | ||
TwoDataObjectSealedClass.FirstObject -> 0 | ||
TwoDataObjectSealedClass.SecondObject -> 1 | ||
} | ||
public override fun nameOf(obj: TwoDataObjectSealedClass): String = when (obj) { | ||
TwoDataObjectSealedClass.FirstObject -> "TwoDataObjectSealedClass_FirstObject" | ||
TwoDataObjectSealedClass.SecondObject -> "TwoDataObjectSealedClass_SecondObject" | ||
} | ||
public override fun valueOf(name: String): TwoDataObjectSealedClass = when (name) { | ||
"TwoDataObjectSealedClass_FirstObject" -> TwoDataObjectSealedClass.FirstObject | ||
"TwoDataObjectSealedClass_SecondObject" -> TwoDataObjectSealedClass.SecondObject | ||
else -> throw IllegalArgumentException(""${'"'}No sealed enum constant ${'$'}name""${'"'}) | ||
} | ||
public override fun sealedObjectToEnum(obj: TwoDataObjectSealedClass): | ||
TwoDataObjectSealedClassEnum = when (obj) { | ||
TwoDataObjectSealedClass.FirstObject -> | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_FirstObject | ||
TwoDataObjectSealedClass.SecondObject -> | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_SecondObject | ||
} | ||
public override fun enumToSealedObject(`enum`: TwoDataObjectSealedClassEnum): | ||
TwoDataObjectSealedClass = when (enum) { | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_FirstObject -> | ||
TwoDataObjectSealedClass.FirstObject | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_SecondObject -> | ||
TwoDataObjectSealedClass.SecondObject | ||
} | ||
} | ||
/** | ||
* The index of [this] in the values list. | ||
*/ | ||
public val TwoDataObjectSealedClass.ordinal: Int | ||
get() = TwoDataObjectSealedClassSealedEnum.ordinalOf(this) | ||
/** | ||
* The name of [this] for use with valueOf. | ||
*/ | ||
public val TwoDataObjectSealedClass.name: String | ||
get() = TwoDataObjectSealedClassSealedEnum.nameOf(this) | ||
/** | ||
* A list of all [TwoDataObjectSealedClass] objects. | ||
*/ | ||
public val TwoDataObjectSealedClass.Companion.values: List<TwoDataObjectSealedClass> | ||
get() = TwoDataObjectSealedClassSealedEnum.values | ||
/** | ||
* Returns an implementation of [SealedEnum] for the sealed class [TwoDataObjectSealedClass] | ||
*/ | ||
public val TwoDataObjectSealedClass.Companion.sealedEnum: TwoDataObjectSealedClassSealedEnum | ||
get() = TwoDataObjectSealedClassSealedEnum | ||
/** | ||
* Returns the [TwoDataObjectSealedClass] object for the given [name]. | ||
* | ||
* If the given name doesn't correspond to any [TwoDataObjectSealedClass], an | ||
* [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun TwoDataObjectSealedClass.Companion.valueOf(name: String): TwoDataObjectSealedClass = | ||
TwoDataObjectSealedClassSealedEnum.valueOf(name) | ||
""".trimIndent() |
54 changes: 54 additions & 0 deletions
54
...n/test/kotlin/com/livefront/sealedenum/compilation/basic/TwoDataObjectSealedClassTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.livefront.sealedenum.compilation.basic | ||
|
||
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 org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class TwoDataObjectSealedClassTests { | ||
@Test | ||
fun `two data objects sealed class`() { | ||
assertEquals( | ||
listOf(TwoDataObjectSealedClass.FirstObject, TwoDataObjectSealedClass.SecondObject), | ||
TwoDataObjectSealedClassSealedEnum.values | ||
) | ||
} | ||
|
||
@Test | ||
fun `two enums for sealed class`() { | ||
assertEquals( | ||
listOf( | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_FirstObject, | ||
TwoDataObjectSealedClassEnum.TwoDataObjectSealedClass_SecondObject | ||
), | ||
enumValues<TwoDataObjectSealedClassEnum>().toList() | ||
) | ||
} | ||
|
||
@Test | ||
fun `two enums for sealed class with mapping`() { | ||
assertEquals( | ||
TwoDataObjectSealedClass.values.map(TwoDataObjectSealedClass::enum), | ||
enumValues<TwoDataObjectSealedClassEnum>().toList() | ||
) | ||
} | ||
|
||
@Test | ||
fun `correct enum class`() { | ||
assertEquals(TwoDataObjectSealedClassEnum::class.java, TwoDataObjectSealedClassSealedEnum.enumClass) | ||
} | ||
|
||
@Test | ||
fun `compilation generates correct code`() { | ||
val result = compile(getCommonSourceFile("compilation", "basic", "TwoDataObjectSealedClass.kt")) | ||
|
||
assertCompiles(result) | ||
assertGeneratedFileMatches( | ||
"TwoDataObjectSealedClass_SealedEnum.kt", | ||
twoDataObjectSealedClassGenerated, | ||
result | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters