Skip to content

Commit

Permalink
Merge pull request #135 from npatarino/add_mapof_method_for_tuple2
Browse files Browse the repository at this point in the history
Add mapOf method for Tuple2
  • Loading branch information
raulraja authored Jul 21, 2017
2 parents aaf4192 + 824b052 commit bd5f070
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kategory/src/main/kotlin/kategory/Maps.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kategory

fun <K, V> mapOf(vararg tuple: Tuple2<K, V>): Map<K, V> =
if (tuple.isNotEmpty()) tuple.map { it.a to it.b }.toMap() else emptyMap()
37 changes: 37 additions & 0 deletions kategory/src/test/kotlin/kategory/MapsTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}

}
}

0 comments on commit bd5f070

Please sign in to comment.