package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.Shell;

/** Running NeXtMidas no-graphics mode
    Empty Unittest intrinisic since Unittest is now in nxm.sys.test.junit
    @author J. Czechowski IV / Neon Ngo
*/
public class Unittest extends Intrinsic {
  /** ("INFO: "|"WARN: "|etc.) + ("..PASS"|"..FAIL"|etc.) */
  public static final int OUTPUT_OVERHEAD_LENGTH = 6 + 6;
  /** This is the total length of an output message minus overhead (info|warn) and (pass|fail). */
  public static final int OUTPUT_LENGTH = (80 - OUTPUT_OVERHEAD_LENGTH); // total - <overhead length>

  /** This is the reference to the original Unittest class after it has been moved under test.junit */
  private static Class<?> movedUnittest = null;

  static {
    try {
      movedUnittest = Class.forName("nxm.sys.test.junit.Unittest");
    }
    catch (Exception ex) {
      // do nothing
    }
  }

  /** default constructor */
  public Unittest () {}

  public int open() {
    String toTest = MA.getQ("OPT","SYS").trim();
    boolean showError = MA.getState("/ERROR",true);
    boolean showFailure = MA.getState("/FAILURE",true);
    boolean showTrace = MA.getState("/ST",false);
    String results = MA.getS("RES").trim();

    if (movedUnittest != null) {
      String errorFlagStr = "/ERROR=" + showError + "/FAILURE=" + showFailure + "/ST=" + showTrace;
      String unittestCmd = "nxm.sys.test.junit.Unittest" + errorFlagStr + " OPT="+toTest;
      if ((results != null) && (results != "")) unittestCmd += " RES=" + results;

      Shell.runCommand(M, unittestCmd);
    }
    else {
      M.warning("UNITTEST command is not available ....");
    }

    return NORMAL;
  }
}
