QueryableTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Queryable.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  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. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Linq.Expressions;
  33. using System.Reflection;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Linq {
  36. [TestFixture]
  37. public class QueryableTest {
  38. [Test] // #701187
  39. public void ConcatCastedQueryables ()
  40. {
  41. var bs = new List<B> { new B (), new B () }.AsQueryable ();
  42. var cs = new List<C> { new C (), new C () }.AsQueryable ();
  43. var concat = abs.Cast<A> ().Concat (acs.Cast<A> ());
  44. Assert.AreEqual (4, concat.Count ());
  45. }
  46. class A { }
  47. class B : A { }
  48. class C : A { }
  49. [Test]
  50. public void TestElementType ()
  51. {
  52. var data = new int [] { 1, 2, 3 };
  53. var queryable = data.AsQueryable ();
  54. Assert.AreEqual (typeof (int), queryable.ElementType);
  55. }
  56. [Test]
  57. public void TestCount ()
  58. {
  59. var q = CreateQueryable<string> ();
  60. q.Count ();
  61. AssertReceived (GetMethod ("Count", 0), q);
  62. }
  63. [Test]
  64. public void TestCountPredicate ()
  65. {
  66. var q = CreateQueryable<string> ();
  67. q.Count (s => true);
  68. AssertReceived (GetMethod ("Count", 1), q);
  69. }
  70. public static void AssertReceived<T> (MethodInfo method, MockQuery<T> query)
  71. {
  72. Expression expression = query.MockProvider.Received;
  73. MethodCallExpression call = expression as MethodCallExpression;
  74. Assert.IsNotNull (call, "Expected a MethodCallExpression");
  75. MethodInfo expected = method.MakeGenericMethod (typeof (T));
  76. Assert.AreEqual (expected, call.Method, "Expected method: " + expected);
  77. }
  78. public static MethodInfo GetMethod (string name, int parameters)
  79. {
  80. var methods = from m in typeof (Queryable).GetMethods ()
  81. where m.Name == name && m.GetParameters ().Length == parameters + 1
  82. select m;
  83. return methods.First ();
  84. }
  85. static MockQuery<T> CreateQueryable<T> ()
  86. {
  87. return new MockQuery<T> (new MockQueryProvider ());
  88. }
  89. public class MockQueryProvider : IQueryProvider {
  90. Expression received;
  91. public Expression Received {
  92. get { return received; }
  93. }
  94. public IQueryable<TElement> CreateQuery<TElement> (Expression expression)
  95. {
  96. throw new NotImplementedException ();
  97. }
  98. public IQueryable CreateQuery (Expression expression)
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. public TResult Execute<TResult> (Expression expression)
  103. {
  104. received = expression;
  105. return default (TResult);
  106. }
  107. public object Execute (Expression expression)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. }
  112. public class MockQuery<T> : IQueryable<T> {
  113. MockQueryProvider provider;
  114. Expression expression;
  115. public Type ElementType {
  116. get { return typeof (T); }
  117. }
  118. public Expression Expression {
  119. get { return expression; }
  120. }
  121. public IQueryProvider Provider {
  122. get { return provider; }
  123. }
  124. public MockQueryProvider MockProvider {
  125. get { return provider; }
  126. }
  127. public MockQuery (MockQueryProvider provider)
  128. {
  129. this.provider = provider;
  130. this.expression = Expression.Constant (this);
  131. }
  132. public MockQuery (MockQueryProvider provider, Expression expression)
  133. {
  134. this.provider = provider;
  135. this.expression = expression;
  136. }
  137. public IEnumerator<T> GetEnumerator ()
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. IEnumerator IEnumerable.GetEnumerator ()
  142. {
  143. return GetEnumerator ();
  144. }
  145. }
  146. }
  147. }