Skip to content

Commit

Permalink
Add EnumExt utils
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Nov 7, 2023
1 parent cc84bce commit 690d48b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.sipgate.dachlatten.primitives

inline fun <reified T : Enum<T>> T.isAnyOf(vararg values: T) = values.any { this == it }

inline fun <reified T : Enum<T>> T.isNoneOf(vararg values: T) = values.none { this == it }
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.sipgate.dachlatten.primitives

import de.sipgate.dachlatten.primitives.EnumExtTest.SomeValues.A
import de.sipgate.dachlatten.primitives.EnumExtTest.SomeValues.B
import de.sipgate.dachlatten.primitives.EnumExtTest.SomeValues.C
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class EnumExtTest {
enum class SomeValues {
A,
B,
C,
}

@Test
fun testIsAnyOfSelf() {
assertTrue(A.isAnyOf(A))
}

@Test
fun testIsAnyOfWithSubset() {
assertTrue(A.isAnyOf(A, B))
}

@Test
fun testIsAnyOfFullEnum() {
assertTrue(A.isAnyOf(*SomeValues.entries.toTypedArray()))
}

@Test
fun testIsAnyOfNotContained() {
assertFalse(A.isAnyOf(B, C))
}

@Test
fun testIsNoneOfSelfIsFalse() {
assertFalse(A.isNoneOf(A))
}

@Test
fun testIsNoneOfOther() {
assertTrue(A.isNoneOf(B))
}

@Test
fun testIsNoneOfNotContained() {
assertTrue(A.isNoneOf(B, C))
}
}

0 comments on commit 690d48b

Please sign in to comment.