! The NeXtMidas side of the messaging tests when running NeXtMidas as an
! X-Midas option tree. See the X-Midas primitive of the same name for
! the other side of these tests and details on which ID is used for what.
!
!  @author  J. Czechowski IV
!  @version $Id: test_xmmessage.mm,v 1.14 2011/03/30 20:30:36 ntn Exp $
!  @since   NeXtMidas 2.1.1
!
startmacro s:xmid["XM.1"]
  res nmid "XM.^{reg.main.id}"
  res/global XM.DONE_^{reg.main.id} FALSE

  ASSERT/INIT=G_STATUS

  assert/text="Found nxm/sys/cfg/qmessages.tbl" nxm.sys.cfg.qmessages.tbl fexists

  res longstr12 "abcdefghijkl"
  res longstr32 "abcdefghijklmnopqrstuzwxyz012345"
  res longstr36 "abcdefghijklmnopqrstuzwxyz0123456789" ! Note: 36 truncated to 32 characters when being sent.

  pipe on
    waveform _junk "SF" 1e9 ! Keeps pipe running long enough to test messaging
  pipe off
endmacro


procedure processMessage m:msg
  !info "Got message name=^{msg.name} info=^{msg.info} data=^{msg.data} from=^{msg.from} to=^{msg.to}"

  if msg.name eqs "TEST1" then
    assert/text="TEST1 message fid=^{xmid}"       msg.fid  eqs "^xmid"
    assert/text="TEST1 message to=^{nmid}"        msg.tid  eqs "^nmid"
    assert/text="TEST1 message info=99"           msg.info         eq 99
    assert/text="TEST1 message data.getsize=0"    msg.data.getsize eq 0

  elseif msg.name eqs "TEST2" then
    assert/text="TEST2 message fid=^{xmid}"       msg.fid  eqs "^xmid"
    assert/text="TEST2 message to=^{nmid}"        msg.tid  eqs "^nmid"
    assert/text="TEST2 message info=7"            msg.info eq 7
    assert/text="TEST2 message data.getsize=3"    msg.data.getsize eq 3
    assert/text="TEST2 message val1=1"            msg.data.val1 eq  1
    assert/text="TEST2 message val2=Two"          msg.data.val2 eqs "Two"
    assert/text="TEST2 message val3=3"            msg.data.val3 eq  3

  elseif msg.name eqs "TEST3" then
    assert/text="TEST3 message fid=^{xmid}"       msg.fid  eqs "^xmid"
    assert/text="TEST3 message to=^{nmid}"        msg.tid  eqs "^nmid"
    assert/text="TEST3 message info=0"            msg.info eq 0
    assert/text="TEST3 message data.getsize=4"    msg.data.getsize eq 4
    assert/text="TEST3 message val1=1"            msg.data.val1 eq 1
    assert/text="TEST3 message val2=longstr12"    msg.data.val2 eqs longstr12
    assert/text="TEST3 message val3=longstr32"    msg.data.val3 eqs longstr32
    assert/text="TEST3 message val4=2"            msg.data.val4 eq 2

  elseif msg.name eqs "TEST4" then
    assert/text="TEST4 message fid=^{xmid}"       msg.fid  eqs "^xmid"
    assert/text="TEST4 message to=^{nmid}"        msg.tid  eqs "^nmid"
    assert/text="TEST4 message info=0"            msg.info eq 0
    assert/text="TEST4 message data.getsize=6"    msg.data.getsize eq 6
    assert/text="TEST4 message val1=1"            msg.data.val1 eq 1
    assert/text="TEST4 message val2=2"            msg.data.val2 eq 2
    assert/text="TEST4 message val3=3.3"          msg.data.val3 eqt 3.3
    assert/text="TEST4 message val4=4.4"          msg.data.val4 eqt 4.4
    assert/text="TEST4 message val5=5.5"          msg.data.val5 eqt 5.5
    assert/text="TEST4 message val6=6.6"          msg.data.val6 eqt 6.6

  elseif msg.name eqs "SETL" then
    assert/text="SETL message fid=^{xmid}"        msg.fid  eqs "^xmid"
    assert/text="SETL message to=^{nmid}"         msg.tid  eqs "^nmid"
    assert/text="SETL message info=42"            msg.info eq 42
    assert/text="SETL message data.getsize=3"     msg.data.getsize eq 3
    assert/text="SETL message object=MyObj"       msg.data.object eqs "MyObj"
    assert/text="SETL message value=1"            msg.data.value  eq  1
    assert/text="SETL message value2=2"           msg.data.value2 eq  2

    if /nosend isFalse then
      set t:data2 {ARGS=3,TYPE="DAD",VAL1=10.0,VAL2="Twenty",VAL3=30.0}
      set t:data3 {ARGS=4,TYPE="LS[12]S[32]L",VAL1=1,VAL2="^longStr12",VAL3="^{longstr36}",VAL4=2}
      set t:data4 {ARGS=6,TYPE="2L2DFF",VAL1=1,VAL2=2,VAL3=3.3,VAL4=4.4,VAL5=5.5,VAL6=6.6}
      set t:datas {OBJECT="NmObj",VALUE2=30,VALUE=20}

      message send ID="XM.3"    NAME="TESTN0" INFO=0              ! Message not sent to macro
      message send ID="^{xmid}" NAME="TESTN1" INFO=10             ! Message with no data sent to XM macro
      message send ID="^{xmid}" NAME="TESTN2" INFO=20 DATA=data2  ! Unconfigured message with data
      message send ID="^{xmid}" NAME="TESTN3" INFO=0  DATA=data3  ! Unconfigured message with string
      message send ID="^{xmid}" NAME="TESTN4" INFO=0  DATA=data4  ! Unconfigured message type multipliers
      message send ID="^{xmid}" NAME="SETL"   INFO=30 DATA=datas  ! Configured message

      ! This next message is a test for Bug 431 (type field length >8 chars)
      message send id="XM.3" name=rerr info=0 data={TEXT="MYTEXT",ADDR="MYADDR",FUNC="MYFUNC",DATA="MYDATA"}

    endif

    ASSERT/TID="ALL"/TOTAL=TESTALL/STAT=STATUS/DEBUG=NO
    res/global XM.STATUS_^{reg.main.id} STATUS
    res/global XM.DONE_^{reg.main.id}   TRUE

  else
    assert/text="Got unknown message name=^{msg.name}" false isTrue
  endif

return
