diff --git a/kategory/src/main/kotlin/kategory/Maps.kt b/kategory/src/main/kotlin/kategory/Maps.kt new file mode 100644 index 00000000000..5b47bf67ef3 --- /dev/null +++ b/kategory/src/main/kotlin/kategory/Maps.kt @@ -0,0 +1,4 @@ +package kategory + +fun mapOf(vararg tuple: Tuple2): Map = + if (tuple.isNotEmpty()) tuple.map { it.a to it.b }.toMap() else emptyMap() \ No newline at end of file diff --git a/kategory/src/test/kotlin/kategory/MapsTest.kt b/kategory/src/test/kotlin/kategory/MapsTest.kt new file mode 100644 index 00000000000..d2755e2d7f2 --- /dev/null +++ b/kategory/src/test/kotlin/kategory/MapsTest.kt @@ -0,0 +1,37 @@ +package kategory + +import io.kotlintest.KTestJUnitRunner +import org.junit.Assert +import org.junit.runner.RunWith + +@RunWith(KTestJUnitRunner::class) +class MapsTest : UnitSpec() { + + init { + + "multiple instances with same values of Tuple2 and Pair should give the same map" { + val mapOfTuple = mapOf(Tuple2("one", 1), Tuple2("two", 2), Tuple2("three", 3)) + val mapOfPair = mapOf("one" to 1, "two" to 2, "three" to 3) + Assert.assertEquals(mapOfPair, mapOfTuple) + } + + "multiple instances with same values of Tuple2 and Pair in different order should give the same map" { + val mapOfTuple = mapOf(Tuple2("one", 1), Tuple2("two", 2), Tuple2("three", 3)) + val mapOfPair = mapOf("two" to 2, "three" to 3, "one" to 1) + Assert.assertEquals(mapOfPair, mapOfTuple) + } + + "different instances of Tuple2 and Pair should give a different maps" { + val mapOfTuple = mapOf(Tuple2("one", 1), Tuple2("two", 2), Tuple2("three", 3)) + val mapOfPair = mapOf("four" to 4, "two" to 2, "three" to 3) + Assert.assertNotEquals(mapOfPair, mapOfTuple) + } + + "an instance of Tuple2 should give the same map as the instance of Pair with same value" { + val mapOfTuple = mapOf(Tuple2("one", 1)) + val mapOfPair = mapOf("one" to 1) + Assert.assertEquals(mapOfPair, mapOfTuple) + } + + } +}