summaryrefslogtreecommitdiffstats
path: root/Scala/example/src/test
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-03-27 23:25:43 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:20 +0100
commitdcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a (patch)
treef5ab0b7e1ab3e2186dd864230419c5dc20404c1f /Scala/example/src/test
parentab61b59311fc1428a8edbe99b103ab77627c7dc8 (diff)
downloadcoursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.zip
coursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.tar.gz
Scala : add scala/example
Diffstat (limited to 'Scala/example/src/test')
-rw-r--r--Scala/example/src/test/scala/example/ListsSuite.scala124
1 files changed, 124 insertions, 0 deletions
diff --git a/Scala/example/src/test/scala/example/ListsSuite.scala b/Scala/example/src/test/scala/example/ListsSuite.scala
new file mode 100644
index 0000000..4a52667
--- /dev/null
+++ b/Scala/example/src/test/scala/example/ListsSuite.scala
@@ -0,0 +1,124 @@
+package example
+
+import org.scalatest.FunSuite
+
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+/**
+ * This class implements a ScalaTest test suite for the methods in object
+ * `Lists` that need to be implemented as part of this assignment. A test
+ * suite is simply a collection of individual tests for some specific
+ * component of a program.
+ *
+ * A test suite is created by defining a class which extends the type
+ * `org.scalatest.FunSuite`. When running ScalaTest, it will automatically
+ * find this class and execute all of its tests.
+ *
+ * Adding the `@RunWith` annotation enables the test suite to be executed
+ * inside eclipse using the built-in JUnit test runner.
+ *
+ * You have two options for running this test suite:
+ *
+ * - Start the sbt console and run the "test" command
+ * - Right-click this file in eclipse and chose "Run As" - "JUnit Test"
+ */
+@RunWith(classOf[JUnitRunner])
+class ListsSuite extends FunSuite {
+
+ /**
+ * Tests are written using the `test` operator which takes two arguments:
+ *
+ * - A description of the test. This description has to be unique, no two
+ * tests can have the same description.
+ * - The test body, a piece of Scala code that implements the test
+ *
+ * The most common way to implement a test body is using the method `assert`
+ * which tests that its argument evaluates to `true`. So one of the simplest
+ * successful tests is the following:
+ */
+ test("one plus one is two")(assert(1 + 1 == 2))
+
+
+ /**
+ * In Scala, it is allowed to pass an argument to a method using the block
+ * syntax, i.e. `{ argument }` instead of parentheses `(argument)`.
+ *
+ * This allows tests to be written in a more readable manner:
+ */
+ test("one plus one is three?") {
+ assert(1 + 1 == 3) // This assertion fails! Go ahead and fix it.
+ }
+
+
+ /**
+ * One problem with the previous (failing) test is that ScalaTest will
+ * only tell you that a test failed, but it will not tell you what was
+ * the reason for the failure. The output looks like this:
+ *
+ * {{{
+ * [info] - one plus one is three? *** FAILED ***
+ * }}}
+ *
+ * This situation can be improved by using a special equality operator
+ * `===` instead of `==` (this is only possible in ScalaTest). So if you
+ * run the next test, ScalaTest will show the following output:
+ *
+ * {{{
+ * [info] - details why one plus one is not three *** FAILED ***
+ * [info] 2 did not equal 3 (ListsSuite.scala:67)
+ * }}}
+ *
+ * We recommend to always use the `===` equality operator when writing tests.
+ */
+ test("details why one plus one is not three") {
+ assert(1 + 1 === 3) // Fix me, please!
+ }
+
+
+ /**
+ * In order to test the exceptional behavior of a methods, ScalaTest offers
+ * the `intercept` operation.
+ *
+ * In the following example, we test the fact that the method `intNotZero`
+ * throws an `IllegalArgumentException` if its argument is `0`.
+ */
+ test("intNotZero throws an exception if its argument is 0") {
+ intercept[IllegalArgumentException] {
+ intNotZero(0)
+ }
+ }
+
+ def intNotZero(x: Int): Int = {
+ if (x == 0) throw new IllegalArgumentException("zero is not allowed")
+ else x
+ }
+
+
+ /**
+ * Now we finally write some tests for the list functions that have to be
+ * implemented for this assignment. We fist import all members of the
+ * `List` object.
+ */
+ import Lists._
+
+
+ /**
+ * We only provide two very basic tests for you. Write more tests to make
+ * sure your `sum` and `max` methods work as expected.
+ *
+ * In particular, write tests for corner cases: negative numbers, zeros,
+ * empty lists, lists with repeated elements, etc.
+ *
+ * It is allowed to have multiple `assert` statements inside one test,
+ * however it is recommended to write an individual `test` statement for
+ * every tested aspect of a method.
+ */
+ test("sum of a few numbers") {
+ assert(sum(List(1,2,0)) === 3)
+ }
+
+ test("max of a few numbers") {
+ assert(max(List(3, 7, 2)) === 7)
+ }
+}