!------------------------------------------------------------------------------!
! Tests for LIST2 command.
!
!   Author : J. Czechowski IV
!------------------------------------------------------------------------------!
startmacro/tsetup=nxm.sys.test.test_list2_setup.tbl
  switch INTERACTIVE  interAct     GET DEF=TRUE
  switch standalone   standalone   GET DEF=TRUE
  switch COREGRAPHICS coregraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  if interAct OR coreGraphics then
    set t:data {}
    set t:data.0 {PRI=A,X=00,Y=00}
    set t:data.1 {PRI=B,X=10,Y=10}
    set t:data.2 {PRI=C,X=20,Y=20}
    set t:data.3 {PRI=D,X=30,Y=30}
    set t:data.4 {PRI=E,X=40,Y=40}
    set t:data.5 {PRI=F,X=50,Y=50}
    set t:data.6 {PRI=G,X=60,Y=60}
    set t:data.7 {PRI=H,X=70,Y=70}
    set t:data.8 {PRI=I,X=80,Y=80}
    set t:data.9 {PRI=J,X=90,Y=90}
    
    set s:toAdd    "0,1,2"
    set s:toSet    "3,4"
    set s:toUpdate "5,6,7"
    set t:groupAdd {X=^{data.8},Y=^{data.9}}
    
    header/create test_file{AUX=RAM} SR=(PRI/1A,X/SL,Y/SL)
    
    pipe init
      panel/msgid=MAIN/controls=gc
      list2/grid=on/msgid=MAIN/id=idlist/base=0 test_file{AUX=RAM}
      
      ! Do not reorder these tests, add any new ones to the end.
      call test_addRow
      call test_setRow
      call test_updateRow
      set reg.idlist.primarykey "PRI"
      call test_setRow_pri
      call test_updateRow_pri
      if standalone isTrue then
        gcontrol label  macro   "MACRO"
        gcontrol menu MAIN "Exit with Status = " "PASS,FAIL" /tleft
        set gc.MAIN.itemColors {PASS=GREEN,FAIL=RED}
      endif
      gcontrol text idtext "Instructions" "Done with automatic tests." 24 8 /title

    pipe off
  endif
endmacro

!------------------------------------------------------------------------------!
! Procedure for handling all messages
procedure processMessage m:msg
  if msg.name EQS "MAIN" then
    if msg.data EQS "EXIT" then
      pipe stop
    else
      assert/text="LIST2 tests" "PASS" SUBS msg.data
      pipe stop
    endif
  elseif msg.fid EQS "IDLIST" then
    !say "Got ^msg.name from ^msg.fid"
  else
    say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return


!------------------------------------------------------------------------------!
! Verify that the data in LIST2 matches the DATA table
procedure check_expected s:descr
  foreach i inTable data
    table data.^{i} equals reg.idlist.getRow(^{i}) test
    assert/text="^{descr} (i=^{i})" test isTrue
  endfor
return


!------------------------------------------------------------------------------!
! Add some rows using addRow, setRow, updateRow, addRows
procedure test_addRow
  foreach i inList toAdd
    invoke ,, reg.idlist.addRow(data.^{i})
  endfor
  foreach i inList toSet
    set row ^{data.^{i}}
    set row.row i
    invoke ,, reg.idlist.setRow(row)
  endfor
  foreach i inList toUpdate
    set row ^{data.^{i}}
    set row.row i
    invoke ,, reg.idlist.updateRow(row)
  endfor
  invoke ,, reg.idlist.addRows(groupAdd)

  call check_expected "Testing adding rows with various methods"
return

!------------------------------------------------------------------------------!
! Add some rows using setRow
procedure test_setRow
  set data.1.x 0
  set data.1.y 100
  set data.3.x 301
  set data.3.y 302
  set data.4.x 401
  set data.4.y 402

  invoke ,, reg.idlist.setRow(1,{PRI=B,Y=100})
  invoke ,, reg.idlist.setRow(3,{PRI=D,X=301,Y=302})
  invoke ,, reg.idlist.setRow({PRI=E,X=401,Y=402,ROW=4})

  call check_expected "Testing setRow"
return

!------------------------------------------------------------------------------!
! Add some rows using updateRow
procedure test_updateRow
  set data.5.y 555
  set data.7.x 777

  invoke ,, reg.idlist.updateRow(5,{Y=555})
  invoke ,, reg.idlist.updateRow({X=777,ROW=7})

  call check_expected "Testing updateRow"
return

!------------------------------------------------------------------------------!
! Add some rows using setRow with a primary key
procedure test_setRow_pri
  set t:data.6 {PRI=G,X=6666,Y=0}
  set t:data.7 {PRI=H,X=7777,Y=7777}
  

  invoke ,, reg.idlist.setRow({PRI=G,X=6666})
  invoke ,, reg.idlist.setRow({PRI=H,X=7777,Y=7777})

  call check_expected "Testing setRow with a primary key"
return

!------------------------------------------------------------------------------!
! Add some rows using updateRow with a primary key
procedure test_updateRow_pri
  set t:data.8 {PRI=I,X=8888,Y=80}
  set t:data.9 {PRI=J,X=9999,Y=9999}

  invoke ,, reg.idlist.updateRow({PRI=I,X=8888})
  invoke ,, reg.idlist.updateRow({PRI=J,X=9999,Y=9999})

  call check_expected "Testing updateRow with a primary key"
return

