diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-02-21 12:15:02 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-02-21 15:02:01 -0600 |
commit | 22a7e25c0bca5f08d9d8c9df8793238750d091a3 (patch) | |
tree | a388251d6982a99df870bc5a0462b0393ebe4896 /src/main/scala/org/perl8/test/tap | |
parent | 98621ad7b07f076da65c7614227db4c8e33f6dde (diff) | |
download | scala-test-more-22a7e25c0bca5f08d9d8c9df8793238750d091a3.tar.gz scala-test-more-22a7e25c0bca5f08d9d8c9df8793238750d091a3.zip |
move more things around
Diffstat (limited to 'src/main/scala/org/perl8/test/tap')
-rw-r--r-- | src/main/scala/org/perl8/test/tap/Producer.scala | 44 | ||||
-rw-r--r-- | src/main/scala/org/perl8/test/tap/TestBuilder.scala | 126 |
2 files changed, 170 insertions, 0 deletions
diff --git a/src/main/scala/org/perl8/test/tap/Producer.scala b/src/main/scala/org/perl8/test/tap/Producer.scala new file mode 100644 index 0000000..28c9142 --- /dev/null +++ b/src/main/scala/org/perl8/test/tap/Producer.scala @@ -0,0 +1,44 @@ +package org.perl8.test.tap + +import org.perl8.test.Utils._ + +object Producer { + def result ( + cond: Boolean, + num: Int, + desc: Message = NoMessage, + todo: Message = NoMessage + ): String = + join( + (if (!cond) Some("not") else None), + Some("ok"), + Some(num), + desc, + todo.map(t => "# TODO " + t) + ) + + def skip (num: Int, reason: Message = NoMessage): String = + join( + Some("ok"), + Some(num), + Some("# skip"), + reason + ) + + def comment (message: String): String = + message.split("\n").map(m => "# " + m).mkString("\n") + + def plan (plan: Plan): String = + join( + Some("1.." + plan.plan), + (if (plan.skipAll || plan.message.isDefined) Some("#") else None), + (if (plan.skipAll) Some("SKIP") else None), + plan.message + ) + + def bailOut (message: Message = NoMessage) = + join(Some("Bail out!"), message) + + private def join (strings: Option[Any]*): String = + strings.flatMap(x => x).mkString(" ") +} diff --git a/src/main/scala/org/perl8/test/tap/TestBuilder.scala b/src/main/scala/org/perl8/test/tap/TestBuilder.scala new file mode 100644 index 0000000..f9c684c --- /dev/null +++ b/src/main/scala/org/perl8/test/tap/TestBuilder.scala @@ -0,0 +1,126 @@ +package org.perl8.test.tap + +import java.io.OutputStream + +import org.perl8.test.Utils._ + +class TestBuilder ( + plan: Option[Plan], + out: OutputStream, + val indent: Int, + private val name: Message +) { + plan.foreach(p => println(Producer.plan(p))) + + def this ( + plan: Plan, + out: OutputStream = System.out, + indent: Int = 0, + name: Message = NoMessage + ) = + this(Some(plan), out, indent, name) + + def this ( + out: OutputStream = System.out, + indent: Int = 0, + name: Message = NoMessage + ) = + this(None, out, indent, name) + + def ok ( + test: Boolean, + description: Message = NoMessage, + todo: Message = NoMessage + ) { + val line = Producer.result(test, state.currentTest, description, todo) + state.ok(test || todo.isDefined) + println(line) + } + + def skip (reason: Message = NoMessage) { + val line = Producer.skip(state.currentTest, reason) + state.ok(true) + println(line) + } + + def diag (message: Message) { + message.foreach(m => println(Producer.comment(m))) + } + + def bailOut (message: Message = NoMessage) { + println(Producer.bailOut(message)) + throw new BailOutException(message.getOrElse("")) + } + + def doneTesting () { + plan match { + case None => println(Producer.plan(state.currentTest - 1)) + case _ => () + } + + if (!isPassing) { + if (!state.matchesPlan) { + val planCount = (plan match { + case Some(p) => p.plan + case None => state.currentTest - 1 + }) + val planned = planCount + " test" + (if (planCount > 1) "s" else "") + val ran = state.currentTest - 1 + diag("Looks like you planned " + planned + " but ran " + ran + ".") + } + + if (state.currentTest == 1) { + diag("No tests run!") + } + + if (state.failCount > 0) { + val count = state.failCount + val fails = count + " test" + (if (count > 1) "s" else "") + val total = + state.currentTest - 1 + (if (state.matchesPlan) "" else " run") + diag("Looks like you failed " + fails + " of " + total + ".") + } + } + } + + def isPassing: Boolean = + state.isPassing + + def failedTests: Int = + state.failCount + + private val state = new TestState + + private def println (str: String) { + Console.withOut(out) { + val indented = + str.split("\n").map(s => (" " * (indent * 4)) + s).mkString("\n") + Console.println(indented) + } + } + + private class TestState { + var passCount = 0 + var failCount = 0 + + def ok (cond: Boolean) { + if (cond) { + passCount += 1 + } + else { + failCount += 1 + } + } + + def currentTest: Int = + failCount + passCount + 1 + + def matchesPlan: Boolean = plan match { + case Some(p) => p.plan == failCount + passCount + case None => true + } + + def isPassing: Boolean = + currentTest > 1 && failCount == 0 && matchesPlan + } +} |