Add bigger unit test for Intersect and Union
[mono.git] / mcs / class / System.Core / Test / System.Linq / QueryableTest.cs
1 //
2 // Queryable.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 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 System.Reflection;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Linq {
39
40         [TestFixture]
41         public class QueryableTest {
42
43                 [Test]
44                 public void TestElementType ()
45                 {
46                         var data = new int [] { 1, 2, 3 };
47                         var queryable = data.AsQueryable ();
48
49                         Assert.AreEqual (typeof (int), queryable.ElementType);
50                 }
51
52                 [Test]
53                 public void TestCount ()
54                 {
55                         var q = CreateQueryable<string> ();
56
57                         q.Count ();
58
59                         AssertReceived (GetMethod ("Count", 0), q);
60                 }
61
62                 [Test]
63                 public void TestCountPredicate ()
64                 {
65                         var q = CreateQueryable<string> ();
66
67                         q.Count (s => true);
68
69                         AssertReceived (GetMethod ("Count", 1), q);
70                 }
71
72                 public static void AssertReceived<T> (MethodInfo method, MockQuery<T> query)
73                 {
74                         Expression expression = query.MockProvider.Received;
75
76                         MethodCallExpression call = expression as MethodCallExpression;
77
78                         Assert.IsNotNull (call, "Expected a MethodCallExpression");
79
80                         MethodInfo expected = method.MakeGenericMethod (typeof (T));
81
82                         Assert.AreEqual (expected, call.Method, "Expected method: " + expected);
83                 }
84
85                 public static MethodInfo GetMethod (string name, int parameters)
86                 {
87                         var methods = from m in typeof (Queryable).GetMethods ()
88                                                   where m.Name == name && m.GetParameters ().Length == parameters + 1
89                                                   select m;
90
91                         return methods.First ();
92                 }
93
94                 static MockQuery<T> CreateQueryable<T> ()
95                 {
96                         return new MockQuery<T> (new MockQueryProvider ());
97                 }
98
99                 public class MockQueryProvider : IQueryProvider {
100
101                         Expression received;
102
103                         public Expression Received {
104                                 get { return received; }
105                         }
106
107                         public IQueryable<TElement> CreateQuery<TElement> (Expression expression)
108                         {
109                                 throw new NotImplementedException ();
110                         }
111
112                         public IQueryable CreateQuery (Expression expression)
113                         {
114                                 throw new NotImplementedException ();
115                         }
116
117                         public TResult Execute<TResult> (Expression expression)
118                         {
119                                 received = expression;
120
121                                 return default (TResult);
122                         }
123
124                         public object Execute (Expression expression)
125                         {
126                                 throw new NotImplementedException ();
127                         }
128                 }
129
130                 public class MockQuery<T> : IQueryable<T> {
131
132                         MockQueryProvider provider;
133                         Expression expression;
134
135                         public Type ElementType {
136                                 get { return typeof (T); }
137                         }
138
139                         public Expression Expression {
140                                 get { return expression; }
141                         }
142
143                         public IQueryProvider Provider {
144                                 get { return provider; }
145                         }
146
147                         public MockQueryProvider MockProvider {
148                                 get { return provider; }
149                         }
150
151                         public MockQuery (MockQueryProvider provider)
152                         {
153                                 this.provider = provider;
154                                 this.expression = Expression.Constant (this);
155                         }
156
157                         public MockQuery (MockQueryProvider provider, Expression expression)
158                         {
159                                 this.provider = provider;
160                                 this.expression = expression;
161                         }
162
163                         public IEnumerator<T> GetEnumerator ()
164                         {
165                                 throw new NotImplementedException ();
166                         }
167
168                         IEnumerator IEnumerable.GetEnumerator ()
169                         {
170                                 return GetEnumerator ();
171                         }
172                 }
173
174         }
175 }