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

WordSpec Error Message Enhancement #2132

Merged
merged 1 commit into from Jul 20, 2022
Merged
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
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"))))
}

}

}
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"))))
}

}

}
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"))))
}

}

}
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"))))
}

}

}
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"))))
}

}
}
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"))))
}

}
}
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
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
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