Created
March 22, 2021 16:19
-
-
Save davegurnell/2c5ee6eb7a9739f5f7c4118d0a2ab9e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package basics | |
import org.scalatest.matchers.should.Matchers | |
import org.scalatest.wordspec.AnyWordSpec | |
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | |
import org.scalacheck.Gen | |
import org.scalacheck.Arbitrary | |
class MathSpec extends AnyWordSpec with Matchers with ScalaCheckDrivenPropertyChecks { | |
def absolute(n: Int): Int = | |
if (n < 0) -n else n | |
"absolute" should { | |
"turn a negative into a positive" in { | |
val negatives = Gen.negNum[Int] | |
forAll(negatives) { (n: Int) => | |
absolute(n) should be > 0 | |
} | |
} | |
"leave a positive as-is" in { | |
val positives = Gen.negNum[Int] | |
forAll(positives) { (n: Int) => | |
absolute(n) should be(n) | |
} | |
} | |
} | |
def isEven(num: Int): Boolean = | |
num % 2 == 0 | |
"isEven" should { | |
"recognise an even number" in { | |
val numbers = Arbitrary.arbitrary[Int] | |
forAll(numbers) { (n: Int) => | |
isEven(2 * n) should be(true) | |
} | |
} | |
"recognise an odd number" in { | |
val numbers = Arbitrary.arbitrary[Int] | |
forAll(numbers) { (n: Int) => | |
isEven(2 * n - 1) should be(false) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment