PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MongoDB.Driver.Tests/Linq/Processors/PartialEvaluatorTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 142 lines | 105 code | 23 blank | 14 comment | 8 complexity | 84a940f0c5ef6daa32b258e6a98983de MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Linq.Expressions;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using MongoDB.Driver.Linq.Processors;
  22. using FluentAssertions;
  23. using NUnit.Framework;
  24. using System.Diagnostics;
  25. namespace MongoDB.Driver.Tests.Linq.Processors
  26. {
  27. [TestFixture]
  28. public class PartialEvaluatorTests
  29. {
  30. [Test]
  31. public void Indexed_local_capture()
  32. {
  33. var captured = new[] { "Jack", "John" };
  34. Expression<Func<Person, bool>> predicate = x => x.Name == captured[0];
  35. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  36. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  37. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  38. }
  39. [Test]
  40. public void Named_local_capture()
  41. {
  42. var captured = "Jack";
  43. Expression<Func<Person, bool>> predicate = x => x.Name == captured;
  44. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  45. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  46. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  47. }
  48. [Test]
  49. public void Instance_method_call_with_no_arguments()
  50. {
  51. Expression<Func<Person, bool>> predicate = x => x.Name == InstanceGetName();
  52. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  53. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  54. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  55. }
  56. [Test]
  57. public void Instance_method_call_with_a_constant_argument()
  58. {
  59. Expression<Func<Person, bool>> predicate = x => x.Name == InstanceGetName("ck");
  60. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  61. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  62. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  63. }
  64. [Test]
  65. public void Instance_method_call_with_a_captured_argument()
  66. {
  67. var captured = "ck";
  68. Expression<Func<Person, bool>> predicate = x => x.Name == InstanceGetName(captured);
  69. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  70. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  71. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  72. }
  73. [Test]
  74. public void Static_method_call_with_no_arguments()
  75. {
  76. Expression<Func<Person, bool>> predicate = x => x.Name == StaticGetName();
  77. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  78. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  79. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  80. }
  81. [Test]
  82. public void Static_method_call_with_a_constant_argument()
  83. {
  84. Expression<Func<Person, bool>> predicate = x => x.Name == StaticGetName("ck");
  85. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  86. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  87. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  88. }
  89. [Test]
  90. public void Static_method_call_with_a_captured_argument()
  91. {
  92. var captured = "ck";
  93. Expression<Func<Person, bool>> predicate = x => x.Name == StaticGetName(captured);
  94. var evaluated = (BinaryExpression)PartialEvaluator.Evaluate(predicate.Body);
  95. evaluated.Right.NodeType.Should().Be(ExpressionType.Constant);
  96. ((ConstantExpression)evaluated.Right).Value.Should().Be("Jack");
  97. }
  98. private string InstanceGetName()
  99. {
  100. return "Jack";
  101. }
  102. private string InstanceGetName(string suffix)
  103. {
  104. return "Ja" + suffix;
  105. }
  106. private static string StaticGetName()
  107. {
  108. return "Jack";
  109. }
  110. private static string StaticGetName(string suffix)
  111. {
  112. return "Ja" + suffix;
  113. }
  114. private class Person
  115. {
  116. public string Name { get; set; }
  117. }
  118. }
  119. }