Implemented overloaded versions of Parse and TryParse functions for BigInteger.
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_ListInit.cs
1 //
2 // ExpressionTest_ListInit.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Linq.Expressions;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Linq.Expressions {
37
38         [TestFixture]
39         public class ExpressionTest_ListInit {
40
41                 public class Foo {
42                 }
43
44                 public class Bar : IEnumerable {
45
46                         public IEnumerator GetEnumerator ()
47                         {
48                                 throw new NotImplementedException ();
49                         }
50                 }
51
52                 public class Baz : Bar {
53
54                         public void Add (object a, object b)
55                         {
56                         }
57                 }
58
59                 static NewExpression CreateNewList ()
60                 {
61                         return Expression.New (typeof (List<string>));
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentNullException))]
66                 public void NullExpression ()
67                 {
68                         Expression.ListInit (null, new List<ElementInit> ());
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentNullException))]
73                 public void NullElementInitializer ()
74                 {
75                         Expression.ListInit (CreateNewList (), new ElementInit [] { null });
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (ArgumentNullException))]
80                 public void NullExpressionInitializer ()
81                 {
82                         Expression.ListInit (CreateNewList (), new Expression [] { null });
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (InvalidOperationException))]
87                 public void ExpressionTypeDoesntImplementIEnumerable ()
88                 {
89                         Expression.ListInit (Expression.New (typeof (Foo)), "foo".ToConstant ());
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (InvalidOperationException))]
94                 public void ExpressionTypeDoesnHaveAddMethod ()
95                 {
96                         Expression.ListInit (Expression.New (typeof (Bar)), "foo".ToConstant ());
97                 }
98
99                 [Test]
100                 public void InitListOfStringWithConstants ()
101                 {
102                         var li = Expression.ListInit (
103                                 Expression.New (typeof (List<string>)),
104                                 "foo".ToConstant (), "bar".ToConstant ());
105
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 ());
109                 }
110
111                 [Test]
112                 public void InitListOfStringWithElementInitializers ()
113                 {
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 ()));
122
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 ());
126                 }
127
128                 [Test]
129                 public void CompileListOfStringsInit ()
130                 {
131                         var add = typeof (List<string>).GetMethod ("Add");
132
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     ();
138
139                         var list = c ();
140
141                         Assert.IsNotNull (list);
142                         Assert.AreEqual (2, list.Count);
143                         Assert.AreEqual ("foo", list [0]);
144                         Assert.AreEqual ("bar", list [1]);
145                 }
146
147                 [Test]
148                 [Category ("NotDotNet")]
149                 public void CompileArrayListOfStringsInit ()
150                 {
151                         var add = typeof (ArrayList).GetMethod ("Add");
152
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 ();
158
159                         var list = c ();
160
161                         Assert.IsNotNull (list);
162                         Assert.AreEqual (2, list.Count);
163                         Assert.AreEqual ("foo", list [0]);
164                         Assert.AreEqual ("bar", list [1]);
165                 }
166         }
167 }