Skip to content

Instantly share code, notes, and snippets.

@blast-hardcheese
Last active August 9, 2021 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blast-hardcheese/e6790efa23c3b2ffbd14e5650f6a4875 to your computer and use it in GitHub Desktop.
Save blast-hardcheese/e6790efa23c3b2ffbd14e5650f6a4875 to your computer and use it in GitHub Desktop.
package bug.example
import org.scalatest.funsuite.AnyFunSuite
class SetSuite extends AnyFunSuite {
test("An empty Set should have size 0") {
// Option.empty[String].contains(1234) // WARNS
assert(Set.empty.size == 0)
}
test("Invoking head on an empty Set should produce NoSuchElementException") {
assertThrows[NoSuchElementException] {
// Option.empty[String].contains(1234) // WARNS
Set.empty.head
}
}
}
import org.scalatest.flatspec.AnyFlatSpec
class SetFlatSpec extends AnyFlatSpec {
"An empty Set" should "have size 0" in {
Option.empty[String].contains(1234)
assert(Set.empty.size == 0)
}
it should "produce NoSuchElementException when head is invoked" in {
assertThrows[NoSuchElementException] {
Option.empty[String].contains(1234)
Set.empty.head
}
}
}
import org.scalatest.funspec.AnyFunSpec
class SetFunSpec extends AnyFunSpec {
describe("A Set") {
describe("when empty") {
it("should have size 0") {
// Option.empty[String].contains(1234) // WARNS
assert(Set.empty.size == 0)
}
it("should produce NoSuchElementException when head is invoked") {
assertThrows[NoSuchElementException] {
// Option.empty[String].contains(1234) // WARNS
Set.empty.head
}
}
}
}
}
import org.scalatest.wordspec.AnyWordSpec
class SetWordSpec extends AnyWordSpec {
"A Set" when {
"empty" should {
"have size 0" in {
Option.empty[String].contains(1234)
assert(Set.empty.size == 0)
}
"produce NoSuchElementException when head is invoked" in {
assertThrows[NoSuchElementException] {
Option.empty[String].contains(1234)
Set.empty.head
}
}
}
}
}
import org.scalatest.freespec.AnyFreeSpec
class SetFreeSpec extends AnyFreeSpec {
"A Set" - {
"when empty" - {
"should have size 0" in {
Option.empty[String].contains(1234)
assert(Set.empty.size == 0)
}
"should produce NoSuchElementException when head is invoked" in {
assertThrows[NoSuchElementException] {
Option.empty[String].contains(1234)
Set.empty.head
}
}
}
}
}
import org.scalatest._
import org.scalatest.propspec._
import matchers._
import prop._
import scala.collection.immutable._
class SetPropSpec extends AnyPropSpec with TableDrivenPropertyChecks with should.Matchers {
val examples =
Table[AbstractSet[Int]](
"set",
BitSet.empty,
HashSet.empty[Int],
TreeSet.empty[Int]
)
property("an empty Set should have size 0") {
forAll(examples) { set =>
// Option.empty[String].contains(1234) // WARNS
set.size should be (0)
}
}
property("invoking head on an empty set should produce NoSuchElementException") {
forAll(examples) { set =>
a [NoSuchElementException] should be thrownBy {
Option.empty[String].contains(1234)
set.head
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment