mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-05 12:05:58 +01:00
Add Sugar file containing utility scope functions
This commit is contained in:
parent
2db259f00d
commit
31a7a822ac
2 changed files with 136 additions and 0 deletions
69
wot-dijkstra/src/main/kotlin/org/pgpainless/wot/Sugar.kt
Normal file
69
wot-dijkstra/src/main/kotlin/org/pgpainless/wot/Sugar.kt
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.wot
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of [let] which takes an additional [predicate] and returns the result of applying
|
||||||
|
* [block] iff [predicate] evaluates to true, otherwise it returns the value unchanged.
|
||||||
|
*
|
||||||
|
* @param predicate boolean
|
||||||
|
* @param block function block that is applied to [this] iff [predicate] evaluates to true
|
||||||
|
* @return the result of applying [block] to [this] iff [predicate] evaluates to true, [this] otherwise
|
||||||
|
* @receiver [this]
|
||||||
|
*/
|
||||||
|
fun <T> T.letIf(predicate: T.() -> Boolean, block: T.() -> T): T =
|
||||||
|
let {
|
||||||
|
if (predicate(this))
|
||||||
|
block(this)
|
||||||
|
else
|
||||||
|
this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of [let] which takes an additional [condition] and returns the result of applying
|
||||||
|
* [block] iff [condition] is true, otherwise it returns the value unchanged.
|
||||||
|
*
|
||||||
|
* @param condition boolean
|
||||||
|
* @param block function block that is applied to [this] iff [condition] is true
|
||||||
|
* @return the result of applying [block] to [this] iff [condition] is true, [this] otherwise
|
||||||
|
* @receiver [this]
|
||||||
|
*/
|
||||||
|
fun <T> T.letIf(condition: Boolean, block: T.() -> T): T =
|
||||||
|
let {
|
||||||
|
if (condition)
|
||||||
|
block(this)
|
||||||
|
else
|
||||||
|
this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of [apply] which takes an additional [predicate] and only applies the [block]
|
||||||
|
* iff [predicate] evaluates to true.
|
||||||
|
*
|
||||||
|
* @param predicate predicate on [this]
|
||||||
|
* @param block function block that is applied to [this] iff [predicate] evaluates to true
|
||||||
|
* @return the result of applying [block] to this iff [predicate] evaluates to true, otherwise [this] unchanged
|
||||||
|
* @receiver [this]
|
||||||
|
*/
|
||||||
|
fun <T> T.applyIf(predicate: T.() -> Boolean, block: T.() -> Unit): T =
|
||||||
|
apply {
|
||||||
|
if (predicate(this))
|
||||||
|
block(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of [apply] which takes an additional [condition] and only applies the [block]
|
||||||
|
* iff the [condition] is true.
|
||||||
|
*
|
||||||
|
* @param condition boolean
|
||||||
|
* @param block function block that is applied to [this] iff [condition] is true
|
||||||
|
* @return the result of applying [block] to this iff [condition] is true, otherwise [this] unchanged
|
||||||
|
* @receiver [this]
|
||||||
|
*/
|
||||||
|
fun <T> T.applyIf(condition: Boolean, block: T.() -> Unit): T =
|
||||||
|
apply {
|
||||||
|
if (condition)
|
||||||
|
block(this)
|
||||||
|
}
|
67
wot-dijkstra/src/test/kotlin/org/pgpainless/wot/SugarTest.kt
Normal file
67
wot-dijkstra/src/test/kotlin/org/pgpainless/wot/SugarTest.kt
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.wot
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class SugarTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test letIf(predicate, block) with a predicate that evaluates to true`() {
|
||||||
|
assertEquals("Hello, PGP!", "Hello, GPG!".letIf({ contains("GPG") }) {
|
||||||
|
replace("GPG", "PGP")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test letIf(predicate, block) with a predicate that evaluates to false`() {
|
||||||
|
assertEquals("Hello, PGP!", "Hello, PGP!".letIf({ contains("GPG")}) {
|
||||||
|
replace("Hello", "Salut")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test letIf(condition, block) with true condition`() {
|
||||||
|
assertEquals("hello, pgp!", "Hello, PGP!".letIf(true) {
|
||||||
|
lowercase()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test letIf(condition, block) with false condition`() {
|
||||||
|
assertEquals("Hello, PGP!", "Hello, PGP!".letIf(false) {
|
||||||
|
lowercase()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test applyIf(predicate, block) with predicate that evaluates to true`() {
|
||||||
|
assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B").applyIf({ !contains("C") }) {
|
||||||
|
add("C")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test applyIf(predicate, block) with predicate that evaluates to false`() {
|
||||||
|
assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B", "C").applyIf({ !contains("C")}) {
|
||||||
|
add("C")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test applyIf(condition, block) with true condition`() {
|
||||||
|
assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B").applyIf(true) {
|
||||||
|
add("C")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test applyIf(condition, block) with false condition`() {
|
||||||
|
assertEquals(listOf("A", "B"), mutableListOf("A", "B").applyIf(false) {
|
||||||
|
add("C")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue