###############################################################################
#
# object3.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 object-3.1 {object usage with indexed properties} -body {
  list [set x [object create Eagle._Tests.Default]] [object invoke $x Item 0] \
      [object invoke $x Item 0 1] [object invoke $x Item 0 "foo"] [object \
      invoke $x Item 0 "foo" 1] [object dispose $x]
} -cleanup {
  unset -nocomplain x
} -constraints {eagle compile.TEST Eagle._Tests.Default.get_Item\
Eagle._Tests.Default.set_Item} -match regexp -result \
{^Default#\d+ 0 \{\} 1 \{\} disposed$}}

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

runTest {test object-3.2 {simple object invokeall} -body {
  set x [object create System.Int32]; # NOTE: Immutable zero.

  set cmd1 {Format this}
  set cmd2 {Format that}

  list [object invokeall System.String $cmd1 $cmd2] \
      [object invokeall -keepresults System.String $cmd1 $cmd2] \
      [object invokeall -invoke System.String Format this] \
      [object invokeall $x ToString ToString] \
      [object invokeall -keepresults $x ToString ToString] \
      [object invokeall -invoke $x ToString] \
      [catch {object invokeall -nocomplain System.String {Foo}} msg1] $msg1 \
      [catch {object invokeall -nocomplain $x {Foo}} msg2] $msg2
} -cleanup {
  unset -nocomplain msg2 msg1 cmd2 cmd1 x
} -constraints {eagle} -result \
{{Ok 0} {Ok this Ok that} this {Ok 0} {Ok 0 Ok 0} 0 0 {Error 1} 0 {Error 1}}}

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

runTest {test object-3.3 {-alias, -aliasall, -invoke, and -invokeall} -body {
  set x [object create -alias System.Int32]; # NOTE: Immutable zero.
  set y [object create -alias -aliasall System.Int32]; # NOTE: Immutable zero.

  list [$x ToString] \
      [$x ToString X] \
      [$x -invoke ToString X] \
      [$x -invokeall ToString ToString ToString] \
      [$x -invokeall -keepresults ToString ToString ToString] \
      [catch {$x ToString ToString ToString} msg1] $msg1 \
      [$y ToString] \
      [$y -keepresults ToString] \
      [$y -invoke ToString X] \
      [$y -invoke -keepresults ToString X] \
      [$y ToString ToString ToString] \
      [$y -keepresults ToString ToString ToString] \
      [$y -invokeall ToString ToString ToString] \
      [$y -invokeall -keepresults ToString ToString ToString] \
      [catch {$y ToString X} msg2] $msg2
} -cleanup {
  unset -nocomplain msg2 msg1 y x
} -constraints {eagle} -match regexp -result \
{^0 0 0 \{Ok 0\} \{Ok 0 Ok 0 Ok 0\} 1 \{method #\d+\
"System#Int32#\d+\.ToString" requires at most 0 arguments and 2 were supplied\}\
\{Ok 0\} \{Ok 0\} 0 0 \{Ok 0\} \{Ok 0 Ok 0 Ok 0\} \{Ok 0\} \{Ok 0 Ok 0 Ok 0\} 1\
\{type "System\.Int32" member "X" matching member types "Field, Method,\
Property" and flags "Instance, Static, Public, FlattenHierarchy" not found\}$}}

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

runTest {test object-3.4 {object type name lookups} -body {
  object type dict [appendArgs System.Collections.Generic.Dictionary \
      `2\[System.String,System.String\]]

  list [object types] [set x [object create -alias dict]] [$x Add foo bar] \
      [$x Count] [unset x]
} -cleanup {
  object untype -typepattern dict

  unset -nocomplain x
} -constraints {eagle} -match regexp -result {^\{dict\
\{System\.Collections\.Generic\.Dictionary`2\[System\.String,System\.String\]\}\}\
\{System#Collections#Generic#Dictionary`2\[\[System#String, mscorlib,\
Version=\d+#0#0#0, Culture=neutral,\
PublicKeyToken=b77a5c561934e089\],\[System#String, mscorlib, Version=\d+#0#0#0,\
Culture=neutral, PublicKeyToken=b77a5c561934e089\]\]#\d+\} \{\} 1 \{\}$}}

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

runTest {test object-3.5 {object create/invoke with method index} -body {
  list [catch {object invoke -index -1 Int32 Parse 123} result] $result \
      [catch {object invoke -index 0 Int32 Parse 123} result] $result \
      [catch {object invoke -index 1 Int32 Parse 123} result] $result \
      [catch {object create -index -1 Int32} result] $result \
      [catch {object create -index 0 Int32} result] $result \
      [catch {object create -index 1 Int32} result] $result \
      [catch {object invoke -index -1 String Compare foo foo} result] $result \
      [catch {object invoke -index 0 String Compare foo foo} result] $result \
      [catch {object invoke -index 1 String Compare foo foo} result] $result \
      [catch {object create -index -1 String foo} result] $result \
      [catch {object create -index 0 String foo} result] $result \
      [catch {object create -index 1 String foo} result] $result
} -cleanup {
  unset -nocomplain result
} -constraints {eagle} -match regexp -result \
{0 123 0 123 1 \{method "Parse" of object "Int32" not found, invalid method\
index 1, must be 0\} 0 System#Int32#\d+ 1 \{type "System\.Int32" constructor\
not found, cannot specify method index 0 for primitive type\} 1 \{type\
"System\.Int32" constructor not found, cannot specify method index 1 for\
primitive type\} 0 0 0 0 1 \{method "Compare" of object "String" not found,\
invalid method index 1, must be 0\} 0 System#String#\d+ 0 System#String#\d+ 1\
\{type "System\.String" constructor not found, invalid method index 1, must be\
0\}}}

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

