-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
287 additions
and
10 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
102 changes: 102 additions & 0 deletions
102
src/main/kotlin/pcimcioch/gitlabci/dsl/job/GlobalVariablesDsl.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,102 @@ | ||
package pcimcioch.gitlabci.dsl.job | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.MapSerializer | ||
import kotlinx.serialization.builtins.serializer | ||
import pcimcioch.gitlabci.dsl.DslBase | ||
import pcimcioch.gitlabci.dsl.serializer.ValueSerializer | ||
|
||
@Serializable(with = GlobalVariablesDsl.GlobalVariablesDslSerializer::class) | ||
class GlobalVariablesDsl : DslBase() { | ||
var variables: MutableMap<String, GlobalVariableDsl> = mutableMapOf() | ||
|
||
fun add(name: String, block: GlobalVariableDsl.() -> Unit) = variables.put(name, createGlobalVariable(block)) | ||
fun add(name: String, value: GlobalVariableDsl) = variables.put(name, value) | ||
fun add(name: String, value: Any) = variables.put(name, createGlobalVariable(value.toString())) | ||
|
||
fun <T : Enum<T>> add(name: T, block: GlobalVariableDsl.() -> Unit) = variables.put(name.toString(), createGlobalVariable(block)) | ||
fun <T : Enum<T>> add(name: T, value: GlobalVariableDsl) = variables.put(name.toString(), value) | ||
fun <T : Enum<T>> add(name: T, value: Any) = variables.put(name.toString(), createGlobalVariable(value.toString())) | ||
|
||
infix fun String.to(block: GlobalVariableDsl.() -> Unit) = add(this, createGlobalVariable(block)) | ||
infix fun String.to(value: GlobalVariableDsl) = add(this, value) | ||
infix fun String.to(value: Any) = add(this, value) | ||
|
||
infix fun <T : Enum<T>> T.to(block: GlobalVariableDsl.() -> Unit) = add(this, createGlobalVariable(block)) | ||
infix fun <T : Enum<T>> T.to(value: GlobalVariableDsl) = add(this, value) | ||
infix fun <T : Enum<T>> T.to(value: Any) = add(this, value) | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as GlobalVariablesDsl | ||
|
||
if (variables != other.variables) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return variables.hashCode() | ||
} | ||
|
||
object GlobalVariablesDslSerializer : ValueSerializer<GlobalVariablesDsl, Map<String, GlobalVariableDsl>>( | ||
MapSerializer(String.serializer(), GlobalVariableDsl.serializer()), | ||
GlobalVariablesDsl::variables | ||
) | ||
|
||
companion object { | ||
init { | ||
addSerializer(GlobalVariablesDsl::class, serializer()) | ||
} | ||
} | ||
} | ||
|
||
fun createGlobalVariables(block: GlobalVariablesDsl.() -> Unit = {}) = GlobalVariablesDsl().apply(block) | ||
|
||
@Serializable | ||
class GlobalVariableDsl : DslBase() { | ||
var description: String? = null | ||
var value: String? = null | ||
var options: MutableList<String>? = null | ||
var expand: Boolean? = null | ||
|
||
fun options(vararg elements: String) = options(elements.toList()) | ||
fun options(elements: Iterable<String>) = ensureOptions().addAll(elements) | ||
|
||
private fun ensureOptions() = options ?: mutableListOf<String>().also { options = it } | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as GlobalVariableDsl | ||
|
||
if (description != other.description) return false | ||
if (value != other.value) return false | ||
if (options != other.options) return false | ||
if (expand != other.expand) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = description?.hashCode() ?: 0 | ||
result = 31 * result + (value?.hashCode() ?: 0) | ||
result = 31 * result + (options?.hashCode() ?: 0) | ||
result = 31 * result + (expand?.hashCode() ?: 0) | ||
return result | ||
} | ||
|
||
companion object { | ||
init { | ||
addSerializer(GlobalVariableDsl::class, serializer()) | ||
} | ||
} | ||
} | ||
|
||
fun createGlobalVariable(block: GlobalVariableDsl.() -> Unit = {}) = GlobalVariableDsl().apply(block) | ||
fun createGlobalVariable(value: String, block: GlobalVariableDsl.() -> Unit = {}) = GlobalVariableDsl().apply { | ||
this.value = value | ||
}.apply(block) |
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
165 changes: 165 additions & 0 deletions
165
src/test/kotlin/pcimcioch/gitlabci/dsl/job/GlobalVariablesDslTest.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,165 @@ | ||
package pcimcioch.gitlabci.dsl.job | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import pcimcioch.gitlabci.dsl.DslTestBase | ||
|
||
internal class GlobalVariablesDslTest : DslTestBase<GlobalVariablesDsl>(GlobalVariablesDsl.serializer()) { | ||
|
||
@Test | ||
fun `should create from block`() { | ||
// given | ||
val testee = createGlobalVariables { | ||
add("key1", "value 1") | ||
} | ||
|
||
// then | ||
assertDsl( | ||
testee, | ||
""" | ||
"key1": | ||
value: "value 1" | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `should create empty variables`() { | ||
// given | ||
val testee = createGlobalVariables {} | ||
|
||
// then | ||
assertDsl( | ||
testee, | ||
""" | ||
{} | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `should allow multiple types of adding`() { | ||
// given | ||
val testee = createGlobalVariables { | ||
add("key1", "value1") | ||
add("key2", createGlobalVariable("value2")) | ||
add("key3") { | ||
description = "value 3 description" | ||
options = mutableListOf("t1", "t2") | ||
} | ||
|
||
add(RunnerSettingsVariables.GIT_CLONE_PATH, "value4") | ||
add(RunnerSettingsVariables.ARTIFACT_DOWNLOAD_ATTEMPTS, createGlobalVariable("value5")) | ||
add(RunnerSettingsVariables.CACHE_FALLBACK_KEY) { | ||
description = "value 6 description" | ||
expand = false | ||
} | ||
|
||
"key7" to "value7" | ||
"key8" to createGlobalVariable("value8") | ||
"key9" to { | ||
description = "value 9 description" | ||
} | ||
|
||
RunnerSettingsVariables.GIT_DEPTH to "value10" | ||
RunnerSettingsVariables.GIT_STRATEGY to createGlobalVariable("value11") | ||
RunnerSettingsVariables.GIT_SUBMODULE_STRATEGY to { | ||
description = "value 12 description" | ||
} | ||
} | ||
|
||
// then | ||
assertDsl( | ||
testee, | ||
""" | ||
"key1": | ||
value: "value1" | ||
"key2": | ||
value: "value2" | ||
"key3": | ||
description: "value 3 description" | ||
options: | ||
- "t1" | ||
- "t2" | ||
"GIT_CLONE_PATH": | ||
value: "value4" | ||
"ARTIFACT_DOWNLOAD_ATTEMPTS": | ||
value: "value5" | ||
"CACHE_FALLBACK_KEY": | ||
description: "value 6 description" | ||
expand: false | ||
"key7": | ||
value: "value7" | ||
"key8": | ||
value: "value8" | ||
"key9": | ||
description: "value 9 description" | ||
"GIT_DEPTH": | ||
value: "value10" | ||
"GIT_STRATEGY": | ||
value: "value11" | ||
"GIT_SUBMODULE_STRATEGY": | ||
description: "value 12 description" | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `should create direct access`() { | ||
// given | ||
val map = mutableMapOf( | ||
"key1" to createGlobalVariable("value 1"), | ||
"key2" to createGlobalVariable("value 2") | ||
) | ||
|
||
val testee = createGlobalVariables { | ||
variables = map | ||
} | ||
|
||
// then | ||
assertDsl( | ||
testee, | ||
""" | ||
"key1": | ||
value: "value 1" | ||
"key2": | ||
value: "value 2" | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `should override on add`() { | ||
// given | ||
val testee = createGlobalVariables { | ||
add("key1", "value 1") | ||
add("key1", "value 2") | ||
} | ||
|
||
// then | ||
assertDsl( | ||
testee, | ||
""" | ||
"key1": | ||
value: "value 2" | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `should be equal`() { | ||
// given | ||
val testee = createGlobalVariables { | ||
add("one", "one") | ||
add("two", "two") | ||
} | ||
|
||
val expected = createGlobalVariables { | ||
add("one", "one") | ||
add("two", "two") | ||
} | ||
|
||
// then | ||
assertEquals(expected, testee) | ||
} | ||
} |