###############################################################################
#
# object7.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]

###############################################################################
# MONO: Due to a Mono crashing bug, some tests are disabled on Mono.
#       See: https://bugzilla.novell.com/show_bug.cgi?id=471259
#       See: https://bugzilla.novell.com/show_bug.cgi?id=490932
###############################################################################

runTest {test object-7.1 {objects with array and nullable arguments} -body {
  list [set x [object create Eagle._Tests.Default]] [llength [info objects]] \
      [set y 0] [object invoke $x TestByRefValueTypeMethod y] $y \
      [object invoke $x TestNullableValueTypeMethod $null] \
      [object invoke $x TestByRefNullableValueTypeMethod y] $y \
      [object invoke $x TestIntArrayReturnValue] \
      [object invoke $x TestStringArrayReturnValue] \
      [object invoke $x TestStringListReturnValue] \
      [object invoke $x TestStringListArrayReturnValue] \
      [set z(0) foo] [object invoke -marshalflags StringList $x \
      TestByRefStringListArray z] [testArrayGet z true] [unset z] [unset y] \
      [object dispose $x] [unset x] [llength [info objects]]
} -cleanup {
  unset -nocomplain x y z
} -constraints {eagle compile.TEST refCount monoCrash20 monoBug24\
Eagle._Tests.Default.TestByRefValueTypeMethod\
Eagle._Tests.Default.TestNullableValueTypeMethod\
Eagle._Tests.Default.TestByRefNullableValueTypeMethod\
Eagle._Tests.Default.TestIntArrayReturnValue\
Eagle._Tests.Default.TestStringArrayReturnValue\
Eagle._Tests.Default.TestStringListReturnValue\
Eagle._Tests.Default.TestStringListArrayReturnValue\
Eagle._Tests.Default.TestByRefStringListArray} -match regexp \
-result {^Default#\d+ 2 0 \{\} 1 \{\} \{\} 2 \{1 2 3 4 5\} \{1 2 joe jim tom\}\
\{1 2 joe jim tom\} \{\{1 2 joe jim tom\} \{\{hello world\}\} \{\{;\}\}\
\{\\\\\} \{\\\{\}\} foo \{\} \{0 \{1 2 joe jim tom\} 1 \{\{hello world\}\} 2\
\{\{;\}\} 3 \{\\\\\} 4 \{\\\{\}\} \{\} \{\} disposed \{\} 1$}}

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

runTest {test object-7.2 {scalar variable marshal of array types} -setup {
  unset -nocomplain array1 array2 array3
} -body {
  array set array1 {0 1 1 2 2 3}
  array set array2 {}
  set array3 null

  list [object invoke Eagle._Tests.Default TestNullArray null] \
      [object invoke Eagle._Tests.Default TestNullArray array1] \
      [testArrayGet array1 true] \
      [object invoke Eagle._Tests.Default TestNullArray array2] \
      [testArrayGet array2 true] \
      [object invoke Eagle._Tests.Default TestNullArray array3] \
      [testArrayGet array3 true] \
      [object invoke Eagle._Tests.Default TestOutArray true array1] \
      [testArrayGet array1 true] \
      [object invoke Eagle._Tests.Default TestOutArray true array2] \
      [testArrayGet array2 true] \
      [object invoke Eagle._Tests.Default TestOutArray true array3] \
      [testArrayGet array3 true]
} -cleanup {
  unset -nocomplain array1 array2 array3
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestNullArray\
Eagle._Tests.Default.TestOutArray} -result {-1 6 {0 1 1 2 2 3} 0 {} -1 {} True\
{0 1 1 2 2 3} True {0 1 1 2 2 3} True {0 1 1 2 2 3}}}

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

#
# HACK: *MONO* This test is disabled on Mono due to internal implementation
#       details regarding how objects are serialized.
#
runTest {test object-7.3 {scalar variable null object to array} -setup {
  unset -nocomplain int32 bytes result error
} -body {
  set int32 [object create Int32]

  set bytes null
  set error null

  list [object invoke Eagle._Components.Private.XmlOps Serialize $int32 Int32 \
      null bytes error] [catch {testArrayGet bytes true} result] $result \
      $error [object invoke Eagle._Components.Private.XmlOps Serialize $int32 \
      Int32 null bytes error] [catch {testArrayGet bytes true} result] $result \
      [catch {object invoke $error ToString} result] $result
} -cleanup {
  unset -nocomplain int32 bytes result error
} -constraints {eagle dotNet compile.XML} -result {Ok 0 {0 60 1 63 2 120 3 109\
4 108 5 32 6 118 7 101 8 114 9 115 10 105 11 111 12 110 13 61 14 34 15 49 16 46\
17 48 18 34 19 63 20 62 21 13 22 10 23 60 24 105 25 110 26 116 27 62 28 48 29\
60 30 47 31 105 32 110 33 116 34 62} {} Error 0 {0 60 1 63 2 120 3 109 4 108 5\
32 6 118 7 101 8 114 9 115 10 105 11 111 12 110 13 61 14 34 15 49 16 46 17 48\
18 34 19 63 20 62 21 13 22 10 23 60 24 105 25 110 26 116 27 62 28 48 29 60 30\
47 31 105 32 110 33 116 34 62} 0 {cannot overwrite valid byte array}}}

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

runTest {test object-7.4 {scalar variable marshal of input array} -setup {
  unset -nocomplain int32 array1 array2 array3 error
} -body {
  set int32 [object invoke -create Int32 Parse 123]

  set array1 null
  set array2 [object create -alias Int32\[\] 0]
  set array3 [object create -alias Int32\[\] 3]

  $array3 SetValue $int32 0
  $array3 SetValue $int32 1
  $array3 SetValue $int32 2

  list [object invoke Eagle._Tests.Default TestNullArray null] \
      [object invoke Eagle._Tests.Default TestNullArray array1] \
      [object invoke Eagle._Tests.Default TestNullArray array2] \
      [object invoke Eagle._Tests.Default TestNullArray array3] \
      [catch {object invoke Eagle._Tests.Default TestOutArray true array1} \
      error] [testArrayGet array1 true] $error [catch {object invoke \
      Eagle._Tests.Default TestOutArray true array2} error] $error \
      [catch {object invoke Eagle._Tests.Default TestOutArray true array3} \
      error] $error
} -cleanup {
  unset -nocomplain int32 array1 array2 array3 error
} -constraints {eagle compile.TEST Eagle._Tests.Default.TestNullArray\
Eagle._Tests.Default.TestOutArray} -match regexp -result {^-1 -1 0 369 0 \{0 1\
1 2 2 3\} True 1 \{could not convert method #\d+\
"Eagle\._Tests\.Default\.TestOutArray" argument #1 "array" with value "array2"\
to type "System\.Int32\[\]&": cannot convert scalar variable containing object\
of type "System\.Int32\[\]" to output array of type "System\.Int32\[\]&"\} 1\
\{could not convert method #\d+ "Eagle\._Tests\.Default\.TestOutArray" argument\
#1 "array" with value "array3" to type "System\.Int32\[\]&": cannot convert\
scalar variable containing object of type "System\.Int32\[\]" to output array\
of type "System\.Int32\[\]&"\}$}}

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

runTest {test object-7.5 {Enum, ByRef, and Nullable} -setup {
  unset -nocomplain w x y z script msg
} -body {
  set script {
    set w null
    set x Ok
    set y Break
    set z Error
    list $w $x $y $z
  }; eval $script

  list [catch {object invoke Eagle._Tests.Default TestEnum $w} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestEnum $x} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestEnum $y} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestEnum $z} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefEnum w} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefEnum x} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefEnum y} msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefEnum z} msg] $msg \
      [eval $script] \
      [catch {object invoke Eagle._Tests.Default TestNullableEnum $w} msg] \
      $msg \
      [catch {object invoke Eagle._Tests.Default TestNullableEnum $x} msg] \
      $msg \
      [catch {object invoke Eagle._Tests.Default TestNullableEnum $y} msg] \
      $msg \
      [catch {object invoke Eagle._Tests.Default TestNullableEnum $z} msg] \
      $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefNullableEnum w} \
          msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefNullableEnum x} \
          msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefNullableEnum y} \
          msg] $msg \
      [catch {object invoke Eagle._Tests.Default TestByRefNullableEnum z} \
          msg] $msg [eval $script]
} -cleanup {
  unset -nocomplain w x y z script msg
} -constraints {eagle monoBug22 compile.TEST Eagle._Tests.Default.TestEnum\
Eagle._Tests.Default.TestByRefEnum Eagle._Tests.Default.TestNullableEnum\
Eagle._Tests.Default.TestByRefNullableEnum} -match regexp -result \
{^1 \{could not convert method #\d+ "Eagle\._Tests\.Default\.TestEnum" argument\
#0 "x" with value "null" to type "Eagle\._Components\.Public\.ReturnCode":\
argument of value type "Eagle\._Components\.Public\.ReturnCode" cannot be\
null\} 0 True 0 False 0 False 0 True 0 True 0 False 0 False \{null Ok Break\
Error\} 0 False 0 True 0 True 0 True 0 False 0 True 0 True 0 True \{null Ok\
Break Error\}$}}

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

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