QueryableTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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]
  39. public void TestElementType ()
  40. {
  41. var data = new int [] { 1, 2, 3 };
  42. var queryable = data.AsQueryable ();
  43. Assert.AreEqual (typeof (int), queryable.ElementType);
  44. }
  45. [Test]
  46. public void TestCount ()
  47. {
  48. var q = CreateQueryable<string> ();
  49. q.Count ();
  50. AssertReceived (GetMethod ("Count", 0), q);
  51. }
  52. [Test]
  53. public void TestCountPredicate ()
  54. {
  55. var q = CreateQueryable<string> ();
  56. q.Count (s => true);
  57. AssertReceived (GetMethod ("Count", 1), q);
  58. }
  59. public static void AssertReceived<T> (MethodInfo method, MockQuery<T> query)
  60. {
  61. Expression expression = query.MockProvider.Received;
  62. MethodCallExpression call = expression as MethodCallExpression;
  63. Assert.IsNotNull (call, "Expected a MethodCallExpression");
  64. MethodInfo expected = method.MakeGenericMethod (typeof (T));
  65. Assert.AreEqual (expected, call.Method, "Expected method: " + expected);
  66. }
  67. public static MethodInfo GetMethod (string name, int parameters)
  68. {
  69. var methods = from m in typeof (Queryable).GetMethods ()
  70. where m.Name == name && m.GetParameters ().Length == parameters + 1
  71. select m;
  72. return methods.First ();
  73. }
  74. static MockQuery<T> CreateQueryable<T> ()
  75. {
  76. return new MockQuery<T> (new MockQueryProvider ());
  77. }
  78. public class MockQueryProvider : IQueryProvider {
  79. Expression received;
  80. public Expression Received {
  81. get { return received; }
  82. }
  83. public IQueryable<TElement> CreateQuery<TElement> (Expression expression)
  84. {
  85. throw new NotImplementedException ();
  86. }
  87. public IQueryable CreateQuery (Expression expression)
  88. {
  89. throw new NotImplementedException ();
  90. }
  91. public TResult Execute<TResult> (Expression expression)
  92. {
  93. received = expression;
  94. return default (TResult);
  95. }
  96. public object Execute (Expression expression)
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. }
  101. public class MockQuery<T> : IQueryable<T> {
  102. MockQueryProvider provider;
  103. Expression expression;
  104. public Type ElementType {
  105. get { return typeof (T); }
  106. }
  107. public Expression Expression {
  108. get { return expression; }
  109. }
  110. public IQueryProvider Provider {
  111. get { return provider; }
  112. }
  113. public MockQueryProvider MockProvider {
  114. get { return provider; }
  115. }
  116. public MockQuery (MockQueryProvider provider)
  117. {
  118. this.provider = provider;
  119. this.expression = Expression.Constant (this);
  120. }
  121. public MockQuery (MockQueryProvider provider, Expression expression)
  122. {
  123. this.provider = provider;
  124. this.expression = expression;
  125. }
  126. public IEnumerator<T> GetEnumerator ()
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. IEnumerator IEnumerable.GetEnumerator ()
  131. {
  132. return GetEnumerator ();
  133. }
  134. }
  135. }
  136. }