|
| 1 | +import com.guardsquare.proguard.kotlin.printer.KotlinMetadataPrinter |
| 2 | +import io.kotest.core.spec.style.FunSpec |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import proguard.classfile.kotlin.visitor.ReferencedKotlinMetadataVisitor |
| 5 | +import proguard.testutils.ClassPoolBuilder |
| 6 | +import proguard.testutils.KotlinSource |
| 7 | + |
| 8 | +class KotlinTwoPointTwoTest : FunSpec({ |
| 9 | + test("Non-local break & continue") { |
| 10 | + val (programClassPool, _) = ClassPoolBuilder.fromSource( |
| 11 | + KotlinSource( |
| 12 | + "Test.kt", |
| 13 | + """ |
| 14 | + fun processList(elements: List<Int>): Boolean { |
| 15 | + for (element in elements) { |
| 16 | + val variable = element ?: run { |
| 17 | + continue |
| 18 | + } |
| 19 | + if (variable == 0) return true |
| 20 | + } |
| 21 | + return false |
| 22 | + } |
| 23 | + """.trimIndent(), |
| 24 | + ) |
| 25 | + ) |
| 26 | + |
| 27 | + programClassPool.classesAccept( |
| 28 | + ReferencedKotlinMetadataVisitor( |
| 29 | + KotlinMetadataPrinter( |
| 30 | + programClassPool |
| 31 | + ) |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + val testKtMetadata = programClassPool.getClass("TestKt").processingInfo as String |
| 36 | + println(testKtMetadata) |
| 37 | + |
| 38 | + testKtMetadata.trimEnd() shouldBe """ |
| 39 | + /** |
| 40 | + * Kotlin file facade (metadata version 2.2.0). |
| 41 | + * From Java class: TestKt |
| 42 | + */ |
| 43 | + |
| 44 | + // Functions |
| 45 | + |
| 46 | + fun processList(elements: List<Int>): Boolean { } |
| 47 | + """.trimIndent() |
| 48 | + } |
| 49 | + test("Nested type aliases") { |
| 50 | + val (programClassPool, _) = ClassPoolBuilder.fromSource( |
| 51 | + KotlinSource( |
| 52 | + "Test.kt", |
| 53 | + """ |
| 54 | + class Container { |
| 55 | + typealias ContainerSet = Set<Container> |
| 56 | + } |
| 57 | + |
| 58 | + """.trimIndent(), |
| 59 | + ), |
| 60 | + kotlincArguments = listOf("-Xnested-type-aliases") |
| 61 | + ) |
| 62 | + |
| 63 | + programClassPool.classesAccept( |
| 64 | + ReferencedKotlinMetadataVisitor( |
| 65 | + KotlinMetadataPrinter( |
| 66 | + programClassPool |
| 67 | + ) |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + val containerMetadata = programClassPool.getClass("Container").processingInfo as String |
| 72 | + println(containerMetadata) |
| 73 | + |
| 74 | + containerMetadata.trimEnd() shouldBe """ |
| 75 | + /** |
| 76 | + * Kotlin class (metadata version 2.2.0). |
| 77 | + * From Java class: Container |
| 78 | + */ |
| 79 | + class Container { |
| 80 | + |
| 81 | + // Type aliases |
| 82 | + |
| 83 | + typealias ContainerSet = Set<Container> |
| 84 | + } |
| 85 | + """.trimIndent() |
| 86 | + } |
| 87 | + test("RequiresOptIn annotations") { |
| 88 | + val (programClassPool, _) = ClassPoolBuilder.fromSource( |
| 89 | + KotlinSource( |
| 90 | + "Test.kt", |
| 91 | + """ |
| 92 | + @RequiresOptIn( |
| 93 | + level = RequiresOptIn.Level.WARNING, |
| 94 | + message = "Interfaces in this library are experimental" |
| 95 | + ) |
| 96 | + annotation class UnstableApi() |
| 97 | + |
| 98 | + @SubclassOptInRequired(UnstableApi::class) |
| 99 | + interface CoreLibraryApi |
| 100 | + """.trimIndent(), |
| 101 | + ), |
| 102 | + ) |
| 103 | + |
| 104 | + programClassPool.classesAccept( |
| 105 | + ReferencedKotlinMetadataVisitor( |
| 106 | + KotlinMetadataPrinter( |
| 107 | + programClassPool |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + val coreLibraryApiMetadata = programClassPool.getClass("CoreLibraryApi").processingInfo as String |
| 112 | + println(coreLibraryApiMetadata) |
| 113 | + |
| 114 | + coreLibraryApiMetadata.trimEnd() shouldBe """ |
| 115 | + /** |
| 116 | + * Kotlin class (metadata version 2.2.0). |
| 117 | + * From Java class: CoreLibraryApi |
| 118 | + */ |
| 119 | + @SubclassOptInRequired(markerClass = {UnstableApi}) |
| 120 | + interface CoreLibraryApi |
| 121 | + """.trimIndent() |
| 122 | + |
| 123 | + val unstableApiMetadata = programClassPool.getClass("UnstableApi").processingInfo as String |
| 124 | + println(unstableApiMetadata) |
| 125 | + |
| 126 | + unstableApiMetadata.trimEnd() shouldBe """ |
| 127 | + /** |
| 128 | + * Kotlin class (metadata version 2.2.0). |
| 129 | + * From Java class: UnstableApi |
| 130 | + */ |
| 131 | + @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) |
| 132 | + @RequiresOptIn(message = "Interfaces in this library are experimental", level = RequiresOptIn.Level.WARNING) |
| 133 | + annotation class UnstableApi |
| 134 | + """.trimIndent() |
| 135 | + } |
| 136 | + test("Guard conditions in when with a subject") { |
| 137 | + val (programClassPool, _) = ClassPoolBuilder.fromSource( |
| 138 | + KotlinSource( |
| 139 | + "Test.kt", |
| 140 | + """ |
| 141 | + sealed interface Animal { |
| 142 | + data class Cat(val mouseHunter: Boolean) : Animal { |
| 143 | + fun feedCat() {} |
| 144 | + } |
| 145 | + |
| 146 | + data class Dog(val breed: String) : Animal { |
| 147 | + fun feedDog() {} |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + fun feedAnimal(animal: Animal) { |
| 152 | + when (animal) { |
| 153 | + is Animal.Dog -> animal.feedDog() |
| 154 | + is Animal.Cat if !animal.mouseHunter -> animal.feedCat() |
| 155 | + else -> println("Unknown animal") |
| 156 | + } |
| 157 | + } |
| 158 | + """.trimIndent(), |
| 159 | + ) |
| 160 | + ) |
| 161 | + |
| 162 | + programClassPool.classesAccept( |
| 163 | + ReferencedKotlinMetadataVisitor( |
| 164 | + KotlinMetadataPrinter( |
| 165 | + programClassPool |
| 166 | + ) |
| 167 | + ) |
| 168 | + ) |
| 169 | + |
| 170 | + val testKtMetadata = programClassPool.getClass("TestKt").processingInfo as String |
| 171 | + println(testKtMetadata) |
| 172 | + |
| 173 | + testKtMetadata.trimEnd() shouldBe """ |
| 174 | + /** |
| 175 | + * Kotlin file facade (metadata version 2.2.0). |
| 176 | + * From Java class: TestKt |
| 177 | + */ |
| 178 | + |
| 179 | + // Functions |
| 180 | + |
| 181 | + fun feedAnimal(animal: Animal) { } |
| 182 | + """.trimIndent() |
| 183 | + } |
| 184 | + test("Multi-dollar string interpolation") { |
| 185 | + val (programClassPool, _) = ClassPoolBuilder.fromSource( |
| 186 | + KotlinSource( |
| 187 | + "Test.kt", |
| 188 | + """ |
| 189 | + class MultiDollar { |
| 190 | + companion object { |
| 191 | + val multiDollarString: String |
| 192 | + get() = ${"$$"}${"\"\"\""}aMultiDollarString${"\"\"\""} |
| 193 | + } |
| 194 | + } |
| 195 | + """.trimIndent(), |
| 196 | + ) |
| 197 | + ) |
| 198 | + |
| 199 | + programClassPool.classesAccept( |
| 200 | + ReferencedKotlinMetadataVisitor( |
| 201 | + KotlinMetadataPrinter( |
| 202 | + programClassPool |
| 203 | + ) |
| 204 | + ) |
| 205 | + ) |
| 206 | + |
| 207 | + val multiDollarMetadata = programClassPool.getClass("MultiDollar").processingInfo as String |
| 208 | + println(multiDollarMetadata) |
| 209 | + |
| 210 | + multiDollarMetadata.trimEnd() shouldBe """ |
| 211 | + /** |
| 212 | + * Kotlin class (metadata version 2.2.0). |
| 213 | + * From Java class: MultiDollar |
| 214 | + */ |
| 215 | + class MultiDollar { |
| 216 | + |
| 217 | + // Kotlin companion class from Java class: MultiDollar${"$"}Companion |
| 218 | + companion object { |
| 219 | + |
| 220 | + // Properties |
| 221 | + |
| 222 | + val multiDollarString: String |
| 223 | + get // getter method: public final java.lang.String getMultiDollarString() |
| 224 | + } |
| 225 | + |
| 226 | + // Synthetic inner classes - these were generated by the Kotlin compiler from e.g. lambdas |
| 227 | + |
| 228 | + // Kotlin companion class from Java class: MultiDollar${"$"}Companion |
| 229 | + companion object { |
| 230 | + |
| 231 | + // Properties |
| 232 | + |
| 233 | + val multiDollarString: String |
| 234 | + get // getter method: public final java.lang.String getMultiDollarString() |
| 235 | + } |
| 236 | + } |
| 237 | + """.trimIndent() |
| 238 | + |
| 239 | + val multiDollarCompanionMetadata = programClassPool.getClass("MultiDollar${"$"}Companion").processingInfo as String |
| 240 | + println(multiDollarCompanionMetadata) |
| 241 | + |
| 242 | + multiDollarCompanionMetadata.trimEnd() shouldBe """ |
| 243 | + /** |
| 244 | + * Kotlin companion class (metadata version 2.2.0). |
| 245 | + * From Java class: MultiDollar${"$"}Companion |
| 246 | + */ |
| 247 | + companion object MultiDollar_Companion { |
| 248 | + |
| 249 | + // Properties |
| 250 | + |
| 251 | + val multiDollarString: String |
| 252 | + get // getter method: public final java.lang.String getMultiDollarString() |
| 253 | + } |
| 254 | + """.trimIndent() |
| 255 | + } |
| 256 | +}) |
0 commit comments