From 958fbc03bafa2a2462d14901d4111114f8516bf4 Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 24 Apr 2020 22:23:01 +0100 Subject: [PATCH] Add IterableUtilsTest Signed-off-by: Graham --- .../openrs2/util/collect/IterableUtilsTest.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 util/src/test/java/dev/openrs2/util/collect/IterableUtilsTest.kt diff --git a/util/src/test/java/dev/openrs2/util/collect/IterableUtilsTest.kt b/util/src/test/java/dev/openrs2/util/collect/IterableUtilsTest.kt new file mode 100644 index 00000000..a480f975 --- /dev/null +++ b/util/src/test/java/dev/openrs2/util/collect/IterableUtilsTest.kt @@ -0,0 +1,55 @@ +package dev.openrs2.util.collect + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +object IterableUtilsTest { + @Test + fun testRemoveFirst() { + val list = mutableListOf(1, 2, 3) + assertEquals(1, list.removeFirst()) + assertEquals(listOf(2, 3), list) + + assertEquals(2, list.removeFirst()) + assertEquals(listOf(3), list) + + assertEquals(3, list.removeFirst()) + assertEquals(emptyList(), list) + + assertThrows { + list.removeFirst() + } + } + + @Test + fun testRemoveFirstOrNull() { + val list = mutableListOf(1, 2, 3) + assertEquals(1, list.removeFirstOrNull()) + assertEquals(listOf(2, 3), list) + + assertEquals(2, list.removeFirstOrNull()) + assertEquals(listOf(3), list) + + assertEquals(3, list.removeFirstOrNull()) + assertEquals(emptyList(), list) + + assertNull(list.removeFirstOrNull()) + } + + @Test + fun testRemoveFirstMatching() { + val list = mutableListOf(1, 2, 2, 3) + assertTrue(list.removeFirst { it == 2 }) + assertEquals(listOf(1, 2, 3), list) + + assertTrue(list.removeFirst { it == 2 }) + assertEquals(listOf(1, 3), list) + + assertFalse(list.removeFirst { it == 2 }) + assertEquals(listOf(1, 3), list) + } +}