Skip to content

Commit

Permalink
Merge branch 'feature-wordspec-err-msg-fix' of https://github.com/che…
Browse files Browse the repository at this point in the history
…eseng/scalatest into 3.2.x-new
  • Loading branch information
cheeseng committed Jul 20, 2022
2 parents d83d083 + 414c967 commit 67c15f8
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,33 @@ class AsyncWordSpecLikeSpec extends AnyFunSpec {
assert(reporter.testSucceededEventsReceived.length == 3)
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends AsyncWordSpecLike {

// SKIP-SCALATESTJS,NATIVE-START
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
// SKIP-SCALATESTJS,NATIVE-END
// SCALATESTJS-ONLY override implicit val executionContext = scala.scalajs.concurrent.JSExecutionContext.runNow

import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in {
succeed
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("AsyncWordSpecLikeSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 10)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,33 @@ class AsyncWordSpecSpec extends AnyFunSpec {
assert(reporter.testSucceededEventsReceived.length == 3)
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends AsyncWordSpec {

// SKIP-SCALATESTJS,NATIVE-START
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
// SKIP-SCALATESTJS,NATIVE-END
// SCALATESTJS-ONLY override implicit val executionContext = scala.scalajs.concurrent.JSExecutionContext.runNow

import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in {
succeed
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("AsyncWordSpecSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 10)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,36 @@ class FixtureAsyncWordSpecLikeSpec extends scalatest.funspec.AnyFunSpec {
assert(reporter.testSucceededEventsReceived.length == 3)
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends wordspec.FixtureAsyncWordSpecLike {

// SKIP-SCALATESTJS,NATIVE-START
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
// SKIP-SCALATESTJS,NATIVE-END
// SCALATESTJS-ONLY override implicit val executionContext = scala.scalajs.concurrent.JSExecutionContext.runNow

type FixtureParam = String
def withFixture(test: OneArgAsyncTest): FutureOutcome = { test("hi") }

import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in { fixture =>
succeed
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("FixtureAsyncWordSpecLikeSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 10)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,36 @@ class FixtureAsyncWordSpecSpec extends scalatest.funspec.AnyFunSpec {
assert(reporter.testSucceededEventsReceived.length == 3)
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends wordspec.FixtureAsyncWordSpec {

// SKIP-SCALATESTJS,NATIVE-START
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
// SKIP-SCALATESTJS,NATIVE-END
// SCALATESTJS-ONLY override implicit val executionContext = scala.scalajs.concurrent.JSExecutionContext.runNow

type FixtureParam = String
def withFixture(test: OneArgAsyncTest): FutureOutcome = { test("hi") }

import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in { fixture =>
succeed
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("FixtureAsyncWordSpecSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 10)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4496,5 +4496,30 @@ class FixtureWordSpecSpec extends scalatest.funspec.AnyFunSpec {
assert(cause.getMessage == FailureMessages.duplicateTestName(prettifier, UnquotedString("a feature can test 1")))
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends wordspec.FixtureAnyWordSpec {
type FixtureParam = String
def withFixture(test: OneArgTest): Outcome = { test("hi") }
"a feature" can {
//DOTTY-ONLY ()
}
import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in { fixture =>
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("FixtureWordSpecSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 9)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4053,5 +4053,25 @@ class WordSpecSpec extends AnyFunSpec with GivenWhenThen {
assert(cause.getMessage == FailureMessages.duplicateTestName(prettifier, UnquotedString("a feature can test 1")))
}

it("should throw DuplicateTestNameException when duplicate test name is detected inside in a forAll") {
class TestSpec extends AnyWordSpec {
import org.scalatest.prop.TableDrivenPropertyChecks._
"trying something" should {
val scenarios = Table("value", "first", "first")

forAll(scenarios) { value =>
s"work as expected for $value " in {
}
}
}
}
val e = intercept[DuplicateTestNameException] {
new TestSpec
}
assert("WordSpecSpec.scala" == e.failedCodeFileName.get)
assert(e.failedCodeLineNumber.get == thisLineNumber - 9)
assert(e.message == Some(FailureMessages.duplicateTestName(prettifier, UnquotedString("trying something should work as expected for first"))))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ trait AnyWordSpecLike extends TestSuite with TestRegistration with ShouldVerb wi
case "can" => FailureMessages.exceptionWasThrownInCanClause(Prettifier.default, className, description, errorMessage)
}

private def rethrowIfCauseIsNAEOrDTNE(e: StackDepthException, pos: source.Position): Unit =
e.cause match {
case Some(c) if c.isInstanceOf[NotAllowedException] || c.isInstanceOf[DuplicateTestNameException] =>
throw c
case _ =>
throw new NotAllowedException(
FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause,
Some(e),
e.position.getOrElse(pos)
)
}

private def registerBranch(description: String, childPrefix: Option[String], verb: String, methodName:String, stackDepth: Int, adjustment: Int, pos: source.Position, fun: () => Unit): Unit = {

def registrationClosedMessageFun: String =
Expand All @@ -206,8 +218,8 @@ trait AnyWordSpecLike extends TestSuite with TestRegistration with ShouldVerb wi
registerNestedBranch(description, childPrefix, fun(), registrationClosedMessageFun, "AnyWordSpecLike.scala", methodName, stackDepth, adjustment, None, Some(pos))
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(verb, UnquotedString(e.getClass.getName), description, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down Expand Up @@ -240,8 +252,8 @@ trait AnyWordSpecLike extends TestSuite with TestRegistration with ShouldVerb wi
registerNestedBranch(descriptionText, childPrefix, fun(), registrationClosedMessageFun, "AnyWordSpecLike.scala", methodName, stackDepth, adjustment, None, Some(pos))
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(methodName, UnquotedString(e.getClass.getName), descriptionText, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ trait AsyncWordSpecLike extends AsyncTestSuite with AsyncTestRegistration with S
case "can" => FailureMessages.exceptionWasThrownInCanClause(Prettifier.default, className, description, errorMessage)
}

private def rethrowIfCauseIsNAEOrDTNE(e: StackDepthException, pos: source.Position): Unit =
e.cause match {
case Some(c) if c.isInstanceOf[NotAllowedException] || c.isInstanceOf[DuplicateTestNameException] =>
throw c
case _ =>
throw new NotAllowedException(
FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause,
Some(e),
e.position.getOrElse(pos)
)
}

private def registerBranch(description: String, childPrefix: Option[String], verb: String, pos: source.Position, fun: () => Unit): Unit = {
def registrationClosedMessageFun: String =
verb match {
Expand All @@ -211,8 +223,8 @@ trait AsyncWordSpecLike extends AsyncTestSuite with AsyncTestRegistration with S
registerNestedBranch(description, childPrefix, fun(), registrationClosedMessageFun, None, pos)
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(verb, UnquotedString(e.getClass.getName), description, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down Expand Up @@ -245,8 +257,8 @@ trait AsyncWordSpecLike extends AsyncTestSuite with AsyncTestRegistration with S
registerNestedBranch(descriptionText, childPrefix, fun(), registrationClosedMessageFun, None, pos)
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(methodName, UnquotedString(e.getClass.getName), descriptionText, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ trait FixtureAnyWordSpecLike extends org.scalatest.FixtureTestSuite with org.sca
case "can" => FailureMessages.exceptionWasThrownInCanClause(Prettifier.default, className, description, errorMessage)
}

private def rethrowIfCauseIsNAEOrDTNE(e: StackDepthException, pos: source.Position): Unit =
e.cause match {
case Some(c) if c.isInstanceOf[NotAllowedException] || c.isInstanceOf[DuplicateTestNameException] =>
throw c
case _ =>
throw new NotAllowedException(
FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause,
Some(e),
e.position.getOrElse(pos)
)
}

private def registerBranch(description: String, childPrefix: Option[String], verb: String, methodName: String, stackDepth: Int, adjustment: Int, pos: source.Position, fun: () => Unit): Unit = {

def registrationClosedMessageFun: String =
Expand All @@ -226,8 +238,8 @@ trait FixtureAnyWordSpecLike extends org.scalatest.FixtureTestSuite with org.sca
registerNestedBranch(description, childPrefix, fun(), registrationClosedMessageFun, sourceFileName, methodName, stackDepth, adjustment, None, Some(pos))
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(verb, UnquotedString(e.getClass.getName), description, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down Expand Up @@ -260,8 +272,8 @@ trait FixtureAnyWordSpecLike extends org.scalatest.FixtureTestSuite with org.sca
registerNestedBranch(descriptionText, childPrefix, fun(), registrationClosedMessageFun, "WordSpecLike.scala", methodName, stackDepth, adjustment, None, Some(pos))
}
catch {
case e: TestFailedException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestCanceledException => throw new NotAllowedException(FailureMessages.assertionShouldBePutInsideItOrTheyClauseNotShouldMustWhenThatWhichOrCanClause, Some(e), e.position.getOrElse(pos))
case e: TestFailedException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case e: TestCanceledException => rethrowIfCauseIsNAEOrDTNE(e, pos)
case nae: NotAllowedException => throw nae
case trce: TestRegistrationClosedException => throw trce
case e: DuplicateTestNameException => throw new NotAllowedException(exceptionWasThrownInClauseMessageFun(methodName, UnquotedString(e.getClass.getName), descriptionText, e.getMessage), Some(e), e.position.getOrElse(pos))
Expand Down
Loading

0 comments on commit 67c15f8

Please sign in to comment.