Skip to content

Commit 7d17e8e

Browse files
authored
Merge pull request #35 from bcchapman/default-map-values
Adding additional test case to Std Lib Maps
2 parents 833496b + bfdac1c commit 7d17e8e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/main/scala/stdlib/Maps.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ object Maps extends FlatSpec with Matchers with exercise.Section {
7878
myMap("IA") should be(res1)
7979
}
8080

81+
/** If a map key is requested using myMap(missingKey) which does not exist a NoSuchElementException will be thrown.
82+
* Default values may be provided using either getOrElse or withDefaultValue for the entire map
83+
*/
84+
def defaultValuesMayBeProvidedMaps(res0: String, res1: String) {
85+
val myMap = Map("MI" "Michigan", "OH" "Ohio", "WI" "Wisconsin", "IA" "Iowa")
86+
intercept[NoSuchElementException] {
87+
myMap("TX")
88+
}
89+
myMap.getOrElse("TX", "missing data") should be(res0)
90+
91+
val myMap2 = Map("MI" "Michigan", "OH" "Ohio", "WI" "Wisconsin", "IA" "Iowa") withDefaultValue "missing data"
92+
myMap2("TX") should be(res1)
93+
}
94+
8195
/** Map elements can be removed easily:
8296
*/
8397
def easilyRemovedMaps(res0: Boolean, res1: Boolean) {

src/test/scala/exercises/stdlib/MapSpec.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ class MapsSpec extends Spec with Checkers {
7272
)
7373
}
7474

75+
def `map default value access` = {
76+
check(
77+
Test.testSuccess(
78+
Maps.defaultValuesMayBeProvidedMaps _,
79+
"missing data" :: "missing data" :: HNil
80+
)
81+
)
82+
}
83+
7584
def `map element removal` = {
7685
check(
7786
Test.testSuccess(

0 commit comments

Comments
 (0)