Skip to content

Commit

Permalink
πŸ› Consider files with or without packages
Browse files Browse the repository at this point in the history
  • Loading branch information
heytherewill committed Nov 1, 2023
1 parent dadf372 commit 9a45ac5
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 2 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class ActionMappingSymbolProcessor(
val childActionClassName = childAction.toClassName()

val sealedParentActionTypeName = parentActionSealedType.toClassName().simpleName
val parentActionTypeName = parentActionClassName.canonicalName.substring(parentActionClassName.packageName.length, parentActionClassName.canonicalName.length)
val parentActionTypeName = parentActionClassName.canonicalName
.substring(parentActionClassName.packageName.length, parentActionClassName.canonicalName.length)
.trim('.')
val childActionTypeName = childActionClassName.simpleName

val childActionArgumentName = childActionTypeName.toCamelCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ class ActionMappingSymbolProcessorTests {
sources.single().readText().contentEquals(SourceFiles.generatedSettingsFile)
}

@Test
fun `Action mapping methods are generated on files without a package`() {
// Arrange
val compilation = KotlinCompilation().apply {
sources = listOf(SourceFiles.appActionWithoutPackage, SourceFiles.settingsActionWithoutPackage)
symbolProcessorProviders = listOf(ActionMappingSymbolProcessorProvider())
inheritClassPath = true
messageOutputStream = System.out
}

// Act
val result = compilation.compile()

// Assert
result.exitCode shouldBe KotlinCompilation.ExitCode.OK

val sources = result.kspGeneratedSources()
sources.size.shouldBe(1)
sources.single().readText().contentEquals(SourceFiles.generatedSettingsFileWithoutPackage)
}

@Test
fun `If a class annotated with @WrapperAction has multiple properties then the compilation fails`() {
// Arrange
Expand Down
44 changes: 43 additions & 1 deletion komposable-architecture-compiler/src/test/kotlin/SourceFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ object SourceFiles {
val settingsAction = SourceFile.kotlin(
"SettingsAction.kt",
"""
package com.toggl.komposable.compiler.testing
sealed interface SettingsAction {
data class ChangeSomeSetting(val newValue: Boolean) : SettingsAction
}
""".trimIndent(),
)

val settingsActionWithoutPackage = SourceFile.kotlin(
"SettingsAction.kt",
"""
sealed interface SettingsAction {
data class ChangeSomeSetting(val newValue: Boolean) : SettingsAction
}
Expand All @@ -15,6 +26,22 @@ object SourceFiles {
val appAction = SourceFile.kotlin(
"AppAction.kt",
"""
package com.toggl.komposable.compiler.testing
import com.toggl.komposable.architecture.WrapperAction
sealed interface AppAction {
data object ClearList : AppAction
@WrapperAction
data class Settings(val settingsAction: SettingsAction) : AppAction
}
""".trimIndent(),
)

val appActionWithoutPackage = SourceFile.kotlin(
"AppAction.kt",
"""
import com.toggl.komposable.architecture.WrapperAction
sealed interface AppAction {
Expand All @@ -29,6 +56,8 @@ object SourceFiles {
val appActionWithMultipleProperties = SourceFile.kotlin(
"AppAction.kt",
"""
package com.toggl.komposable.compiler.testing
import com.toggl.komposable.architecture.WrapperAction
sealed interface AppAction {
Expand All @@ -41,7 +70,20 @@ object SourceFiles {
)

@Language("kotlin")
val generatedSettingsFile = """import AppAction.Settings
val generatedSettingsFile = """
package com.toggl.komposable.compiler.testing
import AppAction.Settings
public fun mapAppActionToSettingsAction(appAction: AppAction): SettingsAction? = if(appAction is
AppAction.Settings) appAction.settingsAction else null
public fun mapSettingsActionToAppAction(settingsAction: SettingsAction): AppAction.Settings =
AppAction.Settings(settingsAction)
""".trimIndent()

@Language("kotlin")
val generatedSettingsFileWithoutPackage = """import AppAction.Settings
public fun mapAppActionToSettingsAction(appAction: AppAction): SettingsAction? = if(appAction is
AppAction.Settings) appAction.settingsAction else null
Expand Down

0 comments on commit 9a45ac5

Please sign in to comment.