Skip to content

Commit

Permalink
Allow disabling preview features
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleex255 committed Apr 25, 2020
1 parent 2c8f16a commit 4559ac5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.idea/modules/test-output/
.idea/libraries/
.idea/checkstyle-idea.xml
.idea/uiDesigner.xml
.kotlintest/
out/
build/
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/kotlin/Compile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ val systemCompilerVersion = systemCompilerName.let {
data class CompilationArguments(
val wError: Boolean = DEFAULT_WERROR,
@Suppress("ConstructorParameterNaming") val Xlint: String = DEFAULT_XLINT,
val enablePreview: Boolean = DEFAULT_ENABLE_PREVIEW,
@Transient val parentFileManager: JavaFileManager? = null,
@Transient val parentClassLoader: ClassLoader? = null,
val useCache: Boolean = useCompilationCache,
Expand All @@ -46,6 +47,7 @@ data class CompilationArguments(
companion object {
const val DEFAULT_WERROR = false
const val DEFAULT_XLINT = "all"
const val DEFAULT_ENABLE_PREVIEW = true
const val PREVIEW_STARTED = 11
}
override fun equals(other: Any?): Boolean {
Expand All @@ -55,13 +57,19 @@ data class CompilationArguments(
other as CompilationArguments

if (wError != other.wError) return false
if (enablePreview != other.enablePreview) return false
if (Xlint != other.Xlint) return false
if (useCache != other.useCache) return false
if (waitForCache != other.waitForCache) return false

return true
}
override fun hashCode(): Int {
var result = wError.hashCode()
result = 31 * result + enablePreview.hashCode()
result = 31 * result + Xlint.hashCode()
result = 31 * result + useCache.hashCode()
result = 31 * result + waitForCache.hashCode()
return result
}
}
Expand Down Expand Up @@ -114,7 +122,7 @@ private fun compile(
val options = mutableSetOf<String>()
options.add("-Xlint:${compilationArguments.Xlint}")

if (systemCompilerVersion >= CompilationArguments.PREVIEW_STARTED) {
if (compilationArguments.enablePreview && systemCompilerVersion >= CompilationArguments.PREVIEW_STARTED) {
options.addAll(listOf("--enable-preview", "--release", systemCompilerVersion.toString()))
}

Expand Down
43 changes: 40 additions & 3 deletions core/src/test/kotlin/TestCompile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ public class Test {
compiledSource should haveProvidedThisManyClasses(0)
}
}

"should compile sources that use Java 14 features" {
if (systemCompilerVersion < 14) {
throw SkipTestException("Cannot run this test until Java 14")
Expand All @@ -401,14 +400,52 @@ public class Test {
public static void main() {
testInstanceOfPatternMatching();
}
}""".trim()
)
}""".trim())
).compile()

compiledSource should haveDefinedExactlyTheseClasses(setOf("Test"))
compiledSource should haveProvidedThisManyClasses(0)
}
}
"should compile sources that use Java 14 preview features" {
if (systemCompilerVersion < 14) {
throw SkipTestException("Cannot run this test until Java 14")
} else {
val tripleQuote = "\"\"\""
val compiledSource = Source(
mapOf(
"Test.java" to """
public class Test {
public static void main() {
String textBlock = $tripleQuote
Hello world!
$tripleQuote;
}
}""".trim())
).compile()

compiledSource should haveDefinedExactlyTheseClasses(setOf("Test"))
compiledSource should haveProvidedThisManyClasses(0)
}
}
"should not support Java 14 preview features when preview is disabled" {
if (systemCompilerVersion < 14) {
throw SkipTestException("Cannot run this test until Java 14")
} else {
shouldThrow<CompilationFailed> {
val tripleQuote = "\"\"\""
Source(mapOf("Test.java" to """
public class Test {
public static void main() {
String textBlock = $tripleQuote
Hello world!
$tripleQuote;
}
}""".trim())
).compile(CompilationArguments(enablePreview = false))
}
}
}
})

fun haveCompilationErrorAt(source: String = SNIPPET_SOURCE, line: Int, column: Int? = null) =
Expand Down

0 comments on commit 4559ac5

Please sign in to comment.