!******************************************************************************!
! Test case description/purpose: test macro for include a block of code 
!                                from a text file into the current macro
!
! Arguments: None
!
! Note: This macro is testing the following conditions:
!       include <file>
!       include <file>;<option>
!       include <file> <area> <option>
!       include "<file>"
!       include "<file>;<option>"
!       include "<file> <area> <option>"
!     
!       This macro is using a sub macro for testing for: include <file>;<option>
!       The include <file>;<opt> is available since NextMidas 3.7.1
! 
! @bug  NXM-2942 INCLUDE statement should find files from other option trees
! @author:  Juan J Velasquez
!******************************************************************************!
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
    
    ! calling sub macro for testing for format: <file>;<option>
    %test_includesub
    
    ! cleanup and return to normal opt and path
    call final_cleanup
    
    ! reset path to original
    path set ^save_path
    
    ! test local option tree with include <file> with quotes
    include "include_data"
    assert/text="result included" im_included REXISTS
    
    ! test local option tree without quotes
    include include_data
    assert/text="result included" im_included REXISTS
    
    ! test local option tree with <area> <option> separated by spaces
    include include_data test SYS
    assert/text="result included" im_included REXISTS
    
    ! test local option tree with <area> <option> with quotes
    include "include_data test SYS"
    assert/text="result included" im_included REXISTS
  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
