startmacro
  ! Special tests for KeyObject.
  !
  switch INTERACTIVE interAct GET DEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE

  ! Only change aux.dat when dealing the specialty case of dat being at nxm/sys/test/dat
  ! Setting aux.dat when run in nextopt does not behave as expected - NXM-3809 since NeXtMidas 4.1.1
  call setDatDir
  if datDirSlash eqs "nxm/sys/test/dat" then
    res origDatAux "^{aux.dat}"
    invoke nm_root nxm.sys.lib.Shell.getNmRoot()
    res aux.dat "^{nm_root}^{datDirSlash}"
  endif

  call testGetArrayValues
  call testTableValues
  call testSetKeys

  ! This tests a strange special case that was missing for fields.(see bug 553)
  assert/text="test this.lines(this.nextindex) instance of Args" this.lines(this.nextindex) TYPEOF nxm.sys.lib.Args
  new nxm.sys.lib.Time() time
  assert/text="test time.dim(10) = 32" time.dim(10) eq 304

  if interAct then
    pipe init
      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" &
                     "The PLOT should have 'LABEL 2' highlighted in bright red. The SHELL should include &
                      a *single* stack trace for a 'Sample RuntimeException' (you may need to scroll up &
                      in the shell)." 24 12 /title

      ! TEST reg.idplot.layers.coneflags5000.select - this fails if 'select'
      ! is treated as an abbreviation rather than a method name.
      plot/id=idplot world.prm|coneflags5000.prm
      set reg.idplot.layers.coneflags5000.select 1

      set l:nn reg.idplot.layers.coneflags5000.select
      assert/text="test get reg.idplot.layers.coneflags5000.select = 1" nn eq 1

      set l:nn reg.idplot.layers.coneflags5000.select+1
      assert/text="test get reg.idplot.layers.coneflags5000.select+1 = 2" nn eq 2

      ! TEST setting SITE=^fileName via KeyObject.setKeys - this fails with a
      ! "ERROR: Unable to convert FOO to type D [LAYER]" error when there is a
      ! switch that matches the start of the file name.

      if ENV.OSTYPE neqs DOS then
        SEDIT fileName word SPLIT "/"
        res badSwitch "/^{word(1)}"
        res badSwitch badSwitch.toUpperCase()
        res badArg    "foo"

        invoke ,, this.args.put(badSwitch,badArg) !adds a switch to match filename
      else
        info "badSwitch tests not applicable to DOS."
        set fileName ^{datDir}.site.prm
      endif

      layer Geo myLayer { NAME=myLayer,SVA=SV1.prm,SVB=SV2.prm,CONIC=1,SITE="^fileName",&
                          TDOAMODE=Region,FDOAMODE=Region,FLINKT=1e6,&
                          FLINKA=-1e6,FLINKB=-1e6,FLAGS=ALL,TDOA=0,FDOA=0,&
                          DTDOA=1e-5,DFDOA=0.5 } idplot

      assert/text="^reg.main.name: LayerGeo added." reg.idplot.layers.myLayer REXISTS

      ! Test exception handling in KeyObject (Bug 1408)
      res oldDebug "^{env.debug}"
      debug on trace
      shellgui/id=idshell/attach
      invoke ,, nxm.sys.test.TestUtil.throwRuntimeException()

      if "|TRACE|" nsubs oldDebug.toUpperCase() then
        debug off trace
      endif
    pipe off
  endif

  if origDatAux rexists then res aux.dat "^origDatAux"  ! restore original AUX.DAT

endmacro

!******************************************************************************!
procedure processMessage m:msg

  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="Interactive KeyObject tests" msg.data eqss "PASS"
    pipe stop
  else
    ! do nothing
    ! say "Unhandled message name: ^msg.name from fid: ^msg.fid dats: ^msg.data"
  endif

return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
procedure testGetArrayValues
  ! This tests for Bug 229 which caused an exception if ginve an array index greater
  ! than 9.

  res temp (0,1,2,3,4,5,6,7,8,9,10)

  assert/text="KEYOBJECT: temp.size = 11" temp.size eq 11

  res i 0
  while i LT temp.size
    assert/text="KEYOBJECT: temp(^i) = ^i" temp(^i) eq ^i
    calc i i 1 +
  endwhile

  rem x y z t r i alt lat lon
  assert/text="KEYOBJECT: temp(X) = 0" temp(X) eq 0
  assert/text="KEYOBJECT: temp(Y) = 1" temp(Y) eq 1
  assert/text="KEYOBJECT: temp(Z) = 2" temp(Z) eq 2
  assert/text="KEYOBJECT: temp(T) = 3" temp(T) eq 3
  assert/text="KEYOBJECT: temp(R) = 0" temp(R) eq 0
  assert/text="KEYOBJECT: temp(I) = 1" temp(I) eq 1
  assert/text="KEYOBJECT: temp(ALT) = 0" temp(ALT) eq 0
  assert/text="KEYOBJECT: temp(LAT) = 1" temp(LAT) eq 1
  assert/text="KEYOBJECT: temp(LON) = 2" temp(LON) eq 2

  rem temp
return

!******************************************************************************!
procedure testTableValues
  res foo {}
  res foo.bar {}
  set foo.bar.baz 1

  set l:xx foo.bar.baz
  assert/text="test get foo.bar.baz = 1" xx eq 1

  set l:xx foo.bar.baz+1
  assert/text="test get foo.bar.baz+1 = 2" xx eq 2


  ! Check for a existant table entry
  assert/text="REG.SHELL REXISTS = TRUE" REG.SHELL REXISTS

  ! Check for a non-existant table entry
  assert/text="REG.SNAFU REXISTS = FALSE" REG.SNAFU NREXISTS

  ! Check for a non-existant table entry that happens to match a field in Table
  assert/text="REG.HTAB REXISTS = FALSE" REG.HTAB NREXISTS
return


!******************************************************************************!
procedure testSetKeys
  if ENV.OSTYPE neqs DOS then
    fname fileName dat site prm sys

    SEDIT fileName word SPLIT "/"
    res badSwitch "/^{word(1)}"
    res badSwitch badSwitch.toUpperCase()
    res badArg    "foo"
    res switchOne "/ONE"
    res valueOne  "1"
    res switchTwo "/TWO"
    res valueTwo  "2"

    invoke ,, this.args.put(badSwitch,badArg)   ! adds a switch to match filename
    invoke ,, this.args.put(switchOne,valueOne) ! adds a switch that is a number
    invoke ,, this.args.put(switchTwo,valueTwo) ! adds a switch that is a number

    res valueA "/ONE"
    res valueB "/ONE/TWO"
    res valueC "(/ONE/TWO)"
    res valueD "(/ONE)/TWO"
    res valueE "^/ONE/TWO"

    res oldTable {FN=^fileName,A=^valueA,B=^valueB,C=^valueC,D=^valueD,E=^valueE}
    res newTable {}

    invoke ,, nxm.sys.lib.KeyObject.setKeys(newTable,oldTable,this,1)

    assert/text="newtable.FN = ^fileName"  newtable.fn eqs ^fileName
    assert/text="newtable.A  = 1.0"        newtable.a  eq  1.0
    assert/text="newtable.B  = /ONE/TWO"   newtable.b  eqs "/ONE/TWO"
    assert/text="newtable.C  = (/ONE/TWO)" newtable.c  eqs "(/ONE/TWO)"
    assert/text="newtable.D  = (/ONE)/TWO" newtable.d  eqs "(/ONE)/TWO"
    assert/text="newtable.E  = 1/TWO"      newtable.e  eqs "1/TWO"
  else
    info "newtable tests not applicable to DOS."
  endif
return
