1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // Federico Di Gregorio <fog@initd.org>
23 using System
.Reflection
;
24 using System
.Collections
.Generic
;
26 using System
.Linq
.Expressions
;
27 using NUnit
.Framework
;
29 namespace MonoTests
.System
.Linq
.Expressions
32 public class ExpressionTest_Bind
35 [ExpectedException (typeof (ArgumentNullException
))]
36 public void Arg1Null ()
38 Expression
.Bind (null, Expression
.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException
))]
43 public void Arg2Null ()
45 Expression
.Bind (MemberClass
.GetRwFieldInfo (), null);
49 [ExpectedException (typeof (ArgumentException
))]
50 public void Method1 ()
52 // This tests the MethodInfo version of Bind(): should raise an exception
53 // because the method is not an accessor.
54 Expression
.Bind (MemberClass
.GetMethodInfo (), Expression
.Constant (1));
58 [ExpectedException (typeof (ArgumentException
))]
59 public void Method2 ()
61 // This tests the MemberInfo version of Bind(): should raise an exception
62 // because the argument is not a field or property accessor.
63 Expression
.Bind ((MemberInfo
)MemberClass
.GetMethodInfo (), Expression
.Constant (1));
67 [ExpectedException (typeof (ArgumentException
))]
70 Expression
.Bind (MemberClass
.GetEventInfo (), Expression
.Constant (1));
74 [ExpectedException (typeof (ArgumentException
))]
75 public void PropertyRo ()
77 Expression
.Bind (MemberClass
.GetRoPropertyInfo (), Expression
.Constant (1));
81 public void FieldRo ()
83 MemberAssignment expr
= Expression
.Bind (MemberClass
.GetRoFieldInfo (), Expression
.Constant (1));
84 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#01");
85 Assert
.AreEqual ("TestField1 = 1", expr
.ToString(), "Bind#02");
89 public void FieldRw ()
91 MemberAssignment expr
= Expression
.Bind (MemberClass
.GetRwFieldInfo (), Expression
.Constant (1));
92 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#03");
93 Assert
.AreEqual ("TestField2 = 1", expr
.ToString(), "Bind#04");
97 public void FieldStatic ()
99 MemberAssignment expr
= Expression
.Bind (MemberClass
.GetStaticFieldInfo (), Expression
.Constant (1));
100 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#05");
101 Assert
.AreEqual ("StaticField = 1", expr
.ToString(), "Bind#06");
105 public void PropertyRw ()
107 MemberAssignment expr
= Expression
.Bind (MemberClass
.GetRwPropertyInfo (), Expression
.Constant (1));
108 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#07");
109 Assert
.AreEqual ("TestProperty2 = 1", expr
.ToString(), "Bind#08");
113 public void PropertyStatic ()
115 MemberAssignment expr
= Expression
.Bind (MemberClass
.GetStaticPropertyInfo (), Expression
.Constant (1));
116 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#09");
117 Assert
.AreEqual ("StaticProperty = 1", expr
.ToString(), "Bind#10");
121 public void PropertyAccessor ()
123 MethodInfo mi
= typeof(MemberClass
).GetMethod("get_TestProperty2");
125 MemberAssignment expr
= Expression
.Bind (mi
, Expression
.Constant (1));
126 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#11");
127 Assert
.AreEqual ("TestProperty2 = 1", expr
.ToString(), "Bind#12");
128 Assert
.AreEqual (MemberClass
.GetRwPropertyInfo(), expr
.Member
, "Bind#13");
132 public void PropertyAccessorStatic ()
134 MethodInfo mi
= typeof(MemberClass
).GetMethod("get_StaticProperty");
136 MemberAssignment expr
= Expression
.Bind (mi
, Expression
.Constant (1));
137 Assert
.AreEqual (MemberBindingType
.Assignment
, expr
.BindingType
, "Bind#14");
138 Assert
.AreEqual ("StaticProperty = 1", expr
.ToString(), "Bind#15");
139 Assert
.AreEqual (MemberClass
.GetStaticPropertyInfo(), expr
.Member
, "Bind#16");
143 public int Integer { get; set; }
144 public short Short { get; set; }
148 public void BindValueTypes ()
150 var i
= Expression
.Parameter (typeof (int), "i");
151 var s
= Expression
.Parameter (typeof (short), "s");
153 var gslot
= Expression
.Lambda
<Func
<int, short, Slot
>> (
154 Expression
.MemberInit (
155 Expression
.New (typeof (Slot
)),
156 Expression
.Bind (typeof (Slot
).GetProperty ("Integer"), i
),
157 Expression
.Bind (typeof (Slot
).GetProperty ("Short"), s
)), i
, s
).Compile ();
159 Assert
.AreEqual (new Slot { Integer = 42, Short = -1 }
, gslot (42, -1));