2 // ExpressionTest_MemberBind.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
31 using System.Reflection;
33 using System.Linq.Expressions;
35 using NUnit.Framework;
37 namespace MonoTests.System.Linq.Expressions {
40 public class ExpressionTest_MemberBind {
48 public Gazonk Gazoo { get; set; }
50 public string Gruik { get; set; }
54 Gazoo = new Gazonk ();
64 public string Couic { get; set; }
66 public string Bang () { return ""; }
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void NullMethod ()
73 Expression.MemberBind (null as MethodInfo, new MemberBinding [0]);
77 [ExpectedException (typeof (ArgumentNullException))]
78 public void NullMember ()
80 Expression.MemberBind (null as MemberInfo, new MemberBinding [0]);
84 [ExpectedException (typeof (ArgumentNullException))]
85 public void NullBindings ()
87 Expression.MemberBind (typeof (Foo).GetField ("Bar"), null);
91 [ExpectedException (typeof (ArgumentException))]
92 public void MemberNotFieldOrProp ()
94 Expression.MemberBind (typeof (Gazonk).GetMethod ("Bang") as MemberInfo, new MemberBinding [0]);
98 [ExpectedException (typeof (ArgumentException))]
99 public void MemberTypeMismatch ()
101 Expression.MemberBind (typeof (Gazonk).GetField ("Klang"), Expression.Bind (typeof (Foo).GetField ("Bar"), "bar".ToConstant ()));
105 [ExpectedException (typeof (ArgumentException))]
106 public void MethodNotPropertyAccessor ()
108 Expression.MemberBind (typeof (Gazonk).GetMethod ("Bang"), new MemberBinding [0]);
112 public void MemberBindToField ()
114 var mb = Expression.MemberBind (typeof (Foo).GetField ("Gaz"),
115 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
117 Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
118 Assert.AreEqual ("Gaz = {Tzap = \"tzap\"}", mb.ToString ());
122 public void MemberBindToProperty ()
124 var mb = Expression.MemberBind (typeof (Foo).GetProperty ("Gazoo"),
125 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
127 Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
128 Assert.AreEqual ("Gazoo = {Tzap = \"tzap\"}", mb.ToString ());
132 public void MemberBindToPropertyAccessor ()
134 var mb = Expression.MemberBind (typeof (Foo).GetProperty ("Gazoo").GetSetMethod (true),
135 Expression.Bind (typeof (Gazonk).GetField ("Tzap"), "tzap".ToConstant ()));
137 Assert.AreEqual (MemberBindingType.MemberBinding, mb.BindingType);
138 Assert.AreEqual ("Gazoo = {Tzap = \"tzap\"}", mb.ToString ());
142 public void CompiledMemberBinding ()
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 ();
156 Assert.IsNotNull (foo);
157 Assert.AreEqual ("tzap", foo.Gazoo.Tzap);
158 Assert.AreEqual (42, foo.Gazoo.Klang);