PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ReplaceWithOfTypeIssueTests.cs

https://github.com/pombredanne/SharpDevelop
C# | 221 lines | 181 code | 15 blank | 25 comment | 0 complexity | 8f8395ffbdeeed9ec1e68e1f4aa6305a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-3.0
  1. //
  2. // ReplaceWithOfTypeIssueTests.cs
  3. //
  4. // Author:
  5. // Mike Kr端ger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2013 Xamarin Inc. (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. using System.Linq;
  31. namespace ICSharpCode.NRefactory.CSharp.CodeIssues
  32. {
  33. [TestFixture]
  34. public class ReplaceWithOfTypeIssueTests : InspectionActionTestBase
  35. {
  36. [Test]
  37. public void TestCaseSelectNotNull ()
  38. {
  39. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  40. class Test
  41. {
  42. public void Foo(object[] obj)
  43. {
  44. obj.SelectNotNull((object o) => o as Test);
  45. }
  46. }", @"using System.Linq;
  47. class Test
  48. {
  49. public void Foo(object[] obj)
  50. {
  51. obj.OfType<Test> ();
  52. }
  53. }");
  54. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  55. class Test
  56. {
  57. public void Foo(object[] obj)
  58. {
  59. obj.SelectNotNull(o => o as Test);
  60. }
  61. }", @"using System.Linq;
  62. class Test
  63. {
  64. public void Foo(object[] obj)
  65. {
  66. obj.OfType<Test> ();
  67. }
  68. }");
  69. }
  70. [Test]
  71. public void TestCaseSelectNotNullWithParentheses ()
  72. {
  73. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  74. class Test
  75. {
  76. public void Foo(object[] obj)
  77. {
  78. obj.SelectNotNull(o => ((o as Test)));
  79. }
  80. }", @"using System.Linq;
  81. class Test
  82. {
  83. public void Foo(object[] obj)
  84. {
  85. obj.OfType<Test> ();
  86. }
  87. }");
  88. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  89. class Test
  90. {
  91. public void Foo(object[] obj)
  92. {
  93. obj.SelectNotNull(o => o as Test);
  94. }
  95. }", @"using System.Linq;
  96. class Test
  97. {
  98. public void Foo(object[] obj)
  99. {
  100. obj.OfType<Test> ();
  101. }
  102. }");
  103. }
  104. [Test]
  105. public void TestCaseSelectWhereCase1 ()
  106. {
  107. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  108. class Test
  109. {
  110. public void Foo(object[] obj)
  111. {
  112. obj.Where(o => o is Test).Select (o => o as Test);
  113. }
  114. }", @"using System.Linq;
  115. class Test
  116. {
  117. public void Foo(object[] obj)
  118. {
  119. obj.OfType<Test> ();
  120. }
  121. }");
  122. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  123. class Test
  124. {
  125. public void Foo(object[] obj)
  126. {
  127. obj.SelectNotNull(o => o as Test);
  128. }
  129. }",@"using System.Linq;
  130. class Test
  131. {
  132. public void Foo(object[] obj)
  133. {
  134. obj.OfType<Test> ();
  135. }
  136. }");
  137. }
  138. [Test]
  139. public void TestCaseSelectWhereGabage ()
  140. {
  141. TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
  142. class Test
  143. {
  144. public void Foo(object[] obj)
  145. {
  146. obj.Where(o => o is Test).Select (x => o as Test);
  147. }
  148. }");
  149. TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
  150. class Test
  151. {
  152. public void Foo(object[] obj)
  153. {
  154. obj.SelectNotNull(o => null as Test);
  155. }
  156. }");
  157. }
  158. [Test]
  159. public void TestCaseSelectWhereCase2WithParens ()
  160. {
  161. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  162. class Test
  163. {
  164. public void Foo(object[] obj)
  165. {
  166. obj.Where(o => (o is Test)).Select (o => ((Test)(o)));
  167. }
  168. }", @"using System.Linq;
  169. class Test
  170. {
  171. public void Foo(object[] obj)
  172. {
  173. obj.OfType<Test> ();
  174. }
  175. }");
  176. Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
  177. class Test
  178. {
  179. public void Foo(object[] obj)
  180. {
  181. obj.SelectNotNull(o => o as Test);
  182. }
  183. }", @"using System.Linq;
  184. class Test
  185. {
  186. public void Foo(object[] obj)
  187. {
  188. obj.OfType<Test> ();
  189. }
  190. }");
  191. }
  192. [Test]
  193. public void TestDisable ()
  194. {
  195. TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
  196. class Test
  197. {
  198. public void Foo(object[] obj)
  199. {
  200. // ReSharper disable once ReplaceWithOfType
  201. obj.SelectNotNull((object o) => o as Test);
  202. }
  203. }");
  204. }
  205. }
  206. }