!******************************************************************************!
! Test "as is" timeline propagation using CoreIO.IOOption and file qualifier
!
! Arguments: None
!
! @bug      NXM-3532 Indexing error with TIMELINE/TC keyword set file copy
! @since    NeXtMidas 3.9.3
! @author : Jeanette McCall

!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE

  env get IOOptions origIOOptions

  ! Specs from this file used for timeline creation
  res referenceFile nxm.sys.test.test_fftout_timeline.prm
  res test_file ram:create_tl_not_first.tmp
  call createFile

  res input_file                   ^test_file
  res qualifier_true_test_output   "tl_copy_qualifier.tmp"
  res qualifier_false_test_output  "tl_copy_qualifier_false.tmp"
  res coreio_test_output           "tl_output_coreio.tmp"

  !*************************************************************
  ! Test as is timeline keyword propagation via qualifier true
  !*************************************************************
  env set IOOptions -SameTimelineKeywords
  noop input_file ^{qualifier_true_test_output}{SAMETLKW=true}
  assert/text="^reg.main.name: flag PropagateTimelineAsIs test" qualifier_true_test_output FEQ/K input_file 


  !***** CoreIO.IOOptions.propagateTimelineAsIs in effect ******
  env set IOOptions +SameTimelineKeywords

  !*************************************************************
  ! Test as is timeline keyword propagation via CoreIO.IOOption
  !*************************************************************
  noop input_file coreio_test_output
  assert/text="^reg.main.name: CoreIO SameTimelineKeywords test" coreio_test_output FEQ/K input_file 

  !*************************************************************
  ! Test that qualifier false overrides CoreIO.IOOPtion
  !*************************************************************
  noop input_file ^qualifier_false_test_output{SameTimelineKeywords=false}
  key input_file TGET kwTableIn
  table kwTableIn GETKEY TCSAMPLE_1 in_tcsample1
  key qualifier_false_test_output TGET kwTableOut
  table kwTableOut GETKEY TCSAMPLE_1 out_tcsample1
  assert/text="^reg.main.name: qualifier overrides CoreIO TCSAMPLE_1 matches" in_tcsample1 eq out_tcsample1
  assert/text="^reg.main.name: qualifier overrides CoreIO timeline changed" qualifier_false_test_output NFEQ/K input_file 

  !***** Restore original CoreIO.IOOptions **********************
  env set IOOptions origIOOptions

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

!*******************************************************************************
! Create the file
!*******************************************************************************
procedure createFile
  info "It is okay to see two warnings, as this 'bad' file is created ... tests should pass"
  set tolr            1e-6  ! timeline tolerance
  set timelineLength  16    ! timeline length

  noop/tl=1 referenceFile test_file{USEBLUETCTIMELINE=true,TLT=^tolr,TLL=^timelineLength}

return
