5 // Jb Evain (jbevain@novell.com)
7 // (C) 2007 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 System.Reflection;
36 using NUnit.Framework;
38 namespace MonoTests.System.Linq {
41 public class QueryableTest {
44 public void TestElementType ()
46 var data = new int [] { 1, 2, 3 };
47 var queryable = data.AsQueryable ();
49 Assert.AreEqual (typeof (int), queryable.ElementType);
53 public void TestCount ()
55 var q = CreateQueryable<string> ();
59 AssertReceived (GetMethod ("Count", 0), q);
63 public void TestCountPredicate ()
65 var q = CreateQueryable<string> ();
69 AssertReceived (GetMethod ("Count", 1), q);
72 public static void AssertReceived<T> (MethodInfo method, MockQuery<T> query)
74 Expression expression = query.MockProvider.Received;
76 MethodCallExpression call = expression as MethodCallExpression;
78 Assert.IsNotNull (call, "Expected a MethodCallExpression");
80 MethodInfo expected = method.MakeGenericMethod (typeof (T));
82 Assert.AreEqual (expected, call.Method, "Expected method: " + expected);
85 public static MethodInfo GetMethod (string name, int parameters)
87 var methods = from m in typeof (Queryable).GetMethods ()
88 where m.Name == name && m.GetParameters ().Length == parameters + 1
91 return methods.First ();
94 static MockQuery<T> CreateQueryable<T> ()
96 return new MockQuery<T> (new MockQueryProvider ());
99 public class MockQueryProvider : IQueryProvider {
103 public Expression Received {
104 get { return received; }
107 public IQueryable<TElement> CreateQuery<TElement> (Expression expression)
109 throw new NotImplementedException ();
112 public IQueryable CreateQuery (Expression expression)
114 throw new NotImplementedException ();
117 public TResult Execute<TResult> (Expression expression)
119 received = expression;
121 return default (TResult);
124 public object Execute (Expression expression)
126 throw new NotImplementedException ();
130 public class MockQuery<T> : IQueryable<T> {
132 MockQueryProvider provider;
133 Expression expression;
135 public Type ElementType {
136 get { return typeof (T); }
139 public Expression Expression {
140 get { return expression; }
143 public IQueryProvider Provider {
144 get { return provider; }
147 public MockQueryProvider MockProvider {
148 get { return provider; }
151 public MockQuery (MockQueryProvider provider)
153 this.provider = provider;
154 this.expression = Expression.Constant (this);
157 public MockQuery (MockQueryProvider provider, Expression expression)
159 this.provider = provider;
160 this.expression = expression;
163 public IEnumerator<T> GetEnumerator ()
165 throw new NotImplementedException ();
168 IEnumerator IEnumerable.GetEnumerator ()
170 return GetEnumerator ();