!******************************************************************************!
! Tests passing NXM & XM style args to a Macro Command
!
! Note: BBB should have the space separated expected results for the
!       other args passed in in table form
!
! @issue:   NXM-4063 Macros not handling variable number of arguments
! @author:  Jeanette McCall
! @since:   NeXtMidas 4.1.4
!******************************************************************************!
startmacro

  ! Skip tests to run in a thinned NeXtMidas, since OPTION command is needed - 4.0.0
  invoke optionExists reg.shell.M.dictionary.isDictionaryEntry(OPTION)

  if optionExists then
    if ^{env.NMROOT} EQS "classpath:" then
      !take a look at line 289 in BaseFile.java -     return io.exists() && !io.isContainer(); 
      !Both of those checks will use the java.io.file api to see if the file does exist. Always returns false when inside a jar
      warn "SKIPPING ^reg.main.name This is a java.io.file issue where it will check that the directory/file exists but does not work with JAR files. TODO - do we need to implement a way to do these tests from within a jar?"
      stop
    endif
    ! save current path
    res save_path opt.path

    ! set the temporary option tree for testing
    call initial_config
    ! Add the new mytestopt1 path
    path add mytestopt1

    INFO "^reg.main.name testing with args: AAA=Jay FFF=Jeff EEE=Jeremy BBB=ExpectedTable"
    testmacroargs AAA=Jay FFF=Jeff EEE=Jeremy BBB="{AAA=Jay,CCC=GHI,DDD=D4,EEE=Jeremy,FFF=Jeff}"

    INFO "^reg.main.name testing with args: Jay ExpectedTable Chris Denver Jeremy Jeff"
    testmacroargs Jay "{AAA=Jay,CCC=Chris,DDD=Denver,EEE=Jeremy,FFF=Jeff}" Chris Denver Jeremy Jeff

    INFO "^reg.main.name testing with args: EEE=Jeremy BBB=ExpectedTable"
    testmacroargs EEE=Jeremy BBB="{AAA=ABC,CCC=GHI,DDD=D4,EEE=Jeremy,FFF=F6}"

    INFO "^reg.main.name testing with args: AAA=Jay BBB=ExpectedTable CCC=Chris Denver Jeremy Jeff"
    testmacroargs AAA=Jay BBB="{AAA=Jay,CCC=Chris,DDD=Denver,EEE=Jeremy,FFF=Jeff}" CCC=Chris Denver Jeremy Jeff

    call test_macro_call Jay "{AA=Jay,CC=Chris,DD=Denver,EE=Jeremy,FF=Jeff}" Chris Denver Jeremy Jeff

    ! cleanup and return to normal opt and path
    call final_cleanup
    
    ! reset path to original
    path set ^save_path

  else
    info "^reg.main.name: running in mode without OPTION command - no tests run"
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

! *****************************************************
! Setup necessary directories, files, and option trees.
! *****************************************************
procedure initial_config
  FILE NAME rootOptDir (^{env.nmroot},nxm,sys,test,nmopts) "" ""
  ! Add new option trees
  option mytestopt1  ^rootOptDir
return

! *****************************************************
! Cleanup directories, files and option trees.
! *****************************************************
procedure final_cleanup
  ! Remove test option trees from PATH.
  path rem mytestopt1
  ! Remove option tree references from results table.
  option/delete MYTESTOPT1
return

! *****************************************************
! Tests calling a procedure within a macro with args
! *****************************************************
procedure test_macro_call cs:AA cs:BB cs:CC cs:DD cs:EE cs:FF

  table actTable create {AA="^AA",CC="^CC",DD="^DD",EE="^EE",FF="^FF"}
  table expTable create "^BB"

  invoke match nxm.sys.test.TestUtil.tablesEqual(expTable,actTable)
  assert/text="^reg.main.name: Macro Procedure Args Passed in correctly" match isTrue
  if match isFalse
    res actTable
  endif

return