runTest {test object-3.6 {nested list "good" return values} -body {
  list [set x [object create Eagle._Tests.Default]] \
      [set y [object invoke $x TestStringIListReturnValue true 1 2 3 \
          "foo bar"]] \
      [set z [object invoke $x TestStringIListIListIListReturnValue \
          true 1 2 3 "foo bar"]]
} -cleanup {
  unset -nocomplain x y z
} -constraints {eagle compile.TEST\
Eagle._Tests.Default.TestStringIListReturnValue\
Eagle._Tests.Default.TestStringIListIListIListReturnValue} -match regexp \
-result {^Default#\d+ \{1 2 3 \{foo bar\}\} \{\{\{1 2 3 \{foo bar\}\} \{1 2 3\
\{foo bar\}\} \{1 2 3 \{foo bar\}\}\} \{\{1 2 3 \{foo bar\}\} \{1 2 3 \{foo\
bar\}\} \{1 2 3 \{foo bar\}\}\} \{\{1 2 3 \{foo bar\}\} \{1 2 3 \{foo bar\}\}\
\{1 2 3 \{foo bar\}\}\}\}$}}

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

runTest {test object-3.7 {nested list "bad" return values} -body {
  list [set x [object create Eagle._Tests.Default]] \
      [set y [object invoke $x TestStringIListReturnValue false 1 2 3 \
          "foo bar"]] \
      [set z [object invoke $x TestStringIListIListIListReturnValue \
          false 1 2 3 "foo bar"]]
} -cleanup {
  unset -nocomplain x y z
} -constraints {eagle compile.TEST\
Eagle._Tests.Default.TestStringIListReturnValue\
Eagle._Tests.Default.TestStringIListIListIListReturnValue} -match regexp \
-result {^Default#\d+\
\{System\.Collections\.Generic\.List`1\[System.String\]\}\
\{System\.Collections\.Generic\.List`1\[System\.Collections\.Generic\.IList`1\[System\.Collections\.Generic\.IList`1\[System\.String\]\]\]\}$}}

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

runTest {test object-3.8 {implicit fixup of List<byte>} -body {
  set x [object invoke Eagle._Tests.Default TestByteList]
} -cleanup {
  unset -nocomplain x
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestByteList} -result \
{1 2 3 4 5 6 7 8 9 10}}

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

runTest {test object-3.9 {implicit fixup of List<int>} -body {
  set x [object invoke Eagle._Tests.Default TestIntList]
} -cleanup {
  unset -nocomplain x
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestIntList} -result \
{1 2 3 4 5 6 7 8 9 10}}

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

runTest {test object-3.10 {implicit fixup of List<long>} -body {
  set x [object invoke Eagle._Tests.Default TestLongList]
} -cleanup {
  unset -nocomplain x
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestLongList} -result \
{1 2 3 4 5 6 7 8 9 10}}

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

runTest {test object-3.11 {fixup infinite recursion prevention} -body {
  set x [object invoke Eagle._Tests.Default TestDerivedList]
} -cleanup {
  unset -nocomplain x
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestDerivedList} \
-match regexp -result {^DerivedList#\d+$}}

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

runTest {test object-3.12 {generic non-list return values} -body {
  list [set x [object create Eagle._Tests.Default]] \
      [set y [object invoke $x TestStringIDictionaryReturnValue false 1 2 3 \
          "foo bar"]] \
      [set z [object invoke $x TestStringIDictionaryReturnValue true 1 2 3 \
          "foo bar"]]
} -cleanup {
  unset -nocomplain x y z
} -constraints {eagle compile.TEST\
Eagle._Tests.Default.TestStringIDictionaryReturnValue} -match regexp -result \
{^Default#\d+ \{System#Collections#Generic#Dictionary`2\[\[System#String,\
mscorlib, Version=\d+#0#0#0, Culture=neutral,\
PublicKeyToken=b77a5c561934e089\],\[System#String, mscorlib, Version=\d+#0#0#0,\
Culture=neutral, PublicKeyToken=b77a5c561934e089\]\]#\d+\}\
GenericDictionary`2#\d+$}}

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

runTest {test object-3.13 {generic non-list return values} -body {
  set stringType {System.String, mscorlib, Version=2.0.0.0, Culture=neutral,\
      PublicKeyToken=b77a5c561934e089}

  set type [appendArgs "Eagle._Tests.Default+GenericDictionary`2\[\[" \
      $stringType "\],\[" $stringType "\]\]"]

  list [set x [object create StringList one two three four]] \
      [set y [object create StringList 1 2 3 4]] \
      [set z [object create $type $x $y]] [lsort [object invoke $z ToString]]
} -cleanup {
  unset -nocomplain stringType type x y z
} -constraints {eagle compile.TEST\
Eagle._Tests.Default.TestStringIDictionaryReturnValue} -match regexp -result \
{^StringList#\d+ StringList#\d+ GenericDictionary`2#\d+ \{\{four 4\} \{one 1\}\
\{three 3\} \{two 2\}\}$}}

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

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