From: David ‘Bombe’ Roden Date: Thu, 31 Aug 2017 19:29:53 +0000 (+0200) Subject: Add helper for booleans X-Git-Tag: 0.9.7^2~99 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b51bf84d05e317d8a8179d72a788190b350a95a1;p=Sone.git Add helper for booleans --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Booleans.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Booleans.kt new file mode 100644 index 0000000..3202b82 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Booleans.kt @@ -0,0 +1,6 @@ +package net.pterodactylus.sone.utils + +/** + * Returns the value of [block] if `this` is true, returns `null` otherwise. + */ +fun Boolean.ifTrue(block: () -> R): R? = if (this) block() else null diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/BooleansTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/BooleansTest.kt new file mode 100644 index 0000000..5e5ce51 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/BooleansTest.kt @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.utils + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Unit test for [Booleans]. + */ +class BooleansTest { + + @Test + fun `ifTrue is executed if boolean is true`() { + assertThat(true.ifTrue { true }, equalTo(true)) + } + + @Test + fun `ifTrue is not executed if boolean is false`() { + assertThat(false.ifTrue { true }, nullValue()) + } + +}