#!/usr/bin/env perl
# 
#  Copyright (C) 2011-2012,2016,2023  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

#
#############################################################
# Regression Test Script Template
#############################################################
# Turn off buffering
select(STDOUT); $| = 1;

my $tool = pycxcdm; # Name of module to be tested

#----------------------------------------------------------------------
# Find Test Utilities File
#----------------------------------------------------------------------
use File::Basename;
$cwd = dirname($0); # relative to where this script is run from (cwd).
$tutils = "${cwd}/regtest_utils.pl";
if ( ! -e "$tutils" )
{
   print "ERROR: Test Utilities file not found. '${tutils}' \n";
   exit(1);
}
else
{
   require "$tutils";
}

#----------------------------------------------------------------------
# Define base directories
#----------------------------------------------------------------------
#Get test paths with tool specific extensions
($indir, $outdir, $savdir, $logdir) = &get_test_dirs( $tool );

#Create output directories as needed
&gen_test_dirs( $outdir, $logdir );
  
#----------------------------------------------------------------------
# Define Test Suite.
#----------------------------------------------------------------------
my @testlist = define_test_suite();

#----------------------------------------------------------------------
# Standard Test Run.
#----------------------------------------------------------------------
do_standard_test( $tool, $outdir, $logdir, $savdir, \@testlist );



#----------------------------------------------------------------------
# SUBROUTINES
#----------------------------------------------------------------------
# ===================================================================
# define_test_suite( void )
#   Create TestCase objects defining each test.
#
#   Arguments
#     none
#
#   Globals
#     tool    = Tool being tested
#     indir   = Test input directory
#     outdir  = Test output directory
#
#   Returns
#     tests: array of TestCase objects
# --------------------------------------------------------------------
sub define_test_suite
{
  my @testlist = [];
  my $ntests   = 1;   # how many test cases

  # pre-size the testslist array
  $endndx = $ntests - 1;
  $#testlist = $endndx;  # this sizes the array

  #!! Find test executable - adjust as needed to support command generation.
  my $texecdir = "${cwd}/${tool}";
  my $testexe = "${texecdir}/test.py";
  if (! -e $testexe )
  {
    print "ERROR: Test executable not found. ${testexe}\n";
  }

  # Find system utilities used
  #!! Add any system utilities used in test command strings
  #my $CP = find_in_path("cp", $ENV{'PATH'}, ':');

  my $comstr = ""; # command string

  for $ii (1 .. $ntests) 
  {
    my $testid = sprintf("test%02d", $ii);
    my $t = TestCase->new( id => $testid, mksubdir => 0 );

    #Setup each TestCase.
    #!! Fill in TestCase details for each test
    #Example:
    if ( $testid eq "test01" )
    {
      # Build Test Command
      my $resfile = "cxcdm_results.txt";
      $comstr  = "${testexe} outdir=${outdir} log=True ";
      $comstr .= "checklog=False 1> ${outdir}/${resfile}";
    
      $t->desc("full module test script");  # describe what is being tested
      $t->type("regression");               # flavor "unit" or "regression"
      $t->command( $comstr );               # set command string
      $t->filters(0, "sub <TESTIN>/${tool}  -> <TESTIN>");
      $t->filters(1, "sub <TESTOUT>/${tool} -> <TESTOUT>");
      $t->filters(2, "sub <TESTSAV>/${tool} -> <TESTBASE>");
      $t->filters(3, "sub ^\[[][(]'scol'.*  -> <COLSPEC>");
      $t->filters(4, "sub an integer is required.* -> <EXPECTED EXCEPTION MSG>");                                       #P2/3 Exception message
      $t->filters(5, "sub takes exactly 3 arguments [(]1 given[)] -> <EXPECTED EXCEPTION MSG>");                        #P2.7 Exception message
      $t->filters(6, "sub missing 2 required positional arguments: 'name' and 'dtype' -> <EXPECTED EXCEPTION MSG>");    #P3.5 Exception message
      $t->filters(7, "sub takes exactly 3 arguments [(]2 given[)] -> <EXPECTED EXCEPTION MSG>");                        #P2.7 Exception message
      $t->filters(8, "sub missing 1 required positional argument: 'dtype' -> <EXPECTED EXCEPTION MSG>");                #P3.5 Exception message
      $t->filters(9, "sub takes exactly 2 arguments [(]0 given[)] -> <EXPECTED EXCEPTION MSG>");                        #P2.7 Exception message
      $t->filters(10, "sub missing 2 required positional arguments: 'dd' and 'data' -> <EXPECTED EXCEPTION MSG>");      #P3.5 Exception message
      $t->filters(11, "sub takes exactly 2 arguments [(]1 given[)] -> <EXPECTED EXCEPTION MSG>");                       #P2.7 Exception message
      $t->filters(12, "sub missing 1 required positional argument: 'data' -> <EXPECTED EXCEPTION MSG>");                #P3.5 Exception message
      $t->filters(13, "sub argument 2 must be string, not int -> <EXPECTED EXCEPTION MSG>");                            #P2.7 Exception message
      $t->filters(14, "sub argument 2 must be str, not int -> <EXPECTED EXCEPTION MSG>");                               #P3.5 Exception message
      $t->filters(15, "sub numpy.bytes_ -> numpy.string_");                                                             #P3.5 Type change
      $t->filters(16, "sub b\'a -> \'a");                                                                               #P3.5 Byte string in row data 
      $t->filters(17, "sub b\'\' -> \'\'");                                                                             #P3.5 Byte string in row data 
      $t->filters(18, "sub L, -> ,");                                                                                   #P3.5 Long value in row data
      $t->filters(19, "sub 'dmBlock' object cannot be interpreted as an integer -> <EXPECTED EXCEPTION MSG>");          #P3.10 Exception message
      $t->filters(20, "sub integer argument expected, got float -> <EXPECTED EXCEPTION MSG>");                          #P3.x  Exception message
      $t->filters(21, "sub 'float' object cannot be interpreted as an integer -> <EXPECTED EXCEPTION MSG>");            #P3.10 Exception message
      $t->verify( $resfile, diff);          # validation criteria
      $t->verify( "cxcdm_verbose.txt", diff);# validation criteria
    }

    $ndx = $ii - 1;
    $testlist[$ndx] = $t;
  }

  return(@testlist);
}
