!******************************************************************************!
! Test TimeLine Keyword deletion via TCDEL
!
! @bug      NXM-3185 Need way to remove timeline keywords
! @since    NeXtMidas 3.9.0
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  res origTimelineFile "nxm.sys.test.test_fftout_timeline.prm"

  ! Create a table with the needed information to test TCDEL on each timeline keyword type
  res tabTC {FILE="ram:tc_timeline_3185.tmp",TIMELINEKEYS="TCSAMPLE_208,TC_WHOLE_208,TC_FRAC_208",TL_QUAL="{USEBLUETCTIMELINE=true}"}
  res tabTIMELINE {FILE="ram:blue_timeline_3185.tmp",TIMELINEKEYS="TIMELINE",TL_QUAL=""}
  res tabTIMETAG {FILE="ram:platinum_timeline_3185.tmp", &
                  TIMELINEKEYS="TIMETAG.OFFSET,TIMETAG.TIME,TIMETAG.TIME.UNITS,TIME_EPOCH", &
                  TL_QUAL="{USEPLATINUMTIMELINE=true}"}
  
  ! Call test for each timeline keyword type
  call testTimelineRemoved origTimelineFile tabTC
  call testTimelineRemoved origTimelineFile tabTIMELINE
  ! say "!!!!! Currently WARNINGS are expected - though they should be investigated - see issue !!!!" - fixed in 3.9.1
  call testTimelineRemoved origTimelineFile tabTIMETAG

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

!******************************************************************************!
! Test that timeline keywords were remove
!******************************************************************************!
procedure testTimelineRemoved S:origFileName T:timelineTable

   ! Convert original test file to file with specified type of timeline keywords
   convert origFileName KWCONV timelineTable.file^{timelineTable.tl_qual} OPTIONS={}

   ! Get the TimeLine keywords that should originally be there
   invoke keyArray timelineTable.TIMELINEKEYS.split(",")

   ! Make sure keywords are in the file before doing TCDEL
   res missingTLKeywords false
   foreach tlkey IN keyArray
     keyword timelineTable.file GET ^tlkey myval
     if myval ISNULL AND missingTLKeywords ISFALSE then
       assert/text="^reg.main.name: TCDEL test is invalid as ^timelineTable.file does not contain TL Keywords" "FALSE" isTrue
       res missingTLKeywords true
     endif
   endfor

   ! Do the actual delete and test
   if missingTLKeywords ISFALSE then
     ! Delete the keywords
     keyword ^timelineTable.file TCDEL
     ! Check to make sure all are there
     foreach tlkey IN keyArray
       keyword timelineTable.file GET ^tlkey myval
       assert/text="^reg.main.name: TCDEL successfully deleted TL Keyword ^tlkey in ^timelineTable.file" myval ISNULL
     endfor
   endif

return