###############################################################################
#
# namespace-delete.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################

runTest {test namespace-97.1 {delete global namespace from proc} -setup {
  proc foo {} {namespace delete ::}
} -body {
  foo

  if {[object invoke Interpreter IsPendingCleanup ""]} then {
    object invoke -flags +NonPublic -default Interpreter.GetActive \
        DeleteNamespace None "" true result
  }

  info exists dummy
} -constraints {eagle} -isolationLevel Interpreter -returnCodes 1 -result \
{invalid command name "info"}}

###############################################################################

runTest {test namespace-97.2 {delete global namespace, vwait scalar} -body {
  set x 1

  after 1000 [list namespace delete ::]

  after 2000 {
    if {[object invoke Interpreter IsPendingCleanup ""]} then {
      object invoke -flags +NonPublic -default Interpreter.GetActive \
          DeleteNamespace None "" true result
    }
  }

  vwait x
} -constraints {eagle} -isolationLevel Interpreter -result {}}

###############################################################################

runTest {test namespace-97.3 {delete global namespace, vwait element} -body {
  set x(1) 1

  after 1000 [list namespace delete ::]
  after 2000 [list set x(2) 1]

  after 3000 {
    if {[object invoke Interpreter IsPendingCleanup ""]} then {
      object invoke -flags +NonPublic -default Interpreter.GetActive \
          DeleteNamespace None "" true result
    }
  }

  vwait x(1); info exists x(1)
} -constraints {eagle} -isolationLevel Interpreter -returnCodes 1 -result \
{invalid command name "info"}}

###############################################################################

runTest {test namespace-97.4 {delete global namespace, references} -setup {
  unset -nocomplain i x v error
  set i [interp create]
} -body {
  set v null; set error null
  list [set x [interp eval $i {set x [object create Int32]}]] \
      [interp eval $i {namespace delete ::}] \
      [object invoke -alias Value GetInterpreter "" $i Default v error] \
      [$v DoesObjectExist $x]
} -cleanup {
  catch {interp delete $i}
  unset -nocomplain i x v error
} -constraints {eagle} -match regexp -result {^System#Int32#\d+ \{\} Ok Error$}}

###############################################################################

runTest {test namespace-97.5 {delete child namespace w/recursion} -body {
  namespace eval ::foo {}
  namespace eval ::foo::bar {}
  namespace eval ::foo::baz {}
  namespace eval ::foo::bar::splog {}
  namespace delete ::foo
  namespace children :: foo
} -cleanup {
  catch {namespace delete ::foo}
} -constraints {namespaces} -result {}}

###############################################################################

runTest {test namespace-97.6 {listing deleted children} -body {
  namespace eval ::foo::bar {
    namespace delete ::foo
    namespace children :: foo
  }
} -cleanup {
  catch {namespace delete ::foo}
} -constraints {namespaces} -result {}}

###############################################################################

runTest {test namespace-97.7 {namespace enable/disable with procs} -setup {
  unset -nocomplain result
  set savedNamespaces [namespace enable]
} -body {
  lappend result(2) [namespace enable false]
  catch {rename foo::bar ""}

  lappend result(2) [catch {foo::bar} result(1)] $result(1)

  proc foo::bar {} {
    return [list [namespace current] [info level [info level]]]
  }

  lappend result(2) [catch {foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable true]
  lappend result(2) [catch {foo::bar} result(1)] $result(1)
  lappend result(2) [namespace eval foo {}]
  lappend result(2) [catch {foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable false]
  lappend result(2) [catch {foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable true]
  lappend result(2) [catch {foo::bar} result(1)] $result(1)
} -cleanup {
  catch {namespace enable false; rename foo::bar ""}
  catch {namespace enable true; namespace delete ::foo}

  namespace enable $savedNamespaces
  unset -nocomplain savedNamespaces

  unset -nocomplain result
} -constraints {eagle namespaces.available} -result {False 1 {invalid command\
name "foo::bar"} 0 {:: foo::bar} True 0 {:: foo::bar} {} 0 {::foo foo::bar}\
False 0 {:: foo::bar} True 0 {::foo foo::bar}}}

###############################################################################

runTest {test namespace-97.8 {namespace enable/disable with vars} -setup {
  unset -nocomplain result
  set savedNamespaces [namespace enable]
} -body {
  lappend result(2) [namespace enable false]
  catch {unset -nocomplain foo::bar}

  lappend result(2) [catch {set foo::bar} result(1)] $result(1)

  set foo::bar 1

  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable true]
  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
  lappend result(2) [namespace eval foo {}]
  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
  lappend result(2) [namespace eval foo {set bar 2}]
  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable false]
  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
  lappend result(2) [namespace enable true]
  lappend result(2) [catch {set foo::bar} result(1)] $result(1)
} -cleanup {
  catch {namespace enable false; unset -nocomplain foo::bar}
  catch {namespace enable true; namespace delete ::foo}

  namespace enable $savedNamespaces
  unset -nocomplain savedNamespaces

  unset -nocomplain result
} -constraints {eagle namespaces.available} -result {False 1 {can't read\
"foo::bar": no such variable} 0 1 True 1 {namespace "foo" not found in "::"} {}\
1 {can't read "foo::bar": no such variable} 2 0 2 False 0 1 True 0 2}}

###############################################################################

runTest {test namespace-97.9 {object load -namespace} -setup {
  set i [interp create]

  interp eval $i {
    namespace enable true
    namespace eval foo {}
  }
} -body {
  interp eval $i {
    set assembly [object load -namespace foo mscorlib]; unset assembly

    set x [object create -alias Int32]

    list [[appendArgs ::foo:: $x] ToString] [info commands foo::*] \
        [info objects] [unset x] [info commands foo::*] [info objects]
  }
} -cleanup {
  catch {interp delete $i}

  unset -nocomplain i
} -constraints {eagle namespaces.available} -match regexp -result \
{^0 ::foo::System#Int32#\d+ \{null System#Int32#\d+\} \{\} \{\} null$}}

###############################################################################

runTest {test namespace-97.10 {namespace rename (no cmds/vars)} -setup {
  set i [interp create -namespaces -noinitialize -novariables]

  interp eval $i {
    variable g 0
    proc g1 {} {}
    namespace eval foo {
      variable f 1
      proc f1 {} {}
      namespace eval bar {
        variable b 2
        proc b1 {} {}
        namespace eval splog {
          variable s 3
          proc s1 {} {}
        }
      }
    }
  }
} -body {
  set result [list]

  lappend result [interp eval $i {
    set result ""
    list [info procs] [info vars] \
        [catch {namespace children} result] $result \
        [catch {namespace children foo} result] $result \
        [catch {namespace children foo::bar} result] $result \
        [catch {namespace children foo::bar::splog} result] $result \
        [catch {namespace children foo::x} result] $result \
        [catch {namespace children foo::x::splog} result] $result
  }]

  set error null

  set code [object invoke \
      Eagle._Tests.Default TestRenameNamespace $i foo::bar x error]

  if {$code ne "Ok"} then {
    error [object invoke $error ToString]
  }

  lappend result [interp eval $i {
    set result ""
    list [info procs] [info vars] \
        [catch {namespace children} result] $result \
        [catch {namespace children foo} result] $result \
        [catch {namespace children foo::bar} result] $result \
        [catch {namespace children foo::bar::splog} result] $result \
        [catch {namespace children foo::x} result] $result \
        [catch {namespace children foo::x::splog} result] $result
  }]
} -cleanup {
  catch {interp delete $i}

  unset -nocomplain error result code i
} -constraints {eagle namespaces.available compile.TEST\
Eagle._Tests.Default.TestRenameNamespace} -result {{g1 {g result} 0 ::foo 0\
::foo::bar 0 ::foo::bar::splog 0 {} 1 {namespace "foo::x" not found in "::"} 1\
{namespace "foo::x::splog" not found in "::"}} {g1 {g result errorInfo\
errorCode} 0 ::foo 0 ::foo::x 1 {namespace "foo::bar" not found in "::"} 1\
{namespace "foo::bar::splog" not found in "::"} 0 ::foo::x::splog 0 {}}}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
