2 // ExpressionTest_ListInit.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;
31 using System.Collections.Generic;
33 using System.Linq.Expressions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Linq.Expressions {
39 public class ExpressionTest_ListInit {
44 public class Bar : IEnumerable {
46 public IEnumerator GetEnumerator ()
48 throw new NotImplementedException ();
52 public class Baz : Bar {
54 public void Add (object a, object b)
59 static NewExpression CreateNewList ()
61 return Expression.New (typeof (List<string>));
65 [ExpectedException (typeof (ArgumentNullException))]
66 public void NullExpression ()
68 Expression.ListInit (null, new List<ElementInit> ());
72 [ExpectedException (typeof (ArgumentNullException))]
73 public void NullElementInitializer ()
75 Expression.ListInit (CreateNewList (), new ElementInit [] { null });
79 [ExpectedException (typeof (ArgumentNullException))]
80 public void NullExpressionInitializer ()
82 Expression.ListInit (CreateNewList (), new Expression [] { null });
86 [ExpectedException (typeof (InvalidOperationException))]
87 public void ExpressionTypeDoesntImplementIEnumerable ()
89 Expression.ListInit (Expression.New (typeof (Foo)), "foo".ToConstant ());
93 [ExpectedException (typeof (InvalidOperationException))]
94 public void ExpressionTypeDoesnHaveAddMethod ()
96 Expression.ListInit (Expression.New (typeof (Bar)), "foo".ToConstant ());
100 public void InitListOfStringWithConstants ()
102 var li = Expression.ListInit (
103 Expression.New (typeof (List<string>)),
104 "foo".ToConstant (), "bar".ToConstant ());
106 Assert.AreEqual (typeof (List<string>), li.Type);
107 Assert.AreEqual (ExpressionType.ListInit, li.NodeType);
108 Assert.AreEqual ("new List`1() {Void Add(System.String)(\"foo\"), Void Add(System.String)(\"bar\")}", li.ToString ());
112 public void InitListOfStringWithElementInitializers ()
114 var li = Expression.ListInit (
115 Expression.New (typeof (List<string>)),
116 Expression.ElementInit (
117 typeof (List<string>).GetMethod ("Add"),
118 "foo".ToConstant ()),
119 Expression.ElementInit (
120 typeof (List<string>).GetMethod ("Add"),
121 "bar".ToConstant ()));
123 Assert.AreEqual (typeof (List<string>), li.Type);
124 Assert.AreEqual (ExpressionType.ListInit, li.NodeType);
125 Assert.AreEqual ("new List`1() {Void Add(System.String)(\"foo\"), Void Add(System.String)(\"bar\")}", li.ToString ());
129 public void CompileListOfStringsInit ()
131 var add = typeof (List<string>).GetMethod ("Add");
133 var c = Expression.Lambda<Func<List<string>>> (
134 Expression.ListInit (
135 Expression.New (typeof (List<string>)),
136 Expression.ElementInit (add, "foo".ToConstant ()),
137 Expression.ElementInit (add, "bar".ToConstant ()))).Compile ();
141 Assert.IsNotNull (list);
142 Assert.AreEqual (2, list.Count);
143 Assert.AreEqual ("foo", list [0]);
144 Assert.AreEqual ("bar", list [1]);
148 [Category ("NotDotNet")]
149 public void CompileArrayListOfStringsInit ()
151 var add = typeof (ArrayList).GetMethod ("Add");
153 var c = Expression.Lambda<Func<ArrayList>> (
154 Expression.ListInit (
155 Expression.New (typeof (ArrayList)),
156 Expression.ElementInit (add, "foo".ToConstant ()),
157 Expression.ElementInit (add, "bar".ToConstant ()))).Compile ();
161 Assert.IsNotNull (list);
162 Assert.AreEqual (2, list.Count);
163 Assert.AreEqual ("foo", list [0]);
164 Assert.AreEqual ("bar", list [1]);