-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from npatarino/add_mapof_method_for_tuple2
Add mapOf method for Tuple2
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} |