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
Passing position explicitly to test in Scala 3 #2262
Comments
@liontiger23 Hmm, sorry for breaking your use case in Scala 3 build, that was indeed a very creative way of using the implicit position, we did change that intentionally in Scala 3 when we tried to support Scala 3 in its pre-release versions, we were trying to do it the 'Scala 3' way, let's try to discuss with @bvenners and see what he thinks, I'll try to think if there's a workaround for your case first. |
@liontiger23 One workaround may be you can pass the position through the assertion/matchers functions, here's a quick example: import org.scalactic._
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class TestSpec extends AnyFunSuite with Matchers {
/** Attaches position to test arguments (for bulk tests). */
def tp[T](x: T)(implicit pos: source.Position): (T, source.Position) = (x, pos)
for (((x, y, res), pos) <- Seq(
tp(2, 2, 4),
tp(-10, 1, -10) // Failure will be pointing to here.
)) {
test(s"add of $x and $y") {
given source.Position = pos
(x + y) shouldBe res
}
}
} Hope this helps. Cheers. |
Thanks for the suggestion! The error does point to a correct location, but unfortunately IDEA does not resolve this link correctly.
With workaround:
Probably the I have tried a similar workaround abusing
For some reason this one is resolved correctly by IDEA, so
Although these workarounds do help in some capacity, we lose here the actual line, where the error occurred, because workaround effectively replaces that position with the test case one. If there were multiple assertions/matchers in |
Hello, I am having a similar problem with other ScalaTest methods / keywords. The suggested For a trivial example, I'm trying to add an import org.scalactic.Prettifier
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.{Assertion, Assertions}
import org.scalactic.source.Position
import org.scalactic.CanEqual
class UnitSpec extends AnyFunSpec with MyMatchers {
// my tests extend this class
}
trait MyMatchers { this: Assertions =>
def assertEquals[L, R](
actual: L,
expected: R
)(
using prettifier: Prettifier,
pos: Position,
caneq: CanEqual[L, R]
): Assertion = {
assertResult(expected = expected)(actual = actual)
}
} The provided Adding This worked in Scala 2, but it seems impossible to achieve in Scala 3. The new inline def assertResult method calls into a private method, so I can't bypass it by calling the implementation directly. There are a bunch of other methods in Assertions.scala that use the same Is there really no way to pass my |
The Scala 3 version of
test
no longer has implicitsource.Position
parameter. See for examplescalatest/jvm/funsuite/src/main/scala/org/scalatest/funsuite/AnyFunSuiteLike.scala
Lines 150 to 160 in 6bf2e09
In our team, we have used this feature to grab position of each test case data and pass it to a
test
later, so that when it is run in IDEA, double-clicking failed test case will lead to the test case data, from which given test was generated.Here is an example:
Having separate test for each test case is handy, since they will be run separately and in the resulting log we can see which test cases failed. So having each separate test associated with position of corresponding test case is very convenient for such situation.
If this change in API of
test
in Scala 3 version is not intentional, maybe it can be restored in the later versions?Or maybe there is a better way to achieve a similar workflow without abusing implicit position. Any suggestions are welcome.
The text was updated successfully, but these errors were encountered: