! Tests the OPTION command.
!
! Author:  J. Czechowski IV
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  ! Basic tests for the OPT table
  assert/text="^reg.main.name: OPT.PATH contains SYS" ",SYS," SUBS ",^{OPT.PATH},"

  ! Test the /GET switch
  option/get=allOptions
  assert/text="^reg.main.name: OPT/GET=<res> contains SYS" ",SYS," SUBS ",^{allOptions},"

  invoke sep java.io.File.separator
  SEDIT ^{ENV.NMROOT} root GSUBS "^sep" "/" ! This is necessary for Windows
  if ^{env.NMROOT} EQS "classpath:" then
    ! since 3.9.0 switch to check if the jar is nxm-sys vs nxm-sys.jar as the version number and snapshot label could be added to the name of the jar.
    !assert/text="^reg.main.name: OPT.CLASSPATH._CURRENT_ contains NMROOT (NMROOT=classpath: so it is running from the nxm-sys jar file, lets check and see if that is included)" "nxm-sys" SUBS "^{OPT.CLASSPATH._CURRENT_}"
    warn "SKIPPING ^reg.main.name The opt.classpath variable is not being instantiated correctly when run from a jar file and running from jdk 9+"

  else
    assert/text="^reg.main.name: OPT.CLASSPATH._CURRENT_ contains NMROOT" root SUBS "^{OPT.CLASSPATH._CURRENT_}"
  endif
  ! Points to test option trees
  FILE NAME rootOptDir (^{env.nmroot},nxm,sys,test,nmopts) "" ""
  FILE NAME xmOpt2Dir  (^{rootOptDir},xmtestopt) "" ""

  if interAct then
    res inst "All of the option trees should be listed with a <n> prefix before &
              each option tree that is on the path (PATH=^{OPT.PATH})."

    if baseline then
      res inst "^{inst} Should see X-Midas option trees MYXMTESTOPT and XMTESTOPT &
                defined without any warnings in terminal."
    endif

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label  macro   "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" "^inst" 24 12 /title

      shellgui/id=idshellgui/attach

      res allOptInstr "There should be no warnings below for XMTESTOPT since XMTESTOPT not added to the path"

      if baseline then
        ! test BUG 1311
        say ""
        info "There should be NO warnings for defining MYXMTESTOPT under nxm/<optname> path"
        say "nM> OPT MYXMTESTOPT ^rootOptDir"
        OPT MYXMTESTOPT rootOptDir
        ! test BUG 2279
        say ""
        info "There should be NO warnings below for defining XMTESTOPT not under nxm/<optname> path"
        say "nM> OPT XMTESTOPT ^xmOpt2Dir"
        OPT XMTESTOPT xmOpt2Dir
        if ^{env.NMROOT} NEQS "classpath:" then
          res allOptInstr "It is currently OK to see warnings below for XMTESTOPT"
        else
          res allOptInstr "It is currently OK to NOT see warnings below for XMTESTOPT since running from a jar file"
        endif
      endif
      say ""
      info "^{allOptInstr}"
      say "nM> OPT"
      OPT
      say "\n"
      INFO "You SHOULD see an exception below with error text stating 'No files found for [boguspath/nxm/].'"
      opt * "boguspath/nxm/"
      ! Since 3.5.1 Bug_2844 - added for new test in %test_option_create
      say "\n"
      INFO "Check the terminal after this test has completed.  There should not be an \nERROR : ArrayIndexOutOfBoundsException"
    pipe off
  endif

  if baseline then
    %test_option_create

    ! remove "MYXMTESTOPT" and "XMTESTOPT" test option tree definitions
    if interact then
      option/delete myxmtestopt
      option/delete xmtestopt
    endif
  endif
endmacro


procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Tests for OPTION" msg.data eqss "PASS"
    pipe stop
  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return
