QueryableTest.cs 3.9 KB

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