!******************************************************************************!
! Test Non-TimeLine Keyword deletion via NTCDEL
!
! @bug      NXM-3636 Need a way to remove non-timeline keywords
! @since    NeXtMidas 3.9.4
! @author:  Zachary Matthews
!******************************************************************************!
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)
  
  ! Assign types
  set tcType TC
  set timelineType Timeline
  set platType Platinum
  
  ! Assign file names
  set ntcdelTestIdentifier test_ntcdel_
  set testTCFileName ^{ntcdelTestIdentifier}wavechirp_tc.prm
  set testTimelineFileName ^{ntcdelTestIdentifier}fftout_timeline.prm
  set testPlatFileName ^{ntcdelTestIdentifier}testplat1001.cdif
  set origTCFileName nxm.sys.test.fft_wavechirp_tc.prm
  set origTimelineFilename nxm.sys.test.test_fftout_timeline.prm
  set origPlatFileName nxm.sys.test.testplat1001.cdif

  ! Test the NTCDEL functionality on a tc, timeline, and platinum file
  ! If the test fails on one file, the remaining files are not tested and the test altogether is considered failed
  call testNonTimelineRemoved tcType origTCFileName testTCFileName
  call testNonTimelineRemoved timelineType origTimelineFileName testTimelineFileName
  call testNonTimelineRemoved platType origPlatFileName testPlatFileName

  ! Clean up remaining test data
  remove ntcdelTestIdentifier *FileName *Type
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 non-timeline keywords were removed
!******************************************************************************!
procedure testNonTimelineRemoved S:fileType S:origFileName S:testFileName
  set quals ""

  ! Make a copy of the file to be tested
  file copy/f origFileName testFileName
  if fileType eqs "Platinum" then
    info "Expect to see about 10 warnings for Unsupported BLUE version VER='2.0' below for .cdif files"
    set quals {FORCE=TRUE}
    file unprotect testFileName
    keyword testFileName^quals ntcdel
  endif
  
  ! Make a copy of the test file for comparison
  fname label=testFileName name=baseFileName ext=fileExt /parse 
  set testFileNameModified ^{baseFileName}_modified.^fileExt
  file copy/f testFileName testFileNameModified
  
  ! Add non-timeline keys to the modified test file
  keyword testFileNameModified^quals add testKey1 1 testKey2 2 testKey3 3

  ! Run NTCDEL on the modified test file, then check that the keys in both test files match
  keyword testFileNameModified^quals ntcdel

  if fileType neqs "Platinum" then
    assert/text="Verifying NTCDEL with ^fileType file did not alter main header" testFileName feq/h testFileNameModified
  endif
  assert/text="Verifying NTCDEL with ^fileType file deleted proper ext header kw" testFileName feq/k testFileNameModified
  
  ! Clean up outstanding test files
  erase testFileName testFileNameModified
return
