Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Forked from kings13y/FizzBuzzSpecs2.scala
Created May 29, 2012 01:16
Show Gist options
  • Save etorreborre/2822018 to your computer and use it in GitHub Desktop.
Save etorreborre/2822018 to your computer and use it in GitHub Desktop.
FizzBuzz test case in Specs2
package org.scalabound.scatdd.specs2
import org.scalabound.scatdd.FizzBuzz
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
// package unitTest { |> Scala IDE will not pick the class up as runnable if I sub-package it like this :-(
@RunWith(classOf[JUnitRunner])
class FizzBuzzJUnitSpec extends org.specs2.mutable.Specification {
"Multiples of both three and five" should {
"print 'FizzBuzz'" in { FizzBuzz.eval(15) must_== "FizzBuzz" }
}
"Multiples of three only" should {
"print 'Fizz'" in { FizzBuzz.eval(12) must_== "Fizz" }
}
"Multiples of five only" should {
"print 'Buzz'" in { FizzBuzz.eval(10) must_== "Buzz" }
}
"Non multiples of five or three" should {
"print the number back" in { FizzBuzz.eval(11) must_== "11" }
}
}
// package acceptanceTest { |> Scala IDE will not pick the class up as runnable if I sub-package it like this :-(
@RunWith(classOf[JUnitRunner])
class FizzBuzzUATSpec extends org.specs2.Specification { def is =
"This specification is to check the FizzBuzz evaluator" ^
p^
"The FizzBuzz evaluator should" ^
"process a multiple of both three and five to return FizzBuzz" ! e1^
"process a multiple of three only to return Fizz" ! e2^
"process a multiple of five only to return Buzz" ! e3^
"process a non multiple of three or five to return the input" ! e4^
end
def e1 = FizzBuzz.eval(15) must_== "FizzBuzz"
def e2 = FizzBuzz.eval(12) must_== "Fizz"
def e3 = FizzBuzz.eval(10) must_== "Buzz"
def e4 = FizzBuzz.eval(11) must_== "11"
}
@RunWith(classOf[JUnitRunner])
class FizzBuzzDataTableSpec extends org.specs2.Specification with org.specs2.matcher.DataTables { def is =
"Fizz Buzz testing with DataTables" ! e1
// note: when the first column of a DataTable is a String, '!!' needs to be used instead of '!'
// see: http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#DataTables
def e1 =
"spec name" || "input val" | "expected output" |
"Multiple of both three and five" !! 15 ! "FizzBuzz" |
"Multiple of three only" !! 12 ! "Fizz" |
"Multiple of five only" !! 10 ! "Buzz" |
"Non multiple of five or three" !! 11 ! "11" |> {
(a, b, c) => FizzBuzz.eval(b) must_== c // (a, b, c) is a structural match on the table
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment