# -*- Tcl -*-

package prefer latest

package require nx
::nx::configure defaultMethodCallProtection false
package require nx::test

namespace eval ::nx::var1 {
  namespace ensemble create -map {
    exists ::nsf::var::exists 
    import ::nsf::var::import 
    set ::nsf::var::set
  }
}

::nx::Object create ::nx::var2 {
  :object alias exists ::nsf::var::exists 
  :object alias import ::nsf::var::import
  :object alias set ::nsf::var::set
}


nx::test case set+array {
  nx::Object create o1
  
  # first set a scalar variable
  ? {nsf::var::set o1 x 100} "100"
  ? {nsf::var::set o1 x} "100"

  # now, set an array variable; "nsf::var::set -array" is a wrapper
  # around "array set" or "array get"
  ? {nsf::var::set -array o1 a {a 1 y 2}} ""
  ? {nsf::var::set -array o1 a} "y 2 a 1"

  # We have now a scalar and an array variable set.
  ? {lsort [o1 info vars]} "a x"

  # "x" is a variable, but not an array
  ? {nsf::var::exists o1 x} 1
  ? {nsf::var::exists -array o1 x} 0
  
  # "a" is a variable and an array
  ? {nsf::var::exists -array o1 a} 1
  ? {nsf::var::exists o1 a} 1

  # we unset the array
  ? {nsf::var::unset o1 a} ""
  ? {nsf::var::exists o1 a} 0
  ? {nsf::var::exists -array o1 a} 0

  # now, just the scalar is left
  ? {o1 info vars} "x"
  ? {nsf::var::exists o1 x} 1
  ? {nsf::var::exists -array o1 x} 0

  # we unset the scalar
  ? {nsf::var::unset o1 x} ""
  ? {nsf::var::exists o1 x} 0
  ? {nsf::var::exists -array o1 x} 0

  # unset on a non-existing variable
  ? {nsf::var::unset o1 x} {can't unset "x": no such variable}
  ? {nsf::var::unset -nocomplain o1 x} ""
}

nx::test configure -count 10000
nx::test case dummy {
  nx::Object create o {
    set :x 1
  }
  nx::Object create p {
    set :y 1
    :object method foo0 {} {
      incr :y
    }
    :object method foo1 {} {
      o eval {incr :x}
    }
    :object method foo2 {} {
      ::nsf::var::import o x
      incr x
    }
    :object method foo3 {} {
      ::nx::var1 import o x
      incr x
    }
    :object method foo4 {} {
      ::nx::var2 import o x
      incr x
    }
  }
  
  ? {::nsf::var::set o x} 1
  ? {::nsf::var::exists o x} 1
  ? {::nsf::var::exists o y} 0

  ? {::nx::var1 set o x} 1
  ? {::nx::var1 exists o x} 1
  ? {::nx::var1 exists o y} 0

  ? {::nx::var2 set o x} 1
  ? {::nx::var2 exists o x} 1
  ? {::nx::var2 exists o y} 0

  ? {p foo0} 2

  ? {p foo1} 2
  ? {::nsf::var::set o x} 10002

  ? {p foo2} 10003
  ? {::nsf::var::set o x} 20003

  ? {p foo3} 20004
  ? {::nsf::var::set o x} 30004

  ? {p foo4} 30005
  ? {::nsf::var::set o x} 40005
}


#
# Local variables:
#    mode: tcl
#    tcl-indent-level: 2
#    indent-tabs-mode: nil
# End: