!******************************************************************************!
! Tests the MESSAGE command with the (1) this.msgid, (2) "PARENT.<id>" and
! (3) "GLOBAL.<id>" syntax to send messages from child/sub-macro to
! (1) the registered message ID, (2) command in parent's registry, or
! (3) command in the global registry, respectively.
!
! Author : J. Czechowski IV
! Version: $Id: test_message_child_macro.mm,v 1.2 2013/02/04 16:12:37 ntn Exp $
!******************************************************************************!
startmacro
  ! Messages will get INFO= updated when passed around:
  !    INFO=0   Sent from here
  !    INFO=1   Sent from parent macro (via response to CHILD's msg to PARENT.MAIN)
  !    INFO=5   Sent from parent macro (via response to CHILD's msg to THIS.MSGID)
  !    INFO=2   Sent from %test_message_echo in this macro
  !    INFO=3   Sent from %test_message_echo in parent macro
  !    INFO=4   Sent from %test_message_echo in global
  res T:expMsgs  {0={NAME="MSG_TEST_0",INFO=0,DATA="CHILD->MAIN"},&
                  1={NAME="MSG_TEST_1",INFO=1,DATA="CHILD->PARENT.MAIN"},&
                  5={NAME="MSG_TEST_5",INFO=5,DATA="CHILD->THIS.MSGID"},&
                  2={NAME="MSG_TEST_2",INFO=2,DATA="CHILD->ECHO"},&
                  3={NAME="MSG_TEST_3",INFO=3,DATA="CHILD->PARENT.ECHO"},&
                  4={NAME="MSG_TEST_4",INFO=4,DATA="CHILD->GLOBAL.ECHO"}}
  res L:msgCount 0

  ! We intentionally call all of the instances of message_echo with /ID=ECHO since
  ! we want to make sure that it is actually being found in the correct place, not
  ! just chaining up from the current/parent.
! Once Bug 2687 is resolved, change 'ECHOX' back to 'ECHO'
warn "Partial bypass of test case to work around Bug 2687"

  ! Launch the global version of %test_message_echo
  invoke globalM reg.shell.getMidasContext()
  invoke ,,      nxm.sys.lib.Shell.runCommand(globalM,"nxm.sys.test.message_echo/bg/ID=ECHOX COUNT=1 INFO=4 WAIT=5.0")

  pipe on
    nxm.sys.test.message_echo/ID=ECHO COUNT=1 INFO=2 WAIT=5.0
    message SEND "MAIN"         NAME="MSG_TEST_0" INFO=0 DATA="CHILD->MAIN"
    message SEND "PARENT.MAIN"  NAME="MSG_TEST_1" INFO=0 DATA="CHILD->PARENT.MAIN"

    message SEND "ECHO"         NAME="MSG_TEST_2" INFO=0 DATA="CHILD->ECHO"
    message SEND "PARENT.ECHO"  NAME="MSG_TEST_3" INFO=0 DATA="CHILD->PARENT.ECHO"
    message SEND "GLOBAL.ECHOX" NAME="MSG_TEST_4" INFO=0 DATA="CHILD->GLOBAL.ECHO"

    message SEND  this.msgid    NAME="MSG_TEST_5" INFO=0 DATA="CHILD->THIS.MSGID"
  pipe off

  table expMsgs GETSIZE msgTotal
  assert/text="Got all expected messages (^msgTotal) from child macro (^msgCount)" msgCount EQ msgTotal
endmacro

!******************************************************************************!
procedure processMessage m:msg
  res z:ok false

  for expMsg inTable/v expMsgs
    if ((msg.name EQS expMsg.name) and &
        (msg.info EQ  expMsg.info) and &
        (msg.data EQS expMsg.data)) then
      calc L:msgCount msgCount 1 +
      set  Z:ok true
    endif
  endfor

  if (ok) then
    assert/text="Got expected child macro message ^msg.data" true
  else
    assert/text="Got unexpected child macro message ^msg DATA=^msg.data" false
  endif
return
