-
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
23 changed files
with
621 additions
and
440 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
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
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
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
120 changes: 120 additions & 0 deletions
120
processing-tests/common/test/kotlin/com/livefront/sealedenum/compilation/equality/Flag.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,120 @@ | ||
package com.livefront.sealedenum.compilation.equality | ||
|
||
import com.livefront.sealedenum.GenSealedEnum | ||
import org.intellij.lang.annotations.Language | ||
|
||
sealed class Flag { | ||
val i: Int = 1 shl ordinal | ||
object FirstFlag : Flag() | ||
|
||
object SecondFlag : Flag() | ||
|
||
@GenSealedEnum(generateEnum = true) | ||
companion object | ||
} | ||
|
||
@Language("kotlin") | ||
val flagGenerated = """ | ||
package com.livefront.sealedenum.compilation.equality | ||
import com.livefront.sealedenum.EnumForSealedEnumProvider | ||
import com.livefront.sealedenum.SealedEnum | ||
import com.livefront.sealedenum.SealedEnumWithEnumProvider | ||
import kotlin.Int | ||
import kotlin.String | ||
import kotlin.collections.List | ||
import kotlin.reflect.KClass | ||
/** | ||
* An isomorphic enum for the sealed class [Flag] | ||
*/ | ||
public enum class FlagEnum() { | ||
Flag_FirstFlag, | ||
Flag_SecondFlag, | ||
} | ||
/** | ||
* The isomorphic [FlagEnum] for [this]. | ||
*/ | ||
public val Flag.`enum`: FlagEnum | ||
get() = FlagSealedEnum.sealedObjectToEnum(this) | ||
/** | ||
* The isomorphic [Flag] for [this]. | ||
*/ | ||
public val FlagEnum.sealedObject: Flag | ||
get() = FlagSealedEnum.enumToSealedObject(this) | ||
/** | ||
* An implementation of [SealedEnum] for the sealed class [Flag] | ||
*/ | ||
public object FlagSealedEnum : SealedEnum<Flag>, SealedEnumWithEnumProvider<Flag, FlagEnum>, | ||
EnumForSealedEnumProvider<Flag, FlagEnum> { | ||
public override val values: List<Flag> = listOf( | ||
Flag.FirstFlag, | ||
Flag.SecondFlag | ||
) | ||
public override val enumClass: KClass<FlagEnum> | ||
get() = FlagEnum::class | ||
public override fun ordinalOf(obj: Flag): Int = when (obj) { | ||
is Flag.FirstFlag -> 0 | ||
is Flag.SecondFlag -> 1 | ||
} | ||
public override fun nameOf(obj: Flag): String = when (obj) { | ||
is Flag.FirstFlag -> "Flag_FirstFlag" | ||
is Flag.SecondFlag -> "Flag_SecondFlag" | ||
} | ||
public override fun valueOf(name: String): Flag = when (name) { | ||
"Flag_FirstFlag" -> Flag.FirstFlag | ||
"Flag_SecondFlag" -> Flag.SecondFlag | ||
else -> throw IllegalArgumentException(""${'"'}No sealed enum constant ${'$'}name""${'"'}) | ||
} | ||
public override fun sealedObjectToEnum(obj: Flag): FlagEnum = when (obj) { | ||
is Flag.FirstFlag -> FlagEnum.Flag_FirstFlag | ||
is Flag.SecondFlag -> FlagEnum.Flag_SecondFlag | ||
} | ||
public override fun enumToSealedObject(`enum`: FlagEnum): Flag = when (enum) { | ||
FlagEnum.Flag_FirstFlag -> Flag.FirstFlag | ||
FlagEnum.Flag_SecondFlag -> Flag.SecondFlag | ||
} | ||
} | ||
/** | ||
* The index of [this] in the values list. | ||
*/ | ||
public val Flag.ordinal: Int | ||
get() = FlagSealedEnum.ordinalOf(this) | ||
/** | ||
* The name of [this] for use with valueOf. | ||
*/ | ||
public val Flag.name: String | ||
get() = FlagSealedEnum.nameOf(this) | ||
/** | ||
* A list of all [Flag] objects. | ||
*/ | ||
public val Flag.Companion.values: List<Flag> | ||
get() = FlagSealedEnum.values | ||
/** | ||
* Returns an implementation of [SealedEnum] for the sealed class [Flag] | ||
*/ | ||
public val Flag.Companion.sealedEnum: FlagSealedEnum | ||
get() = FlagSealedEnum | ||
/** | ||
* Returns the [Flag] object for the given [name]. | ||
* | ||
* If the given name doesn't correspond to any [Flag], an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Flag.Companion.valueOf(name: String): Flag = FlagSealedEnum.valueOf(name) | ||
""".trimIndent() |
50 changes: 50 additions & 0 deletions
50
...ssing-tests/common/test/kotlin/com/livefront/sealedenum/compilation/equality/FlagTests.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,50 @@ | ||
package com.livefront.sealedenum.compilation.equality | ||
|
||
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 FlagTests { | ||
@Test | ||
fun `two objects sealed class`() { | ||
assertEquals( | ||
listOf(Flag.FirstFlag, Flag.SecondFlag), | ||
FlagSealedEnum.values | ||
) | ||
} | ||
|
||
@Test | ||
fun `two enums for sealed class`() { | ||
assertEquals( | ||
listOf( | ||
FlagEnum.Flag_FirstFlag, | ||
FlagEnum.Flag_SecondFlag | ||
), | ||
enumValues<FlagEnum>().toList() | ||
) | ||
} | ||
|
||
@Test | ||
fun `two enums for sealed class with mapping`() { | ||
assertEquals( | ||
Flag.values.map(Flag::enum), | ||
enumValues<FlagEnum>().toList() | ||
) | ||
} | ||
|
||
@Test | ||
fun `correct enum class`() { | ||
assertEquals(FlagEnum::class, FlagSealedEnum.enumClass) | ||
} | ||
|
||
@Test | ||
fun `compilation generates correct code`() { | ||
val result = compile(getCommonSourceFile("compilation", "equality", "Flag.kt")) | ||
|
||
assertCompiles(result) | ||
assertGeneratedFileMatches("Flag_SealedEnum.kt", flagGenerated, result) | ||
} | ||
} |
Oops, something went wrong.