package testprogs;

import java.util.*;
import java.io.*;
import org.gregorie.runtest.*;

/**
 * <p><b>CustomTest</b> exercises the code in org.gregorie.runtest.RunTest
 * and org.gregorie.runtest.Custom.</p>
 * <pre>
 *             Written by M.C.Gregorie.
 *
 *             Version history
 *             
 *             1.0   06/11/11   Initial release
 *             2.1   26/07/20   environ package dependencies removed
 * </pre>
 * <PRE>
 * <b>Licensing</b>
 * 
 * Java support classes
 * Copyright (C) 2020  Martin C Gregorie
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Lesser GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 * 
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * </PRE>
 * <p>Contact Martin Gregorie at
 * <a href="mailto:martin@gregorie.org">martin@gregorie.org</a>.<br>
 * Read the <a href="http://www.gnu.org/licenses/lgpl.html">Lesser GNU
 * General Public License</a>.</p>
 */

public class CustomTest extends org.gregorie.runtest.Custom
{
   private static final String COPYRIGHT = "(c) Martin Gregorie, 2020"; 
   private static final String PROGNAME  = "CustomTest"; 
   private static final String VERSION   = "2.1";
 
   private RunTest      rt = null;

   /**
    * Accept and store a reference to the RunTest instance.
    */ 
   public void link(RunTest rt)
   {
      this.rt = rt;
   }
   
   /**
    * <p>This callAdapter() tests the RunTest methods that report
    * test results and errors.</p>
    *
    * @param fn Name of the function to be tested.
    * @param p  List of function arguments as Strings
    * @param n  List of function arguments as integers
    */ 
   public void callAdapter(String fn,
                           ArrayList<String> p,
                           ArrayList<Integer> n)
   {      
      if (fn.equals("boolean"))
      {
         boolean b = (p.get(0).equals("true") ? true : false);
         rt.setResult(b);
      }
      else
      if (fn.equals("char"))
         rt.setResult(defaultToSpace(p, 0));
      else
      if (fn.equals("double"))
      {
         double d = strToDouble(p.get(0));
         rt.setResult(d);
      }
      else
      if (fn.equals("hex"))
      {
         StringBuilder s = new StringBuilder(p.get(0));
         s.append('\n');
         s.append(p.get(1));
         rt.setResultWithHex(s.toString());
      }
      else
      if (fn.equals("int"))
         rt.setResult(n.get(0));
      else
      if (fn.equals("string"))
         rt.setResult(trimQuotes(collectArguments(p)));
      else
      if (fn.equals("wait"))
      {
         rt.setNote("Waited " + n.get(0) + " seconds");
         rt.wait(n.get(0));
         rt.setResult("Wait complete");
      }
      else
         rt.setError("Unknown action");
   }

   /**
    * This method carries out test-specific cleanup.
    * If it is not needed by the target set of tests, remove the
    * trace statement.
    */
   public void finishTest()
   {
      rt.trace(1, "finishTest() not implemented");
   }
         
   /**
    * This method carries out test-specific initialisation
    * If it is not needed by the target set of tests, remove the
    * trace statement.
    */
   public void initialiseTest()
   {
      rt.trace(1, "initialiseTest() not implemented");
   }
         
   /**
    * This method is run when the '-?' option is present.
    * It should be overridden by class-specific text in your extension.
    */
   public void showHelp()
   {
      rt.trace("\n" + PROGNAME + " - " + VERSION + " " + COPYRIGHT + "\n");
      rt.trace("Syntax  : java testprogs/RunTest " + PROGNAME +
               " [<opts>] script...");
      rt.trace("Function: " + PROGNAME + " provides basic tests for");
      rt.trace("          RunTest and its linkage to a subclass of");
      rt.trace("          Custom.");
   }
}
