Last active
March 13, 2018 08:12
-
-
Save MarcinusX/213f141bc6b1ae26b7673f2f35a4dbcb to your computer and use it in GitHub Desktop.
Tests from college scala test presentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Presentation: https://goo.gl/GhYvKy | |
// Dependency: libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test" | |
// | |
import org.scalatest.{FunSpec, FunSuite} | |
object Calculator { | |
def sum(a: Int, b: Int): Int = { | |
a + b | |
} | |
def divide(a: Int, b: Int): Int = { | |
a / b | |
} | |
} | |
/** | |
* Created by Marcin Szalek on 13.03.18. | |
*/ | |
class CalculatorTest extends FunSuite { | |
test("Calculator should sum 2 ints") { | |
var result = Calculator.sum(5, 7); | |
assert(result == 12) | |
} | |
test("Division wokrs") { | |
val res = Calculator.divide(10, 5) | |
assert(res == 2) | |
} | |
test("Division by 0") { | |
assertThrows[ArithmeticException] { | |
Calculator.divide(10, 0) | |
} | |
} | |
} | |
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 { | |
assertThrows[NoSuchElementException] { | |
Set.empty.head | |
} | |
} | |
} | |
class SetSpec2 extends FunSpec { | |
describe("A Set") { | |
describe("when empty") { | |
var set: Set[Int] = Set.empty | |
it("should have size 0") { | |
assert(set.size == 0) | |
} | |
it("should produce NoSuchElementException when head is invoked") { | |
assertThrows[NoSuchElementException] { | |
set.head | |
} | |
} | |
} | |
describe("when not empty") { | |
var set: Set[Int] = Set.empty | |
set += 5 | |
it("should have size 1") { | |
assert(set.size == 1) | |
} | |
} | |
} | |
} | |
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment