Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method checkpoint to trait Checkpoints for easy use of Checkpoint #2305

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions jvm/core/src/main/scala/org/scalatest/Checkpoints.scala
Expand Up @@ -147,10 +147,6 @@ trait Checkpoints {
* failed checkpoints.
*/
def reportAll()(implicit pos: source.Position): Unit = {
// SKIP-SCALATESTJS,NATIVE-START
val stackDepth = 1
// SKIP-SCALATESTJS,NATIVE-END
//SCALATESTJS,NATIVE-ONLY val stackDepth = 10
if (!failures.isEmpty) {
val failMessages =
for (failure <- failures.asScala)
Expand All @@ -159,6 +155,27 @@ trait Checkpoints {
}
}
}


/**
* Run provided function with a new <code>Checkpoint</code> and report the <code>Checkpoint</code>.
* You can group your assertions, like this:
*
*
* <pre>
* checkpoint { cp =>
* cp { x should be < 0 }
* cp { y should be > 9 }
* }
* </pre>
*
* @param f the block of code, likely containing one or more checkpointed assertions, to execute
*/
def checkpoint(f: Checkpoint => Unit)(implicit pos: source.Position): Unit = {
val cp = new Checkpoint
f(cp)
cp.reportAll()
}
}

/**
Expand Down
Expand Up @@ -20,16 +20,10 @@ import org.scalatest.OptionValues._

// SKIP-SCALATESTJS,NATIVE-START
import org.scalatestplus.junit.AssertionsForJUnit
import org.scalatestplus.junit.JUnitTestFailedError
// SKIP-SCALATESTJS,NATIVE-END
import org.scalatest.SharedHelpers.thisLineNumber
import org.scalatest.exceptions.TestFailedException
import org.scalatest.exceptions.TestCanceledException
import org.scalatest.exceptions.TestRegistrationClosedException
import org.scalatest.exceptions.NotAllowedException
import org.scalatest.exceptions.DuplicateTestNameException

import org.scalactic.source
import org.scalatest.SharedHelpers.thisLineNumber
import org.scalatest.exceptions._
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers._

Expand Down Expand Up @@ -96,8 +90,10 @@ class CheckpointsSpec extends AnyFunSpec with AssertionsForJUnit {
describe("with a success condition") {
it("should not throw an exception") {
val cp = new Checkpoint
cp { 1 should equal (1) }
cp.reportAll()
noException should be thrownBy {
cp { 1 should equal (1) }
cp.reportAll()
}
}
}
describe("when a TestCanceledException is thrown") {
Expand Down Expand Up @@ -134,7 +130,71 @@ class CheckpointsSpec extends AnyFunSpec with AssertionsForJUnit {
}
}

// SKIP-SCALATESTJS,NATIVE-START
describe("The checkpoint construct") {
describe("with a failure condition") {

it("should throw a TestFailedException when reportAll is called") {
val caught = the [TestFailedException] thrownBy {
checkpoint { cp =>
cp { 1 should equal (2) }
}
}

val failConditionLineNumber = thisLineNumber - 4
// For Scala 3 the position is at the end of the application of the checkpoint function.
val reportAllLineNumber = if (ScalaTestVersions.BuiltForScalaVersion.startsWith("3.")) failConditionLineNumber + 1 else failConditionLineNumber - 1

caught.failedCodeLineNumber.value should equal (reportAllLineNumber)
caught.failedCodeFileName.value should be ("CheckpointsSpec.scala")
caught.getMessage should include (Resources.atCheckpointAt +
" CheckpointsSpec.scala:" +
failConditionLineNumber)
}
}

describe("with multiple failure conditions") {

it("should report all failures when reportAll is called") {
val caught =
the [TestFailedException] thrownBy {
checkpoint { cp =>
cp { 1 should equal (2) }
cp { 3 should equal (2) }
}
}

val failCondition1LineNumber = thisLineNumber - 5
val failCondition2LineNumber = failCondition1LineNumber + 1
// For Scala 3 the position is at the end of the application of the checkpoint function.
val reportAllLineNumber = if (ScalaTestVersions.BuiltForScalaVersion.startsWith("3.")) failCondition1LineNumber + 2 else failCondition1LineNumber - 1

caught.failedCodeLineNumber.value should equal (reportAllLineNumber)
caught.failedCodeFileName.value should be ("CheckpointsSpec.scala")

caught.getMessage should include (Resources.atCheckpointAt +
" CheckpointsSpec.scala:" +
failCondition1LineNumber)

caught.getMessage should include (Resources.atCheckpointAt +
" CheckpointsSpec.scala:" +
failCondition2LineNumber)
}
}


describe("with a success condition") {

it("should not throw an exception") {
noException should be thrownBy {
checkpoint { cp =>
cp { 1 should equal (1) }
}
}
}
}
}

// SKIP-SCALATESTJS,NATIVE-START
describe("a Checkpoint using AssertionsForJUnit") {
describe("with a failure condition") {

Expand Down