/*
** Module   : ReadCursorPos.java
** Abstract : directive to read the cursor position (row/col) into variables
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090126 Directive to read the cursor position (row/col) into variables.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 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
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import com.goldencode.harness.*;
import com.goldencode.harness.terminal.*;

/**
 * Directive to read the cursor position (row/col)  into variables.
 */
public class ReadCursorPos
extends Report
implements TerminalTestable
{
   /** Short name for the activity. */
   private static final String NAME = "READ-CURSOR-POS";
   
   /** The row variable in which to store the row index. */
   private final Variable row;
   
   /** The column variable in which to store the column index. */
   private final Variable col;
   
   /**
    * Constructor with either or both row and column target variables. At least
    * one of the variables must be specified, but the other can have checking
    * disabled by passing <code>null</code> for the value.
    *
    * @param    row
    *           The row variable in which to store the row index. Use
    *           <code>null</code> to disable reading of the row, but in that
    *           case the column value must be specified.
    * @param    col
    *           The column variable in which to store the column index. Use
    *           <code>null</code> to disable reading of the column, but in that
    *           case the row value must be specified.
    */
   public ReadCursorPos(Variable row, Variable col)
   {
      super(NAME, "");
      this.row = row;
      this.col = col;
   }
   
   /**
    * Execute the directive of the test using the given terminal as the data
    * sink and/or source for the operation.
    *
    * @param    term
    *           The terminal that represents the system under test.
    */
   @Override
   public void execute(Terminal term)
   {
      if (row == null && col == null)
      {
         setStatus(Status.FAILED,
                   "No variables specified for either row or column.");
         return;
      }
         
      StringBuilder sb = new StringBuilder();
      
      if (row != null)
      {
         int actualRow = term.getCursorRow();
         
         row.setValue(actualRow);
         sb.append(String.format("row = %d;", actualRow));
      }
      
      if (col != null)
      {
         if (sb.length() > 0)
            sb.append(' ');
         
         int actualCol = term.getCursorColumn();
         
         col.setValue(actualCol);
         sb.append(String.format("col = %d;", actualCol));
      }
      
      setDescription(sb.toString());
      setStatus(Status.PASSED, "");
   }
}
