// @(#)$Id: Expect.scala,v 1.1 2005/10/28 09:04:50 leavens Exp leavens $
package testing;

import scala.testing.SUnit;
import SUnit._; 

/** Test cases for simple expected results.
 * @author Gary T. Leavens
 */
class Expect[T](val code : String, value : => T, expected : => T)
     extends TestCase(code)
{
  /** The arrow that separates the code from the actual result. */   
  val arrow = "       ==> ";
  /** What is printed before the correct answer when something goes wrong. */
  val wrong = " EXPECTED: ";
  
  /** Method to run the code to produce the value,
   *  and to announce the result. */
  def evalAnnounce(code: String, value: => T): T = {
    Console.println(code);
    Console.print(arrow);
    Console.flush;
    var finished = false;
    var res: T = _;
    try {
      res = value;
      Console.print(res);
      finished = true;
    } finally {
      if (!finished) Console.print("Error!");
      Console.println;
    }
    return res;
  }

  /** Are v and e different? */
  def isUnexpected(v: T, e: T): Boolean = { v != e }

  // doc comment inherited    
  override def runTest(): Unit = {
    val v = evalAnnounce(code, value);
    if (isUnexpected(v, expected)) {
      Console.print(wrong);
      Console.println(expected);
      fail(code);
    }
  }
}
