aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-02-23 14:56:12 -0600
committerJesse Luehrs <doy@tozt.net>2013-02-23 14:56:12 -0600
commit6d4f400455f40561c853393249cda97f832ce61f (patch)
treef7933ff6e2fbf9d5bde9e00544e75702c70e1d64 /src
parent987ecfb806b140afd955a58d1834bade02c0ca2c (diff)
downloadscala-test-more-6d4f400455f40561c853393249cda97f832ce61f.tar.gz
scala-test-more-6d4f400455f40561c853393249cda97f832ce61f.zip
allow extensions to get the correct stack trace
better implementation ideas here would be nice
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/org/perl8/test/TestMore.scala27
-rw-r--r--src/test/scala/org/perl8/test/ExtensionTest.scala55
2 files changed, 79 insertions, 3 deletions
diff --git a/src/main/scala/org/perl8/test/TestMore.scala b/src/main/scala/org/perl8/test/TestMore.scala
index fa3b6d4..487b023 100644
--- a/src/main/scala/org/perl8/test/TestMore.scala
+++ b/src/main/scala/org/perl8/test/TestMore.scala
@@ -10,6 +10,7 @@ class TestMore (plan: Option[Plan] = None) extends Test with DelayedInit {
this(Some(plan))
def delayedInit (body: => Unit) {
+ level = 0
todo = NoMessage
builder = new TestBuilder(plan, "", NoMessage)
testBody = () => body
@@ -133,9 +134,16 @@ class TestMore (plan: Option[Plan] = None) extends Test with DelayedInit {
}
private def failed (desc: Message) {
- val caller = Thread.currentThread.getStackTrace.drop(1).find(frame => {
- frame.getFileName != "TestMore.scala"
- })
+ val stack = Thread.currentThread.getStackTrace.drop(1)
+ def findIdx (level: Int, start: Int): Int = {
+ val idx = stack.indexWhere({ frame =>
+ frame.getFileName != "TestMore.scala"
+ }, start)
+
+ if (level == 0) { idx } else { findIdx(level - 1, idx + 1) }
+ }
+ val idx = findIdx(level, 0)
+ val caller = stack.drop(idx).headOption
val (file, line) = caller match {
case Some(frame) => (frame.getFileName, frame.getLineNumber)
case None => ("<unknown file>", "<unknown line>")
@@ -151,6 +159,19 @@ class TestMore (plan: Option[Plan] = None) extends Test with DelayedInit {
builder.diag(message + trace)
}
+ def withLevel[T] (newLevel: Int)(body: => T): T = {
+ val oldLevel = level
+ try {
+ // XXX "+4" is something of a hack, not sure how stable it will be
+ level += newLevel + 4
+ body
+ }
+ finally {
+ level = oldLevel
+ }
+ }
+
+ private var level: Int = _
private var todo: Message = _
private var builder: TestBuilder = _
private var testBody: () => Unit = _
diff --git a/src/test/scala/org/perl8/test/ExtensionTest.scala b/src/test/scala/org/perl8/test/ExtensionTest.scala
new file mode 100644
index 0000000..e616d3e
--- /dev/null
+++ b/src/test/scala/org/perl8/test/ExtensionTest.scala
@@ -0,0 +1,55 @@
+package org.perl8.test
+
+import java.io.ByteArrayOutputStream
+
+trait NumberZero { this: TestMore =>
+ def is_zero (i: Int, desc: String): Boolean = {
+ withLevel(1) {
+ is(i, 0, desc)
+ }
+ }
+}
+
+trait NumberZeroWrapped extends NumberZero { this: TestMore =>
+ def isZero (i: Int): Boolean = {
+ withLevel(1) {
+ is_zero(i, "the number is zero")
+ }
+ }
+}
+
+class ExtensionTest extends TestMore {
+ private class ExtensionTestTest extends TestMore with NumberZeroWrapped {
+ is_zero(0, "it's zero")
+ is_zero(1, "it's not zero")
+ isZero(0)
+ isZero(1)
+ }
+
+ val out = new ByteArrayOutputStream
+ val exitCode = Console.withOut(out) {
+ Console.withErr(out) {
+ (new ExtensionTestTest).run
+ }
+ }
+
+ is(exitCode, 2)
+
+ val tap =
+ "ok 1 - it's zero\n" +
+ "not ok 2 - it's not zero\n" +
+ "# Failed test 'it's not zero'\n" +
+ "# at ExtensionTest.scala line 24.\n" +
+ "# got: '1'\n" +
+ "# expected: '0'\n" +
+ "ok 3 - the number is zero\n" +
+ "not ok 4 - the number is zero\n" +
+ "# Failed test 'the number is zero'\n" +
+ "# at ExtensionTest.scala line 26.\n" +
+ "# got: '1'\n" +
+ "# expected: '0'\n" +
+ "1..4\n" +
+ "# Looks like you failed 2 tests of 4.\n"
+
+ is(out.toString, tap)
+}