From 2885060d44bfc38061f25bad13007abb250dbae8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 19 Feb 2013 17:15:57 -0600 Subject: actually, stop using tap4j tap is simple enough that it doesn't buy us a whole lot, and java style libraries are painful to work with because java is awful, and it doesn't even provide a streaming interface, which is a pretty useful feature for tap producers --- build.sbt | 2 - src/main/scala/testbuilder.scala | 72 +++++++++++++++++++++++------------- src/main/scala/testbuilder/tap.scala | 19 ++++++++++ src/test/scala/basic.scala | 69 +++++++++++++++++----------------- 4 files changed, 99 insertions(+), 63 deletions(-) create mode 100644 src/main/scala/testbuilder/tap.scala diff --git a/build.sbt b/build.sbt index 5150ced..7448a33 100644 --- a/build.sbt +++ b/build.sbt @@ -4,8 +4,6 @@ version := "0.01" scalaVersion := "2.10.0" -libraryDependencies += "org.tap4j" % "tap4j" % "4.0" - libraryDependencies += "org.scalatest" % "scalatest_2.10" % "1.9.1" % "test" scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") diff --git a/src/main/scala/testbuilder.scala b/src/main/scala/testbuilder.scala index 428d9ff..151343f 100644 --- a/src/main/scala/testbuilder.scala +++ b/src/main/scala/testbuilder.scala @@ -1,50 +1,72 @@ package testbuilder -import org.tap4j.model._ -import org.tap4j.producer._ -import org.tap4j.util._ +import java.io.OutputStream -class Builder (plan: Option[Int] = None) { - def this (plan: Int) = - this(Some(plan)) +class Builder (plan: Option[Int], out: OutputStream) { + plan.foreach(p => { + Console.withOut(out) { + println(tap.plan(p)) + } + }) + + def this (plan: Int, out: OutputStream = System.out) = + this(Some(plan), out) + + def this (out: OutputStream = System.out) = + this(None, out) def ok (test: Boolean, description: String) { ok(test, Some(description)) } def ok (test: Boolean, description: Option[String] = None) { - val status = if (test) StatusValues.OK else StatusValues.NOT_OK - val result = new TestResult(status, currentTest) - description.foreach(d => result.setDescription("- " + d)) - testSet.addTestResult(result) - currentTest += 1 + val line = tap.result(test, state.currentTest, description) + state.ok(test) + Console.withOut(out) { + println(line) + } } def diag (message: String) { - testSet.addComment(new Comment(message)) + val line = tap.comment(message) + Console.withOut(out) { + println(line) + } } - def tap: String = { - finalizeTestSet - producer.dump(testSet) + def doneTesting () { + if (noPlan) { + Console.withOut(out) { + println(tap.plan(state.currentTest - 1)) + } + } } def isPassing: Boolean = - currentTest > 1 && !testSet.containsNotOk + state.isPassing - private val producer = TapProducerFactory.makeTap13Producer - - private val testSet = new TestSet - plan.foreach(p => testSet.setPlan(new Plan(p))) - - private var currentTest = 1 + private val state = new TestState private def noPlan = plan.isEmpty - private def finalizeTestSet { - if (noPlan) { - testSet.setPlan(new Plan(currentTest - 1)) + private class TestState { + private var passCount = 0 + private var failCount = 0 + + def ok (cond: Boolean) { + if (cond) { + passCount += 1 + } + else { + failCount += 1 + } } + + def currentTest: Int = + failCount + passCount + 1 + + def isPassing: Boolean = + currentTest > 1 && failCount == 0 } } diff --git a/src/main/scala/testbuilder/tap.scala b/src/main/scala/testbuilder/tap.scala new file mode 100644 index 0000000..bd65c61 --- /dev/null +++ b/src/main/scala/testbuilder/tap.scala @@ -0,0 +1,19 @@ +package testbuilder + +object tap { + def result (cond: Boolean, num: Int, desc: String): String = + result(cond, num, Some(desc)) + + def result (cond: Boolean, num: Int, desc: Option[String] = None): String = + Seq( + if (cond) Some("ok") else Some("not ok"), + Some(num), + desc + ).flatMap(x => x).mkString(" ") + + def comment (message: String): String = + "# " + message + + def plan (num: Int) = + "1.." + num +} diff --git a/src/test/scala/basic.scala b/src/test/scala/basic.scala index 7371a2a..88c8f6d 100644 --- a/src/test/scala/basic.scala +++ b/src/test/scala/basic.scala @@ -2,81 +2,78 @@ import org.scalatest.FunSuite import testbuilder._ +import java.io.ByteArrayOutputStream + class Basic extends FunSuite { test ("ok") { - val builder = new Builder(4) + val output = new ByteArrayOutputStream + val builder = new Builder(4, output) builder.ok(true, "test succeeded") builder.ok(false, "test failed") builder.ok(true) builder.ok(false) + builder.doneTesting val expected = - "1..4\n" + - "ok 1 - test succeeded\n" + - "not ok 2 - test failed\n" + - "ok 3\n" + + "1..4\n" + + "ok 1 test succeeded\n" + + "not ok 2 test failed\n" + + "ok 3\n" + "not ok 4\n" - assert(builder.tap === expected) - - builder.ok(true) - assert(builder.tap === expected + "ok 5\n") + assert(output.toString === expected) } test ("no plan") { - val builder = new Builder + val output = new ByteArrayOutputStream + val builder = new Builder(output) builder.ok(true, "test succeeded") builder.ok(false, "test failed") builder.ok(true) builder.ok(false) + builder.doneTesting val expected = - "1..4\n" + - "ok 1 - test succeeded\n" + - "not ok 2 - test failed\n" + - "ok 3\n" + - "not ok 4\n" + "ok 1 test succeeded\n" + + "not ok 2 test failed\n" + + "ok 3\n" + + "not ok 4\n" + + "1..4\n" - assert(builder.tap === expected) - - val expectedModified = - "1..5\n" + - "ok 1 - test succeeded\n" + - "not ok 2 - test failed\n" + - "ok 3\n" + - "not ok 4\n" + - "ok 5\n" - - builder.ok(true) - assert(builder.tap === expectedModified) + assert(output.toString === expected) } test ("empty") { - val builder = new Builder + val output = new ByteArrayOutputStream + val builder = new Builder(output) + builder.doneTesting - assert(builder.tap === "1..0\n") + assert(output.toString === "1..0\n") } test ("diag") { - val builder = new Builder + val output = new ByteArrayOutputStream + val builder = new Builder(output) builder.ok(true, "the test passes") builder.ok(false, "the test passes") builder.diag("got false, expected true") builder.ok(true) + builder.doneTesting val expected = - "1..3\n" + - "ok 1 - the test passes\n" + - "not ok 2 - the test passes\n" + + "ok 1 the test passes\n" + + "not ok 2 the test passes\n" + "# got false, expected true\n" + - "ok 3\n" + "ok 3\n" + + "1..3\n" - assert(builder.tap === expected) + assert(output.toString === expected) } test ("is passing") { - val builder = new Builder + val output = new ByteArrayOutputStream + val builder = new Builder(output) assert(!builder.isPassing) builder.ok(true) -- cgit v1.2.3-54-g00ecf