Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error during snippet compilation (#4536)
- Loading branch information
Showing
5 changed files
with
162 additions
and
22 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
50 changes: 50 additions & 0 deletions
50
detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/KotlinScriptEngineTest.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 io.github.detekt.test.utils | ||
|
||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.RepeatedTest | ||
import org.junit.jupiter.api.Test | ||
|
||
class KotlinScriptEngineTest { | ||
|
||
@Test | ||
fun `can compile a valid script`() { | ||
val code = """ | ||
package foo.a | ||
class A | ||
""".trimIndent() | ||
KotlinScriptEngine.compile(code) | ||
} | ||
|
||
@Test | ||
fun `fails compiling an invalid script`() { | ||
val invalidCode = """ | ||
package foo.b | ||
val unknownType: UnknownType | ||
""".trimIndent() | ||
assertThatThrownBy { KotlinScriptEngine.compile(invalidCode) } | ||
.isInstanceOf(KotlinScriptException::class.java) | ||
} | ||
|
||
@RepeatedTest(10) | ||
fun `can compile the same script repeatedly`() { | ||
val code = """ | ||
package foo.c | ||
class A | ||
""".trimIndent() | ||
KotlinScriptEngine.compile(code) | ||
} | ||
|
||
@RepeatedTest(10) | ||
fun `fails repeatedly on invalid script`() { | ||
val invalidCode = """ | ||
package foo.d | ||
val unknownType: UnknownType | ||
""".trimIndent() | ||
assertThatThrownBy { KotlinScriptEngine.compile(invalidCode) } | ||
.isInstanceOf(KotlinScriptException::class.java) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/PooledScriptEngineTest.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,56 @@ | ||
package io.github.detekt.test.utils | ||
|
||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.jetbrains.kotlin.util.KotlinFrontEndException | ||
import org.junit.jupiter.api.Test | ||
import javax.script.ScriptException | ||
|
||
internal class PooledScriptEngineTest { | ||
|
||
@Test | ||
fun `invalid code fails with ScriptException`() { | ||
val invalidCode = """ | ||
val unknownType: Foo | ||
""".trimIndent() | ||
|
||
val engine = KotlinScriptEnginePool.borrowEngine() | ||
|
||
assertThatThrownBy { engine.compile(invalidCode) } | ||
.isInstanceOf(ScriptException::class.java) | ||
} | ||
|
||
@Test | ||
fun `compiling the same type twice leads to a compiler error`() { | ||
val validCode = """ | ||
package pooled | ||
class A | ||
""".trimIndent() | ||
|
||
val engine = KotlinScriptEnginePool.borrowEngine() | ||
|
||
engine.compile(validCode) | ||
assertThatThrownBy { engine.compile(validCode) } | ||
.isInstanceOf(KotlinFrontEndException::class.java) | ||
} | ||
|
||
@Test | ||
fun `can be reused after failing to compile an invalid script`() { | ||
val invalidCode = """ | ||
val unknownType: Foo | ||
""".trimIndent() | ||
|
||
val validCode = """ | ||
package pooled.c | ||
class A | ||
""".trimIndent() | ||
|
||
val engine = KotlinScriptEnginePool.borrowEngine() | ||
|
||
assertThatThrownBy { engine.compile(invalidCode) } | ||
.isInstanceOf(ScriptException::class.java) | ||
|
||
engine.compile(validCode) | ||
} | ||
} |