!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Test find command
!
! @author Jay P Hallam
!
! DEPENDENCIES: Depends on REMOVE and RESULTS commands
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!q
startmacro

  ! 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

  find world fullPathName 
  assert/text="^reg.main.name: world full path name" fullPathName FEXISTS

  find world urlName /url
  assert/text="^reg.main.name: world URL string" urlName FEXISTS

  call test_fullname_initialization

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

endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "FIND_SELECTION" then
    if msg.data eqs "FILE_EXISTS" then
      find world urlName /URL 
      res urlName
    else
      find my_file_that_does_not_exist urlName /URL
      res urlName
    endif
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
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

!******************************************************************************!
! 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

!******************************************************************************!
! Test proper initialization of FIND Intrinsic fullName value - should not be field
!******************************************************************************!
procedure test_fullname_initialization

  call test_fullname_initialization_getinstance
  call test_fullname_initialization_loop

return
!**********************************************************
! FIND Intrinsic Initialization - original - for reference
! This was developed before understanding Jeff's email
! stating the issue was in a "control section". It is left
! for reference. NXM-3619
!**********************************************************
procedure test_fullname_initialization_getinstance

  invoke mymidas reg.main.getParentContext()

  ! run find command with file that does exist and save result
  invoke findCmd nxm.sys.lib.Shell.runCommand(mymidas,"FIND FILE=world FULLNAME=myfilepath")
  res myvalid mymidas.results.myfilepath

  ! change FIND command FILE argument to file that does not exist
  invoke ,, findCmd.checkin(mymidas)
  invoke ,, findCmd.setArg(FILE,"find_file_that_does_not_exist")

  ! execute SAME FIND command instance with new FILE argument
  invoke ,, nxm.sys.lib.Shell.execCommand(mymidas,findCmd)
  res myinvalid mymidas.results.myfilepath

  ! without the fix, myfilepath would not get updated and would
  ! still have the world path and myinvalid will equal myvalid
  assert/text="^reg.main.name: initialization of fullName (part 1)" myinvalid NEQS myvalid
  assert/text="^reg.main.name: initialization of fullName (part 2)" myinvalid EQS  ""

return
!**********************************************************
! FIND Intrinsic Initialization - based on Jeff's use case
! NXM-3619
!**********************************************************
procedure test_fullname_initialization_loop

  ! Create only odd numbered files
  DO count 1 7 2
    waveform ram:my_file_to_find^{count}.tmp
  ENDDO

  ! Try to find both even and odd
  ! NOTE: RAM files end up with extension -tmp - is this correct?
  DO count 1 7 1
    find my_file_to_find^{count}.tmp fullPathName
    calc modres count 2 MOD
    IF modres EQ 1
      res expected "ram:my_file_to_find^{count}-tmp" ! odd expected
    ELSE
      res expected ""                                ! even expected
    ENDIF
    assert/text="^reg.main.name: initialization of fullName (file ^count})" &
        fullPathName EQS expected
  ENDDO

return