PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantWhereWithPredicateIssueTests.cs

http://github.com/icsharpcode/NRefactory
C# | 113 lines | 79 code | 9 blank | 25 comment | 0 complexity | c98e23bcd61b371928aefe2d48392cff MD5 | raw file
  1. //
  2. // ReplaceWithSingleCallToAnyIssueTests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2013 Xamarin <http://xamarin.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using NUnit.Framework;
  28. using ICSharpCode.NRefactory.CSharp.Refactoring;
  29. using ICSharpCode.NRefactory.CSharp.CodeActions;
  30. namespace ICSharpCode.NRefactory.CSharp.CodeIssues
  31. {
  32. [TestFixture]
  33. public class ReplaceWithSingleCallToAnyIssueTests : InspectionActionTestBase
  34. {
  35. [Test]
  36. public void TestWhereAnyCase1 ()
  37. {
  38. var input = @"using System.Linq;
  39. public class CSharpDemo {
  40. public void Bla () {
  41. int[] arr;
  42. var bla = arr.Where (x => x < 4).Any ();
  43. }
  44. }";
  45. TestRefactoringContext context;
  46. var issues = GetIssues (new ReplaceWithSingleCallToAnyIssue (), input, out context);
  47. Assert.AreEqual (1, issues.Count);
  48. CheckFix (context, issues, @"using System.Linq;
  49. public class CSharpDemo {
  50. public void Bla () {
  51. int[] arr;
  52. var bla = arr.Any (x => x < 4);
  53. }
  54. }");
  55. }
  56. [Test]
  57. public void TestWhereAnyWrongWhere1 ()
  58. {
  59. var input = @"using System.Linq;
  60. public class CSharpDemo {
  61. public void Bla () {
  62. int[] arr;
  63. var bla = arr.Where ((x, i) => x + i < 4).Any ();
  64. }
  65. }";
  66. TestRefactoringContext context;
  67. var issues = GetIssues (new ReplaceWithSingleCallToAnyIssue (), input, out context);
  68. Assert.AreEqual (0, issues.Count);
  69. }
  70. [Test]
  71. public void TestWhereAnyWrongWhere2 ()
  72. {
  73. var input = @"using System;
  74. using System.Linq;
  75. public class X
  76. {
  77. X Where (Func<int,int> f) { return null; }
  78. bool Any () { return false; }
  79. public void Bla () {
  80. X ex = null;
  81. var bla = ex.Where (x => x + 1).Any ();
  82. }
  83. }";
  84. TestRefactoringContext context;
  85. var issues = GetIssues (new ReplaceWithSingleCallToAnyIssue (), input, out context);
  86. Assert.AreEqual (0, issues.Count);
  87. }
  88. [Test]
  89. public void TestDisable()
  90. {
  91. var input = @"using System.Linq;
  92. public class CSharpDemo {
  93. public void Bla () {
  94. int[] arr;
  95. // ReSharper disable once ReplaceWithSingleCallToAny
  96. var bla = arr.Where (x => x < 4).Any ();
  97. }
  98. }";
  99. TestRefactoringContext context;
  100. var issues = GetIssues(new ReplaceWithSingleCallToAnyIssue(), input, out context);
  101. Assert.AreEqual(0, issues.Count);
  102. }
  103. }
  104. }