startmacro
!------------------------------------------------------------------------------!
! Tests for ENVIRONMENT command.
!
!   Author : J. Hallam / Neon Ngo
!
!------------------------------------------------------------------------------!
  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)

  invoke skipBecauseHeadless nxm.sys.lib.Shell.isHeadless()

  call test_get
  call test_set
  if skipBecauseHeadless then
    info "^reg.main.name: skipping test_nslookup because HEADLESS"
  else
    call test_nslookup
  endif
  call test_gc

  if interAct then
    res inst "In the SHELLGUI on the right (not console) see:\n&
              - the Java system properties\n&
              - as key=values\n&
              - these should not be truncated at\n  40 characters\n\n&
              Below (in control pane) see:\n&
              - total number of screens\n&
              - # of non-virtual screens\n  &
                should not include virtual screens\n  &
                e.g. in cinerama display mode,\n  it should be 1 not 2 or 3."

    ! two GETs are for testing BUG 2117
    environment GET NUMSCREENS        ns
    environment GET NUMNONVIRTSCREENS nnvs

    pipe on
      panel/msgid=MAIN/setup/controls=gc

      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemcolors {PASS=GREEN,FAIL=RED}

      gcontrol text idtext "Instructions" "^inst" 24 16 /title

      ! read only to display numscreen and numnonvirtscreen results for testing BUG 2117
      gcontrol lval numscr "# Screens"             ns   ns   ns   /flags=NOEDIT|NOMSG
      gcontrol lval numscr "# Non-Virtual Screens" nnvs nnvs nnvs /flags=NOEDIT|NOMSG

      shellgui/id=idshellgui/attach
      say ""
      ! test case for Bug 2043
      say "nM> environment "SHOW" "PROPS"
      environment "SHOW" "PROPS"
      say ""
    pipe off

  endif

  ! Additional interactive tests, only run with /BASELINE
  if interAct AND baseline then
    %test_environment_os_env_vars ! Bug 1368
  endif

endmacro

procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests " msg.data eqss "PASS"
    pipe stop
  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
procedure test_set
  set str "9"
  env set junk str
  assert/text="^reg.main.name: set JUNK" "^str" EQS "^env.junk"

  ! test case for Bug 1343 - REMOVE: key from protected Table should NOT be allowed
  info "One warning should be displayed saying: Must use FORCE flag to remove tag=JUNK from ReadOnly table."
  remove env.junk
  assert/text="^reg.main.name: REMOVE ENV.JUNK (key on protected table)" env.junk REXISTS

  ! test case for Bug 1870 - REMOVE: honor /FORCE for removing key from protected Table
  remove/force env.junk
  assert/text="^reg.main.name: REMOVE/FORCE ENV.JUNK (key on protected table)" env.junk NREXISTS

  ! test case for Bug 2741 - setting UseNative with +/- option
  set origUseNative env.usenative
  env set usenative -Other
  invoke expectedUseNative nxm.sys.lib.Native.getUse()
  assert/text="^reg.main.name: Setting UseNative with -Other" expectedUseNative eqs env.usenative
  env set usenative origUseNative
  
  ! test for Bug 2985 "ENVIRONMENT: SET KEYMAP should support loading from .tbl file" 
  res t:origKeyMap env.keymap
  res t:expKeyMapTable {key1=test1,key2=test2}
  res/save=keymap_test_table.tbl expKeyMaptable
  env set keymap keymap_test_table.tbl
  assert/text="^reg.main.name: SET KEYMAP support loading from .tbl file" expKeyMapTable oeq env.keymap.expKeyMapTable
  env set keymap origKeyMap
  remove/force expKeyMapTable
  files/e keymap_test_table.tbl 
return

