2 // ExpressionTest_New.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.Reflection;
32 using System.Linq.Expressions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Linq.Expressions {
39 public class ExpressionTest_New {
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void NullType ()
45 Expression.New (null as Type);
49 [ExpectedException (typeof (ArgumentNullException))]
50 public void NullConstructor ()
52 Expression.New (null as ConstructorInfo);
64 public string Value { get; set; }
75 [ExpectedException (typeof (ArgumentException))]
76 public void NoParameterlessConstructor ()
78 Expression.New (typeof (Foo));
82 [ExpectedException (typeof (ArgumentException))]
83 public void ConstructorHasTooMuchParameters ()
85 Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }));
89 [Category ("NotDotNet")]
90 [ExpectedException (typeof (ArgumentException))]
91 public void NewVoid ()
93 Expression.New (typeof (void));
97 [ExpectedException (typeof (ArgumentNullException))]
98 public void HasNullArgument ()
100 Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), null as Expression);
104 [ExpectedException (typeof (ArgumentException))]
105 public void HasWrongArgument ()
107 Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant (12));
111 public void NewFoo ()
113 var n = Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant ("foo"));
115 Assert.AreEqual (ExpressionType.New, n.NodeType);
116 Assert.AreEqual (typeof (Foo), n.Type);
117 Assert.AreEqual (1, n.Arguments.Count);
118 Assert.IsNull (n.Members);
119 Assert.AreEqual ("new Foo(\"foo\")", n.ToString ());
123 public void NewBar ()
125 var n = Expression.New (typeof (Bar));
127 Assert.IsNotNull (n.Constructor);
128 Assert.IsNotNull (n.Arguments);
129 Assert.IsNull (n.Members); // wrong doc
131 Assert.AreEqual ("new Bar()", n.ToString ());
133 n = Expression.New (typeof (Bar).GetConstructor (Type.EmptyTypes));
135 Assert.AreEqual ("new Bar()", n.ToString ());
138 public class Gazonk {
142 public Gazonk (string s)
147 public override bool Equals (object obj)
149 var o = obj as Gazonk;
153 return value == o.value;
156 public override int GetHashCode ()
158 return value.GetHashCode ();
163 public void CompileNewClass ()
165 var p = Expression.Parameter (typeof (string), "p");
166 var n = Expression.New (typeof (Gazonk).GetConstructor (new [] { typeof (string) }), p);
167 var fgaz = Expression.Lambda<Func<string, Gazonk>> (n, p).Compile ();
169 var g1 = new Gazonk ("foo");
170 var g2 = new Gazonk ("bar");
172 Assert.IsNotNull (g1);
173 Assert.AreEqual (g1, fgaz ("foo"));
174 Assert.IsNotNull (g2);
175 Assert.AreEqual (g2, fgaz ("bar"));
177 n = Expression.New (typeof (Bar));
178 var lbar = Expression.Lambda<Func<Bar>> (n).Compile ();
182 Assert.IsNotNull (bar);
183 Assert.IsNull (bar.Value);
186 public class FakeAnonymousType {
188 public string Foo { get; set; }
189 public string Bar { get; set; }
190 public string Baz { get; set; }
191 public int Gazonk { get; set; }
192 public string Tzap { set {} }
194 public FakeAnonymousType (string foo)
199 public FakeAnonymousType (string foo, string bar, string baz)
208 public void NewFakeAnonymousType ()
210 var n = Expression.New (
211 typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string), typeof (string), typeof (string) } ),
212 new [] { "Foo".ToConstant (), "Bar".ToConstant (), "Baz".ToConstant () },
213 new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar"), typeof (FakeAnonymousType).GetProperty ("Baz") });
215 Assert.IsNotNull (n.Constructor);
216 Assert.IsNotNull (n.Arguments);
217 Assert.IsNotNull (n.Members);
218 Assert.AreEqual ("new FakeAnonymousType(Foo = \"Foo\", Bar = \"Bar\", Baz = \"Baz\")", n.ToString ());
222 [ExpectedException (typeof (ArgumentNullException))]
223 public void NullMember ()
226 typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
227 new [] { "Foo".ToConstant () },
228 new MemberInfo [] { null });
232 [ExpectedException (typeof (ArgumentException))]
233 public void MemberArgumentMiscount ()
236 typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
237 new [] { "Foo".ToConstant () },
238 new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar") });
242 [ExpectedException (typeof (ArgumentException))]
243 public void MemberArgumentMismatch ()
246 typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
247 new [] { "Foo".ToConstant () },
248 new [] { typeof (FakeAnonymousType).GetProperty ("Gazonk") });
252 [ExpectedException (typeof (ArgumentException))]
253 public void MemberHasNoGetter ()
256 typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
257 new [] { "Foo".ToConstant () },
258 new [] { typeof (FakeAnonymousType).GetProperty ("Tzap") });
261 public struct EineStrukt {
265 public EineStrukt (int left, int right)
273 public void CompileNewStruct ()
275 var create = Expression.Lambda<Func<EineStrukt>> (
276 Expression.New (typeof (EineStrukt))).Compile ();
279 Assert.AreEqual (0, s.left);
280 Assert.AreEqual (0, s.right);
284 public void CompileNewStructWithParameters ()
286 var pl = Expression.Parameter (typeof (int), "left");
287 var pr = Expression.Parameter (typeof (int), "right");
289 var create = Expression.Lambda<Func<int, int, EineStrukt>> (
290 Expression.New (typeof (EineStrukt).GetConstructor (new [] { typeof (int), typeof (int) }), pl, pr), pl, pr).Compile ();
292 var s = create (42, 12);
294 Assert.AreEqual (42, s.left);
295 Assert.AreEqual (12, s.right);
298 public class EineKlass {
300 public string Left { get; set; }
301 public string Right { get; set; }
307 public EineKlass (string l, string r)
315 public void CompileNewClassEmptyConstructor ()
317 var create = Expression.Lambda<Func<EineKlass>> (
318 Expression.New (typeof (EineKlass))).Compile ();
321 Assert.IsNull (k.Left);
322 Assert.IsNull (k.Right);
326 public void CompileNewClassWithParameters ()
328 var pl = Expression.Parameter (typeof (string), "left");
329 var pr = Expression.Parameter (typeof (string), "right");
331 var create = Expression.Lambda<Func<string, string, EineKlass>> (
332 Expression.New (typeof (EineKlass).GetConstructor (new [] { typeof (string), typeof (string) }), pl, pr), pl, pr).Compile ();
334 var k = create ("foo", "bar");
336 Assert.AreEqual ("foo", k.Left);
337 Assert.AreEqual ("bar", k.Right);