/** *

* TrivialTestEngine is a trivial testing framework (duh) * that implements an "ok" method similar to perl's. *

 * Sample usage: 
 *   testEngine = new TrivialTestEngine();
 *   testEngine.ok(2 == 1 + 1, "two is one plus one");
 *   testEngine.ok(3 > 2, "three is greater than two");
 *   testEngine.ok(3 < 2, "three is less than two");
 *   System.out.println(testEngine.getReport());
 * 
* * @version 1.0, Feb 7 2007 * @author Jim Mahoney */ public class TrivialTestEngine { private int successfulTestCount; private int failedTestCount; private int testCount; private String reportMessage; public TrivialTestEngine(){ this.successfulTestCount = this.failedTestCount = this.testCount = 0; this.reportMessage = ""; } public void ok(boolean test, String message){ this.testCount++; String okText; if (test){ this.successfulTestCount++; okText = "ok "; } else { this.failedTestCount++; okText = "not ok "; } okText += Integer.toString(this.testCount); this.reportMessage += String.format("%-16s",okText) + message + "\n"; } public void ok(boolean test){ ok(test, ""); } public String getReport(){ String result = "1.." + this.testCount + "\n" + this.reportMessage; if (this.failedTestCount == 0 && this.successfulTestCount == this.testCount){ result += "All tests successful.\n"; } else { String plural = this.testCount == 1 ? "" : "s"; result += "# Failed " + this.failedTestCount + " and passed " + this.successfulTestCount + " out of " + this.testCount + " test" + plural + ".\n"; } return result; } }