import org.scalatest.FlatSpec class SetSpec extends FlatSpec { "An empty Set" should "have size 0" in { assert(Set.empty.size == 0) } it should "produce NoSuchElementException when head is invoked" in { intercept[NoSuchElementException] { Set.empty.head } } }
import org.scalatest._ class TVSet { private var on: Boolean = false def isOn: Boolean = on def pressPowerButton() { on = !on } } class TVSetSpec extends FeatureSpec with GivenWhenThen { info("As a TV set owner") info("I want to be able to turn the TV on and off") info("So I can watch TV when I want") info("And save energy when I'm not watching TV") feature("TV power button") { scenario("User presses power button when TV is off") { Given("a TV set that is switched off") val tv = new TVSet assert(!tv.isOn) When("the power button is pressed") tv.pressPowerButton() Then("the TV should switch on") assert(tv.isOn) } scenario("User presses power button when TV is on") { Given("a TV set that is switched on") val tv = new TVSet tv.pressPowerButton() assert(tv.isOn) When("the power button is pressed") tv.pressPowerButton() Then("the TV should switch off") assert(!tv.isOn) } } }
import org.scalatest.FunSuite class SetSuite extends FunSuite { test("An empty Set should have size 0") { assert(Set.empty.size == 0) } test("Invoking head on an empty Set should produce NoSuchElementException") { intercept[NoSuchElementException] { Set.empty.head } } }
import org.scalatest.FunSpec class SetSpec extends FunSpec { describe("A Set") { describe("when empty") { it("should have size 0") { assert(Set.empty.size == 0) } it("should produce NoSuchElementException when head is invoked") { intercept[NoSuchElementException] { Set.empty.head } } } } }
import org.scalatest.WordSpec class SetSpec extends WordSpec { "A Set" when { "empty" should { "have size 0" in { assert(Set.empty.size == 0) } "produce NoSuchElementException when head is invoked" in { intercept[NoSuchElementException] { Set.empty.head } } } } }
import collection.mutable.Stack import org.scalatest._ class StackSpec extends FlatSpec { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) assert(stack.pop() == 2) assert(stack.pop() == 1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[String] intercept[NoSuchElementException] { emptyStack.pop() } } }
val a = 5 val b = 2 assertResult(2) { a - b }
val s = "hi" intercept[IndexOutOfBoundsException] { s.charAt(-1) }
intercept[IndexOutOfBoundsException](s charAt -1).getMessage should be == "..."
4 must equal(4) "foo" must equal("foo") List("foo", "bar", "baz") must equal(List("foo", "bar", "baz")) (1, "foo") must equal((1, "foo"))
"foo" must have length (3) List(1, 2, 3, 4) must have length (4) Array('A', 'B') must have length (2) List(1, 2, 3, 4) must have size (4) Array('A', 'B') must have size (2)
"foobarbaz" must startWith("foo") "foobarbaz" must endWith("baz") "foobarbaz" must include("bar") "foobarbaz" must startWith regex ("f[o]+") "foobarbaz" must endWith regex ("[ba]{2}z") "foobarbaz" must include regex ("foo[\\w]{3}baz") "foobarbaz" must fullyMatch regex ("\\w{1}oobarbaz")
7 must be < (8) 7 must be > (0) 6 must be <= (7) 7 must be >= (6) 13.43 must equal(13.43) 13.43 must be(13.4 plusOrMinus 0.4)
List[Int]() must be('empty) // list.isEmpty new File("/tmp") must be('directory) // file.isDirectory
List(1, 2, 3, 4) must contain(2) Array("foo", "bar", "baz") must contain("bar") var users = Map(1 -> "Joe", 2 -> "Lisa", 3 -> "Dr. Evil") users must contain key (2) users must contain value ("Dr. Evil")
case class Recipe(name: String, ingredients: List[String], bakingMinutes: Int) val cookieRecipe = Recipe("cookieRecipe", List("Flour", "Sugar", "Eggs", "Love"), 30) cookieRecipe must have( 'name("cookieRecipe"), 'ingredients(List("Flour", "Sugar", "Eggs", "Love")), 'bakingMinutes(30) )
users must (have size (3) and contain key (3)) users must (contain value ("Mr. X") or contain value ("Joe"))
Source: https://habr.com/ru/post/209578/
All Articles