package edu.marlboro.cs.prisoner; /** *

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

*

* And it's so trivial that I've added it directly to this package * rather than keep it in its own namespace. *

*
 * 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());
 * 
* * @author Jim Mahoney * @version $Id: TrivialTestEngine.java 11927 2007-03-15 15:49:41Z mahoney $ */ public class TrivialTestEngine { private int successfulTestCount; private int failedTestCount; private int testCount; private String reportMessage; public TrivialTestEngine(){ successfulTestCount = failedTestCount = testCount = 0; reportMessage = ""; } public void ok(boolean test, String message){ testCount++; String okText; if (test){ successfulTestCount++; okText = "ok "; } else { failedTestCount++; okText = "not ok "; } okText += Integer.toString(testCount); reportMessage += String.format("%-16s",okText) + message + "\n"; } /** * Just in case you want to run tests without messages. * (Though that's not recommended.) */ public void ok(boolean test){ ok(test, ""); } public String getReport(){ String result = "1.." + testCount + "\n" + reportMessage; if (failedTestCount == 0 && successfulTestCount == testCount){ result += "All tests successful.\n"; } else { String plural = testCount == 1 ? "" : "s"; result += "# Failed " + failedTestCount + " and passed " + successfulTestCount + " out of " + testCount + " test" + plural + ".\n"; } return result; } }