Implemented overloaded versions of Parse and TryParse functions for BigInteger.
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Bind.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21
22 using System;
23 using System.Reflection;
24 using System.Collections.Generic;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_Bind
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.Bind (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.Bind (MemberClass.GetRwFieldInfo (), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentException))]
50                 public void Method1 ()
51                 {
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));
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentException))]
59                 public void Method2 ()
60                 {
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));
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentException))]
68                 public void Event ()
69                 {
70                         Expression.Bind (MemberClass.GetEventInfo (), Expression.Constant (1));
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentException))]
75                 public void PropertyRo ()
76                 {
77                         Expression.Bind (MemberClass.GetRoPropertyInfo (), Expression.Constant (1));
78                 }
79
80                 [Test]
81                 public void FieldRo ()
82                 {
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");
86                 }
87
88                 [Test]
89                 public void FieldRw ()
90                 {
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");
94                 }
95
96                 [Test]
97                 public void FieldStatic ()
98                 {
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");
102                 }
103
104                 [Test]
105                 public void PropertyRw ()
106                 {
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");
110                 }
111
112                 [Test]
113                 public void PropertyStatic ()
114                 {
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");
118                 }
119
120                 [Test]
121                 public void PropertyAccessor ()
122                 {
123                         MethodInfo mi = typeof(MemberClass).GetMethod("get_TestProperty2");
124
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");
129                 }
130
131                 [Test]
132                 public void PropertyAccessorStatic ()
133                 {
134                         MethodInfo mi = typeof(MemberClass).GetMethod("get_StaticProperty");
135
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");
140                 }
141
142                 struct Slot {
143                         public int Integer { get; set; }
144                         public short Short { get; set; }
145                 }
146
147                 [Test]
148                 public void BindValueTypes ()
149                 {
150                         var i = Expression.Parameter (typeof (int), "i");
151                         var s = Expression.Parameter (typeof (short), "s");
152
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 ();
158
159                         Assert.AreEqual (new Slot { Integer = 42, Short = -1 }, gslot (42, -1));
160                 }
161         }
162 }