Add options param to external dependencies resolver API
This commit is contained in:
committed by
Ilya Chernikov
parent
83087291df
commit
e45e491718
+94
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.test
|
||||
|
||||
import junit.framework.TestCase
|
||||
import kotlin.script.experimental.api.ResultWithDiagnostics
|
||||
import kotlin.script.experimental.api.valueOrThrow
|
||||
import kotlin.script.experimental.dependencies.impl.SimpleExternalDependenciesResolverOptionsParser
|
||||
import kotlin.script.experimental.dependencies.impl.makeExternalDependenciesResolverOptions
|
||||
|
||||
class ResolverOptionsTest : TestCase() {
|
||||
fun testValueInMapAppearsIfPresent() {
|
||||
val map = mapOf("option" to "value")
|
||||
val options = makeExternalDependenciesResolverOptions(map)
|
||||
|
||||
assertEquals(options.value("option"), "value")
|
||||
}
|
||||
|
||||
fun testFlagInMapAppearsIfPresent() {
|
||||
val map = mapOf("option" to "true")
|
||||
val options = makeExternalDependenciesResolverOptions(map)
|
||||
|
||||
assertEquals(options.value("option"), "true")
|
||||
assertEquals(options.flag("option"), true)
|
||||
}
|
||||
|
||||
fun testValueInMapDoesNotAppearsIfPresent() {
|
||||
val options = makeExternalDependenciesResolverOptions(emptyMap())
|
||||
assertNull(options.value("option"))
|
||||
}
|
||||
|
||||
fun testFlagInMapDoesNotAppearsIfPresent() {
|
||||
val options = makeExternalDependenciesResolverOptions(emptyMap())
|
||||
assertNull(options.flag("option"))
|
||||
}
|
||||
|
||||
fun testParserReturnsSingleValue() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "hello")
|
||||
}
|
||||
|
||||
fun testParserReturnsMultipleValue() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello option2 = 42").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "hello")
|
||||
assertEquals(options.value("option2"), "42")
|
||||
}
|
||||
|
||||
|
||||
fun testParserReturnsSingleFlag() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "hello")
|
||||
}
|
||||
|
||||
fun testParserReturnsMultipleFlags() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 option2=false option3").valueOrThrow()
|
||||
assertEquals(options.flag("option1"), true)
|
||||
assertEquals(options.flag("option2"), false)
|
||||
assertEquals(options.flag("option3"), true)
|
||||
}
|
||||
|
||||
fun testParserReturnsMixOfValuesAndFlags() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello option2 option3=world option4 option5 = false").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "hello")
|
||||
assertEquals(options.flag("option2"), true)
|
||||
assertEquals(options.value("option3"), "world")
|
||||
assertEquals(options.flag("option4"), true)
|
||||
assertEquals(options.flag("option5"), false)
|
||||
}
|
||||
|
||||
fun testParserReportsClashWithConflictingOptions() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
when (val result = parser("option1 = hello option1 = world")) {
|
||||
is ResultWithDiagnostics.Success -> fail("Managed to parse options despite conflicting options: ${result.value}")
|
||||
is ResultWithDiagnostics.Failure -> {
|
||||
assertEquals(result.reports.count(), 1)
|
||||
assertEquals(result.reports.first().message, "Conflicting values for option option1: hello and world")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun testParserDoesNotClashWithTheSameOptionTwice() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello option1 = hello").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "hello")
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,7 @@ class ResolversTest : ResolversTestBase() {
|
||||
|
||||
override suspend fun resolve(
|
||||
artifactCoordinates: String,
|
||||
options: ExternalDependenciesResolver.Options,
|
||||
sourceCodeLocation: SourceCode.LocationWithId?
|
||||
): ResultWithDiagnostics<List<File>> {
|
||||
if (!acceptsArtifact(artifactCoordinates)) throw Exception("Path is invalid")
|
||||
@@ -97,6 +98,7 @@ class ResolversTest : ResolversTestBase() {
|
||||
|
||||
override fun addRepository(
|
||||
repositoryCoordinates: RepositoryCoordinates,
|
||||
options: ExternalDependenciesResolver.Options,
|
||||
sourceCodeLocation: SourceCode.LocationWithId?
|
||||
): ResultWithDiagnostics<Boolean> {
|
||||
if (!acceptsRepository(repositoryCoordinates)) return false.asSuccess()
|
||||
|
||||
Reference in New Issue
Block a user