QueryableTest.cs 4.0 KB

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