!******************************************************************************!
procedure test_get
  env get NMROOT NMROOT
  assert/text="^reg.main.name: get NMROOT"    "^NMROOT" EQS "^env.NMROOT"

  env get USER USER
  assert/text="^reg.main.name: get USER"      "^USER" EQS "^env.USER"

  env get HOME HOME
  assert/text="^reg.main.name: get HOME"      "^HOME" EQS "^env.HOME"

  env get HOSTNAME HOSTNAME
  assert/text="^reg.main.name: get HOSTNAME"  "^HOSTNAME" EQS "^env.HOSTNAME"

  env get HOSTADDR HOSTADDR
  assert/text="^reg.main.name: get HOSTADDR"  "^HOSTADDR" EQS "^env.HOSTADDR"

  env get OSTYPE OSTYPE
  assert/text="^reg.main.name: get OSTYPE"    "^OSTYPE" EQS "^env.OSTYPE"

  env get OSNAME OSNAME
  assert/text="^reg.main.name: get OSNAME"    "^OSNAME" EQS "^env.OSNAME"

  env get OSREP OSREP
  assert/text="^reg.main.name: get OSREP"     "^OSREP" EQS "^env.OSREP"

  env get EDITOR EDITOR
  assert/text="^reg.main.name: get EDITOR"    "^EDITOR" EQS "^env.EDITOR"

  env get VIEW VIEW
  assert/text="^reg.main.name: get VIEW"      "^VIEW" EQS "^env.VIEW"

  env get THEME THEME
  assert/text="^reg.main.name: get THEME"     "^THEME" EQS "^env.THEME"

  env get STYLE STYLE
  assert/text="^reg.main.name: get STYLE"     "^STYLE" EQS "^env.STYLE"

  env get PID PID
  invoke pidIsInt nxm.sys.lib.StringUtil.isInteger(PID)
  assert/text="^reg.main.name: get PID"                   ^PID EQ ^env.PID
  assert/text="^reg.main.name: PID is an integer"         pidIsInt
  assert/text="^reg.main.name: PID is a positive integer" PID GE 0

  env get MSF MSF
  assert/text="^reg.main.name: get MSF"       "^MSF" EQS "^env.MSF"

  env get NMVERSION NMVERSION
  assert/text="^reg.main.name: get NMVERSION"   "^NMVERSION" EQS "^env.NMVERSION"

  env get PAUSE PAUSE
  assert/text="^reg.main.name: get PAUSE"     ^PAUSE EQ ^env.PAUSE

  env get VERIFY VERIFY
  assert/text="^reg.main.name: get VERIFY"    "^VERIFY" EQS "^env.VERIFY"

  env get DEBUG DEBUG
  assert/text="^reg.main.name: get DEBUG"     "^DEBUG" EQS "^env.DEBUG"

  env get JVMVERSION JVMVERSION
  assert/text="^reg.main.name: get JVMVERSION"    "^JVMVERSION" EQS ^env.JVMVERSION

  env get USENATIVE USENATIVE
  assert/text="^reg.main.name: get USENATIVE" "^USENATIVE" EQS "^env.USENATIVE"

  if env.hwfile REXISTS then
    env get HWFILE HWFILE
    assert/text="^reg.main.name: get HWFILE"  "^HWFILE" EQS "^env.HWFILE"
  endif

  ! ==== NUMSCR & NUMNONVIRTSCREENS ============================================
  env get NUMSCR numscreens
  assert/text="^reg.main.name: get NUMSCR" numscreens GE 0 AND numscreens LE 1000

  env get NUMNONVIRTSCREENS numscrnonvirtual
  assert/text="^reg.main.name: get NUMNONVIRTSCREENS" numscrnonvirtual GE 0 AND numscrnonvirtual LE 1000

  ! ==== DEFAULTTABLEFLAGS & DEFAULTTABLEMODE ==================================
  env get DEFAULTTABLEFLAGS saveFlagsExpE
  env get DEFAULTTABLEMODE  saveModeExpE
  table ,, getdefaultflags  saveFlagsExpT
  table ,, getdefaultmode   saveModeExpT

  env set DEFAULTTABLEFLAGS "|AutoReadPrev|ExceptionOnNull|"
  table ,, getdefaultflags  flagsARPEON
  table ,, setdefaultflags  "|ExceptionOnNull|"
  env get DEFAULTTABLEFLAGS flagsEON

  env set DEFAULTTABLEFLAGS saveFlagsExpE
  env get DEFAULTTABLEFLAGS saveFlagsActE
  table ,, getdefaultflags  saveFlagsActT

  env set DEFAULTTABLEMODE "TreeMap"
  table ,, getdefaultmode  modeTM
  table ,, setdefaultmode  "LinkedHashMap"
  env get DEFAULTTABLEMODE modeLHM

  env set DEFAULTTABLEMODE saveModeExpE
  env get DEFAULTTABLEMODE saveModeActE
  table ,, getdefaultmode  saveModeActT

  assert/text="^reg.main.name: set DEFAULTTABLEFLAGS"     flagsARPEON  eqs "|AutoReadPrev|ExceptionOnNull|"
  assert/text="^reg.main.name: get DEFAULTTABLEFLAGS"     flagsEON     eqs "|ExceptionOnNull|"
  assert/text="^reg.main.name: set/get DEFAULTTABLEFLAGS" saveModeExpE eqs saveModeActE
  assert/text="^reg.main.name: set/get DEFAULTTABLEFLAGS" saveModeExpT eqs saveModeActT

  assert/text="^reg.main.name: set DEFAULTTABLEMODE"     modeTM       eqs "TM"
  assert/text="^reg.main.name: get DEFAULTTABLEMODE"     modeLHM      eqs "LinkedHashMap"
  assert/text="^reg.main.name: set/get DEFAULTTABLEMODE" saveModeExpE eqs saveModeActE
  assert/text="^reg.main.name: set/get DEFAULTTABLEMODE" saveModeExpT eqs saveModeActT

  ! ==== SERVER ================================================================
  env set SERVER "test"
  env get SERVER myserver
  assert/text="^reg.main.name: get SERVER (case-sensitive 'test')"/cs "^myserver" EQS "test"

  env set SERVER 123.456.789
  env get SERVER myserver
  assert/text="^reg.main.name: get SERVER (set to '123.456.789')" "^myserver" EQS "123.456.789"

  env set SERVER "NULL"

  ! ==== FNCASE & FILENAMECASE =================================================
  invoke fncaseSave nxm.sys.lib.FileName.getDefaultFNCase()
  for option inList "LowerCase,KeepCase,AutoCase"
    env set FILENAMECASE "^option"
    invoke fncaseAct nxm.sys.lib.FileName.getDefaultFNCase()
    assert/text="^reg.main.name: set FILENAMECASE=^{option}" "^{option}" EQS "^{fncaseAct}"

    env get FILENAMECASE fncaseAct
    assert/text="^reg.main.name: get FILENAMECASE=^{option}" "^{option}" EQS "^{fncaseAct}"

    env set FNCASE "^option"
    invoke fncaseAct nxm.sys.lib.FileName.getDefaultFNCase()
    assert/text="^reg.main.name: set FNCASE=^{option}" "^{option}" EQS "^{fncaseAct}"

    env get FNCASE fncaseAct
    assert/text="^reg.main.name: get FNCASE=^{option}" "^{option}" EQS "^{fncaseAct}"
  endfor
  invoke nxm.sys.lib.FileName.setDefaultFNCase(fncaseSave)

  ! ==== HOSTNAMEFQDN & SERVERFQDN =============================================
  if skipBecauseHeadless then
    info "^reg.main.name: skipping ENV HOSTNAME EQ HOSTNAMEFQDN test since fails in HEADLESS with Gitlab CI/CD (not Jenkins, only Gitlab)."
  else
    env get HOSTNAME     hostName
    env get HOSTNAMEFQDN hostNameFQDN
    assert/text="^reg.main.name: get HOSTNAMEFQDN" hostNameFQDN EQSS hostName
  endif

  env get SERVER       server
  env get SERVERFQDN   serverFQDN
  assert/text="^reg.main.name: get SERVERFQDN"   serverFQDN   EQSS server

return

!******************************************************************************!
procedure test_nslookup

  env nslookup "^env.hostname" inet
  assert/text="^reg.main.name: NSLOOKUP localhost" "^inet" EQS "^env.HOSTADDR"

return

!******************************************************************************!
procedure test_gc
! Garbage Collection
  env gc ! This is only making sure GC does not output to screen in a macro
  assert/text="^reg.main.name: GC no output to screen" "TRUE" EQS "TRUE"

return