|
| 1 | +package io.mcarle.konvert.processor.konvertfrom |
| 2 | + |
| 3 | +import com.tschuchort.compiletesting.KotlinCompilation |
| 4 | +import com.tschuchort.compiletesting.SourceFile |
| 5 | +import io.mcarle.konvert.converter.SameTypeConverter |
| 6 | +import io.mcarle.konvert.processor.KonverterITest |
| 7 | +import io.mcarle.konvert.processor.exceptions.InaccessibleDueToVisibilityClassException |
| 8 | +import io.mcarle.konvert.processor.generatedSourceFor |
| 9 | +import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi |
| 10 | +import org.junit.jupiter.params.ParameterizedTest |
| 11 | +import org.junit.jupiter.params.provider.Arguments |
| 12 | +import org.junit.jupiter.params.provider.MethodSource |
| 13 | +import org.paukov.combinatorics3.Generator |
| 14 | +import org.paukov.combinatorics3.IGenerator |
| 15 | +import kotlin.test.assertContains |
| 16 | + |
| 17 | +@OptIn(ExperimentalCompilerApi::class) |
| 18 | +class KonvertFromVisibilityITest : KonverterITest() { |
| 19 | + |
| 20 | + companion object { |
| 21 | + @JvmStatic |
| 22 | + private fun possibleCombinations(): IGenerator<List<String>> = Generator.cartesianProduct( |
| 23 | + // source class (can be java) |
| 24 | + listOf( |
| 25 | + "public", "private", "internal", "java:public", "java:", |
| 26 | + ), |
| 27 | + // target class (cannot be java) |
| 28 | + listOf( |
| 29 | + "public", "private", "internal" |
| 30 | + ), |
| 31 | + // target class companion (cannot be java) |
| 32 | + listOf( |
| 33 | + "public", "private", "internal", "protected" |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + private fun Iterable<List<String>>.toParameters(): List<Arguments> = this.map { |
| 38 | + val sourceClassVisibility = it[0] |
| 39 | + val targetClassVisibility = it[1] |
| 40 | + val targetClassCompanionVisibility = it[2] |
| 41 | + Arguments.of(sourceClassVisibility, targetClassVisibility, targetClassCompanionVisibility) |
| 42 | + } |
| 43 | + |
| 44 | + private fun Iterable<List<String>>.filterValid(valid: Boolean) = this.filter { |
| 45 | + val sourceClassVisibility = it[0] |
| 46 | + val targetClassVisibility = it[1] |
| 47 | + val targetClassCompanionVisibility = it[2] |
| 48 | + |
| 49 | + if (sourceClassVisibility == "private" |
| 50 | + || targetClassVisibility == "private" |
| 51 | + || targetClassCompanionVisibility == "private" |
| 52 | + ) { |
| 53 | + return@filter !valid |
| 54 | + } |
| 55 | + |
| 56 | + if (targetClassCompanionVisibility == "protected") { |
| 57 | + return@filter !valid |
| 58 | + } |
| 59 | + |
| 60 | + valid |
| 61 | + } |
| 62 | + |
| 63 | + @JvmStatic |
| 64 | + fun validCombinations(): List<Arguments> = possibleCombinations().filterValid(true).toParameters() |
| 65 | + |
| 66 | + @JvmStatic |
| 67 | + fun invalidCombinations(): List<Arguments> = possibleCombinations().filterValid(false).toParameters() |
| 68 | + } |
| 69 | + |
| 70 | + @ParameterizedTest |
| 71 | + @MethodSource("validCombinations") |
| 72 | + fun checkCorrectVisibilityGenerated( |
| 73 | + sourceClassVisibility: String, |
| 74 | + targetClassVisibility: String, |
| 75 | + targetClassCompanionVisibility: String |
| 76 | + ) { |
| 77 | + val (compilation) = compileWith( |
| 78 | + enabledConverters = listOf(SameTypeConverter()), |
| 79 | + expectResultCode = KotlinCompilation.ExitCode.OK, |
| 80 | + code = arrayOf( |
| 81 | + generateSourceFile(sourceClassVisibility), |
| 82 | + generateTargetFile(targetClassVisibility, targetClassCompanionVisibility) |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + val extensionFunctionCode = compilation.generatedSourceFor("TargetClassKonverter.kt") |
| 87 | + |
| 88 | + val expectedVisibilityModifier = if (sourceClassVisibility.contains("public") |
| 89 | + && targetClassVisibility == "public" |
| 90 | + && targetClassCompanionVisibility == "public" |
| 91 | + ) "public" else "internal" |
| 92 | + |
| 93 | + assertContains(extensionFunctionCode, "$expectedVisibilityModifier fun TargetClass.Companion.fromSourceClass") |
| 94 | + } |
| 95 | + |
| 96 | + @ParameterizedTest |
| 97 | + @MethodSource("invalidCombinations") |
| 98 | + fun invalidCombinationsReportedWithInaccessibleDueToVisibilityClassException( |
| 99 | + sourceClassVisibility: String, |
| 100 | + targetClassVisibility: String, |
| 101 | + targetClassCompanionVisibility: String |
| 102 | + ) { |
| 103 | + val (_, compilationResult) = compileWith( |
| 104 | + enabledConverters = listOf(SameTypeConverter()), |
| 105 | + expectResultCode = KotlinCompilation.ExitCode.COMPILATION_ERROR, |
| 106 | + code = arrayOf( |
| 107 | + generateSourceFile(sourceClassVisibility), |
| 108 | + generateTargetFile(targetClassVisibility, targetClassCompanionVisibility) |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + assertContains(compilationResult.messages, "${InaccessibleDueToVisibilityClassException::class.simpleName}") |
| 113 | + } |
| 114 | + |
| 115 | + private fun generateSourceFile(sourceClassVisibility: String): SourceFile { |
| 116 | + return if (sourceClassVisibility.startsWith("java:")) { |
| 117 | + val effectiveSourceClassVisibility = sourceClassVisibility.removePrefix("java:") |
| 118 | + SourceFile.java( |
| 119 | + name = "SourceClass.java", |
| 120 | + contents = |
| 121 | + """ |
| 122 | +$effectiveSourceClassVisibility class SourceClass { |
| 123 | + private String property; |
| 124 | +
|
| 125 | + public SourceClass(String property) { |
| 126 | + this.property = property; |
| 127 | + } |
| 128 | +
|
| 129 | + @org.jetbrains.annotations.NotNull |
| 130 | + public String getProperty() { |
| 131 | + return property; |
| 132 | + } |
| 133 | +} |
| 134 | + """.trimIndent() |
| 135 | + ) |
| 136 | + } else { |
| 137 | + SourceFile.kotlin( |
| 138 | + name = "SourceClass.kt", |
| 139 | + contents = |
| 140 | + """ |
| 141 | +$sourceClassVisibility class SourceClass(val property: String) |
| 142 | + """.trimIndent() |
| 143 | + ) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private fun generateTargetFile(targetClassVisibility: String, targetClassCompanionVisibility: String): SourceFile { |
| 148 | + return SourceFile.kotlin( |
| 149 | + name = "TargetClass.kt", |
| 150 | + contents = |
| 151 | + """ |
| 152 | +import io.mcarle.konvert.api.KonvertFrom |
| 153 | +
|
| 154 | +@KonvertFrom(SourceClass::class) |
| 155 | +$targetClassVisibility class TargetClass(val property: String) { |
| 156 | + $targetClassCompanionVisibility companion object |
| 157 | +} |
| 158 | + """.trimIndent() |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments