!******************************************************************************!
! test setting and updating RESULTS in different contexts (command line vs macro)
!
! Arguments: L:inRes[DEF=0] starting point for incrementing
!
! @bug      2454: RESULT: Context different when result set in loop in shell vs macro
! @author:  W Knight / J Bauer
!******************************************************************************!
startmacro l:inRes[0] /theme=wob

  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)

  if interAct then
    set instructions "The theme of this macro should be WOB (White On Black).\n\n&
                      Press the 'Run Automatic Test' button and observe results in the terminal.\n\n&
                      Next, open a new Shell and type the following commands:\n  &
                      res x 0\n  &
                      do i 1 5 \n    &
                        calc x x 1 + \n  &
                      enddo \n  &
                      say \"Value of x=^x \"\n\n &
                      The value of x should be 5."

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 18 /title

      gcontrol button testButt "" "Run Automatic Test"
      call test_context  "inside pipe section"

    pipe off

  endif

  call test_context "outside pipe section"

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 "TESTBUTT" then
    call test_context  "inside pipe section via message"
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! test when run within macro context
procedure test_context S:where

  res myRes ^inRes          ! create a new Result
  calc savedRes ^myRes 5 +  ! set the correct end result value
  do count 1 5 1
    calc myRes myRes 1 +    ! increment it by 1 five times
  enddo

  ! 2012-06-04: remove /DEBUG=0 switch value if it was set
  switch DEBUG origDebug DEL

  assert/text="^reg.main.name: result incremented in loop: ^where" savedRes eq myRes

  if origDebug nIsNull then
    ! 2012-06-04: restore original /DEBUG switch value if it was set
    switch DEBUG origDebug SET
  endif

return
