Skip to content

Commit

Permalink
Added new AboutLists koan, fixed AboutSequences
Browse files Browse the repository at this point in the history
  • Loading branch information
dickwall committed Mar 5, 2010
1 parent 8530517 commit 6865d10
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
target
36 changes: 17 additions & 19 deletions src/test/scala/org/functionalkoans/forscala/AboutEmptyValues.scala
Expand Up @@ -8,61 +8,59 @@ import org.scalatest.matchers.ShouldMatchers
class AboutEmptyValues extends KoanSuite with ShouldMatchers {

test ("None equals None") {
assert(None == None )
assert(None == __ )
}

test ("None should be identical to None") {
val a = None
assert(a eq None ) // note that eq denotes identity, and == denotes equality in Scala
assert(a eq __ ) // note that eq denotes identity, and == denotes equality in Scala
}

test ("None can be converted to a String") {
assert(None.toString == "None" )
assert(None.toString == __ )
}

test ("An empty list can be represented by another value: Nil") {
assert(List() == Nil )
test ("An empty list can be represented by another nothing value: Nil") {
assert(List() == __ )
}

test ("None can be converted to an empty list") {
val a = None
assert(a.toList == Nil )
assert(a.toList == __ )
}

test ("Nil ")

test ("None is considered empty") {
assert(None.isEmpty == true )
assert(None.isEmpty == __ )
}

test ("None can be cast Any, AnyRef or AnyVal") {
assert(None.asInstanceOf[Any] == None)
assert(None.asInstanceOf[AnyRef] == None)
assert(None.asInstanceOf[AnyVal] == None)
assert(None.asInstanceOf[Any] == __)
assert(None.asInstanceOf[AnyRef] == __)
assert(None.asInstanceOf[AnyVal] == __)
}

test ("None cannot be cast to all types of objects") {
intercept[ClassCastException] {
intercept[___] {
assert(None.asInstanceOf[String] == None)
}
}

test ("None can be used with Option instead of null references") {
var optional : Option[String] = None
assert(optional.isEmpty == true)
assert(optional == None)
assert(optional.isEmpty == __)
assert(optional == __)
}

test ("Some is the opposite of None for Option types") {
var optional : Option[String] = Some("Some Value")
assert((optional == None) == false, "Some(value) should not equal None")
assert(optional.isEmpty == false, "Some(value) should not be empty")
assert((optional == None) == __, "Some(value) should not equal None")
assert(optional.isEmpty == __, "Some(value) should not be empty")
}

test ("Option.getOrElse can be used to provide a default in the case of None") {
var optional : Option[String] = Some("Some Value")
var optional2 : Option[String] = None
assert(optional.getOrElse("No Value") == "Some Value", "Should return the value in the option")
assert(optional2.getOrElse("No Value") == "No Value", "Should return the specified default value")
assert(optional.getOrElse("No Value") == __, "Should return the value in the option")
assert(optional2.getOrElse("No Value") == __, "Should return the specified default value")
}
}
89 changes: 89 additions & 0 deletions src/test/scala/org/functionalkoans/forscala/AboutLists.scala
@@ -0,0 +1,89 @@
package org.functionalkoans.forscala

import support.KoanSuite
import support.BlankValues._
import org.scalatest.matchers.ShouldMatchers

class AboutLists extends KoanSuite with ShouldMatchers {

test ("Nil lists are identical, even of different types") {
val a : List[String] = Nil
val b : List[Int] = Nil

(a == Nil) should be (true)
(a eq Nil) should be (true)

(b == Nil) should be (true)
(b eq Nil) should be (true)

(a == b) should be (true)
(a eq b) should be (true)
}

test ("Lists are easily created") {
val a = List(1,2,3)
a should equal (List(1,2,3))
}

test ("Lists can be accessed via head and tail") {
val a = List(1,2,3)
a.head should equal (1)
a.tail should equal (List(2,3))
}

test ("Lists can accessed at random") {
val a = List(1,3,5,7,9)
a(0) should equal (1)
a(2) should equal (5)
a(4) should equal (9)

intercept[NoSuchElementException] {
println(a(5))
}
}

test ("Lists are immutable") {
val a = List(1,3,5,7,9)
val b = a.remove(v => v == 5) // remove where value is 5

a should equal (List(1,3,5,7,9))
b should equal (List(1,3,7,9))
}

test ("Lists have many useful methods") {
val a = List(1,3,5,7,9)
a.length should equal (5)
a.reverse should equal (List(9,7,5,3,1))
a.toString should equal ("List(1, 3, 5, 7, 9)")
a.map { v => v * 2} should equal (List(2,6,10,14,18))
a.filter { v => v % 3 == 0 } should equal (List(3,9))
}

test ("Functions over lists can use _ as shorthand") {
val a = List(1,2,3)
a.map { _ * 2 } should equal (List(2,4,6))
a.filter { _ % 2 == 0} should equal (List(2))
}

test ("Functions over lists can use () instead of {}") {
val a = List(1,2,3)
a.map ( _ * 2 ) should equal (List(2,4,6))
a.filter ( _ % 2 != 0 ) should equal (List(1,3))
}

test ("Lists can be 'reduced' with a mathematical operation") {
val a = List(1,3,5,7)
// note the two _s below indicate the first and second args respectively
a.reduceLeft( _ + _ ) should equal (16)
a.reduceLeft( _ * _ ) should equal (105)
}

test ("Foldleft is like reduce, but with an explicit starting value") {
val a = List(1,3,5,7)
// foldLeft uses a form called currying that we will explore later
a.foldLeft(0)( _ + _ ) should equal (16)
a.foldLeft(10)( _ + _ ) should equal (26)
a.foldLeft(1)( _ * _ ) should equal (105)
a.foldLeft(0)( _ * _ ) should equal (0)
}
}
Expand Up @@ -6,7 +6,9 @@ import org.scalatest.matchers.ShouldMatchers

class AboutSequences extends KoanSuite with ShouldMatchers {

koan("An empty array") {
val empty = Array.empty
koan("A list can be converted to an array") {
val l = List(1,2,3)
val a = l.toArray
a should equal (Array(1,2,3))
}
}
Expand Up @@ -5,5 +5,5 @@ import org.junit.runner.RunWith

@RunWith(classOf[JUnitRunner])
class PathToEnlightenmentTest extends Suite {
override def nestedSuites = List(/*new AboutAsserts, new AboutEmptyValues,*/ new AboutSequences)
override def nestedSuites = List(/*new AboutAsserts, new AboutEmptyValues,*/ new AboutLists)
}

0 comments on commit 6865d10

Please sign in to comment.