!******************************************************************************!
! Test adding features that do NOT have a TABLE with NAME specified.
!
! An auto-generated name should be provided as follows:
!  Assign FEATURE the next available FEATURE_# name, staring with # = #_existing_features + 1
!
! Arguments: None
!
! Note: This test heavily relies on the fact that RESULT will try to resolve the value given to a 
! variable name and give its value, but if no match is found it will assume the value given is a 
! String. The RESULT will be of type O (Object) if the feature name is found, but of type String
! if it is not
!
! Issue:   NXM-2761 FEATURE: NullPointerException if NAME= and/or <TABLE> are omitted
! Since:   NeXtMidas 3.5.1
! Author:  J. McCall
!******************************************************************************!
startmacro
switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir

  info "!!!!!SHOULD SEE 7 NO UNIQUE NAME WARNINGS BELOW!!!!!"
  pipe init

  ! Test that trying to add a feature to the MAP plot before it exists does not cause an error.  It
  ! just does not get attached to the plot
  feature ,,,, "MAP"

  !
  ! TEST adding FEATUES without a TABLE NAME to a plot
  !

  plot/id=map ^{datDir}.world.prm VIEW=LatLon 
  feature ,, "LABELNAME" ,, "MAP"             ! FEATURE with LABEL but no TABLE
  feature ,, "LABELNAME2" {COLOR=RED} "MAP"   ! FEATURE with LABEL but TABLE does not have NAME
  feature ,,, {COLOR=RED} "MAP"               ! FEATURE without LABEL and TABLE does not have NAME
  feature ,,,, "MAP"                          ! FEATURE without LABEL or TABLE

  ! Get FEATUREs KeyVector table
  res myfeatures reg.map.features

  ! FEATURE with LABEL but no TABLE: uses label name, since it is unique
  res name1 reg.map.features.LABELNAME
  assert/text="^reg.main.name FEATURE has LABEL but TABLE has no NAME" name1 NTYPEOF "S"

  ! FEATURE with LABEL but TABLE does not have NAME: uses label name, since it is unique
  res name2 reg.map.features.LABELNAME2
  assert/text="^reg.main.name FEATURE has LABEL but not TABLE" name2 NTYPEOF "S"

  ! FEATURE without LABEL and TABLE does not have NAME: name default to FEATURE_3
  res name3 reg.map.features.FEATURE_3
  assert/text="^reg.main.name FEATURE with no LABEL and TABLE has no NAME" name3 NTYPEOF "S"

  ! FEATURE without LABEL or TABLE: FEATURE NAME: FEATURE name should default to FEATURE_4
  res name4 reg.map.features.FEATURE_4
  assert/text="^reg.main.name FEATURE with no LABEL and no TABLE" name4 NTYPEOF "S"

  ! Insert a named FEATURE that's name does not conflict
  feature ,,, {NAME=NAMED_FEAT,COLOR=RED} "MAP" 

  ! Insert a named FEATURE that's name WILL CONFLICT  
  feature ,,, {NAME=FEATURE_8,COLOR=RED} "MAP"

  ! Then add more FEATURES without  names
  ! FEATUREs without LABEL (or TABLE NAME) should be given distinct FEATURE_# names
  DO count 7 10 1
      feature ,,,, "MAP"
  ENDDO

  ! FEATURE_5 FEATURE_6 names will be skipped, because the 5th & 6th FEATUREs are named features
  res name5 reg.map.features.FEATURE_5
  assert/text="^reg.main.name Inserted FEATURE causes skip in FEATURE_#" name5 TYPEOF "S"

  ! FEATURE_11 will be reached because the name FEATURE_8 was already used and will be skipped
  res name11 reg.map.features.FEATURE_11
  assert/text="^reg.main.name Increment to next FEATURE_# when name in use" name11 NTYPEOF "S"

  ! Add a FEATURE with a LABEL that is already being used as a NAME
  feature ,, "NAMED_FEAT" ,, "MAP" 
  res name12 reg.map.features.FEATURE_12
  assert/text="^reg.main.name LABEL name already in use as FEATURE name" name12 NTYPEOF "S"

  pipe stop
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg

  ! do nothing
  say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"

return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
procedure open
  ! Automatically called
return
