Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_MemberBind.cs
1 //
2 // ExpressionTest_MemberBind.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Linq;
33 using System.Linq.Expressions;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Linq.Expressions {
38
39         [TestFixture]
40         public class ExpressionTest_MemberBind {
41
42                 public class Foo {
43                         public string Bar;
44                         public string Baz;
45
46                         public Gazonk Gaz;
47
48                         public Gazonk Gazoo { get; set; }
49
50                         public string Gruik { get; set; }
51
52                         public Foo ()
53                         {
54                                 Gazoo = new Gazonk ();
55                                 Gaz = new Gazonk ();
56                         }
57                 }
58
59                 public class Gazonk {
60                         public string Tzap;
61
62                         public int Klang;
63
64                         public string Couic { get; set; }
65
66                         public string Bang () { return ""; }
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void NullMethod ()
72                 {
73                         Expression.MemberBind (null as MethodInfo, new MemberBinding [0]);
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentNullException))]
78                 public void NullMember ()
79                 {
80                         Expression.MemberBind (null as MemberInfo, new MemberBinding [0]);
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentNullException))]
85                 public void NullBindings ()
86                 {
87                         Expression.MemberBind (typeof (Foo).GetField ("Bar"), null);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentException))]
92                 public void MemberNotFieldOrProp ()
93                 {
94                         Expression.MemberBind (typeof (Gazonk).GetMethod ("Bang") as MemberInfo, new MemberBinding [0]);
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (ArgumentException))]
99                 public void MemberTypeMismatch ()
100                 {
101                         Expression.MemberBind (typeof (Gazonk).GetField ("Klang"), Expression.Bind (typeof (Foo).GetField ("Bar"), "bar".ToConstant ()));
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (ArgumentException))]
106                 public void MethodNotPropertyAccessor ()
107                 {
108                         Expression.MemberBind (typeof (Gazonk).GetMethod ("Bang"), new MemberBinding [0]);
109                 }
110
111                 [Test]
112                 public void MemberBindToField ()
113                 {
114                         var mb = Expression.MemberBind (typeof (Foo).GetField ("Gaz"),
115                                 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
116
117                         Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
118                         Assert.AreEqual ("Gaz = {Tzap = \"tzap\"}", mb.ToString ());
119                 }
120
121                 [Test]
122                 public void MemberBindToProperty ()
123                 {
124                         var mb = Expression.MemberBind (typeof (Foo).GetProperty ("Gazoo"),
125                                 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
126
127                         Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
128                         Assert.AreEqual ("Gazoo = {Tzap = \"tzap\"}", mb.ToString ());
129                 }
130
131                 [Test]
132                 public void MemberBindToPropertyAccessor ()
133                 {
134                         var mb = Expression.MemberBind (typeof (Foo).GetProperty ("Gazoo").GetSetMethod (true),
135                                 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
136
137                         Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
138                         Assert.AreEqual ("Gazoo = {Tzap = \"tzap\"}", mb.ToString ());
139                 }
140
141                 [Test]
142                 public void CompiledMemberBinding ()
143                 {
144                         var getfoo = Expression.Lambda<Func<Foo>> (
145                                 Expression.MemberInit (
146                                         Expression.New (typeof (Foo)),
147                                         Expression.MemberBind (
148                                                 typeof (Foo).GetProperty ("Gazoo"),
149                                                 Expression.Bind (typeof (Gazonk).GetField ("Tzap"),
150                                                         "tzap".ToConstant ()),
151                                                 Expression.Bind (typeof (Gazonk).GetField ("Klang"),
152                                                         42.ToConstant ())))).Compile ();
153
154                         var foo = getfoo ();
155
156                         Assert.IsNotNull (foo);
157                         Assert.AreEqual ("tzap", foo.Gazoo.Tzap);
158                         Assert.AreEqual (42, foo.Gazoo.Klang);
159                 }
160         }
161 }