PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/pombredanne/SharpDevelop
C# | 252 lines | 214 code | 13 blank | 25 comment | 0 complexity | a6b8063b667e78fb7e6c7e8b8e5d0ab9 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. // ReplaceWithIsOperatorTests.cs
  3. //
  4. // Author:
  5. // Ji Kun <jikun.nus@gmail.com>
  6. //
  7. // Copyright (c) 2013 Ji Kun
  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 OperatorIsCanBeUsedIssueTests : InspectionActionTestBase
  34. {
  35. [Test]
  36. public void TestInspectorCase1()
  37. {
  38. var input = @"
  39. using System;
  40. using System.Linq;
  41. using System.Reflection;
  42. namespace Demo
  43. {
  44. public class BaseClass
  45. {
  46. public static void main(string[] args)
  47. {
  48. int a = 1;
  49. if (typeof (int) == a.GetType()){}}}}
  50. ";
  51. TestRefactoringContext context;
  52. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  53. Assert.AreEqual(1, issues.Count);
  54. CheckFix(context, issues, @"
  55. using System;
  56. using System.Linq;
  57. using System.Reflection;
  58. namespace Demo
  59. {
  60. public class BaseClass
  61. {
  62. public static void main(string[] args)
  63. {
  64. int a = 1;
  65. if (a is int) {
  66. }}}}
  67. ");
  68. }
  69. [Test]
  70. public void TestInspectorCase2()
  71. {
  72. var input = @"
  73. using System;
  74. using System.Linq;
  75. using System.Reflection;
  76. namespace Demo
  77. {
  78. public class BaseClass
  79. {
  80. public static void main(string[] args)
  81. {
  82. int a = 1;
  83. if (a.GetType() == typeof (int)){}}}}
  84. ";
  85. TestRefactoringContext context;
  86. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  87. Assert.AreEqual(1, issues.Count);
  88. CheckFix(context, issues, @"
  89. using System;
  90. using System.Linq;
  91. using System.Reflection;
  92. namespace Demo
  93. {
  94. public class BaseClass
  95. {
  96. public static void main(string[] args)
  97. {
  98. int a = 1;
  99. if (a is int) {
  100. }}}}
  101. ");
  102. }
  103. [Test]
  104. public void TestInspectorCase3()
  105. {
  106. var input = @"
  107. using System;
  108. using System.Linq;
  109. using System.Reflection;
  110. namespace Demo
  111. {
  112. public class BaseClass
  113. {
  114. static public int a;
  115. public static void main(string[] args)
  116. {
  117. if (BaseClass.a.GetType() == typeof (int)){}}}}
  118. ";
  119. TestRefactoringContext context;
  120. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  121. Assert.AreEqual(1, issues.Count);
  122. CheckFix(context, issues, @"
  123. using System;
  124. using System.Linq;
  125. using System.Reflection;
  126. namespace Demo
  127. {
  128. public class BaseClass
  129. {
  130. static public int a;
  131. public static void main(string[] args)
  132. {
  133. if (BaseClass.a is int) {
  134. }}}}
  135. ");
  136. }
  137. [Test]
  138. public void TestInspectorCase4()
  139. {
  140. var input = @"
  141. using System;
  142. using System.Reflection;
  143. namespace Demo
  144. {
  145. public sealed class TestClass
  146. {
  147. }
  148. public class BaseClass
  149. {
  150. public static void main(string[] args)
  151. {
  152. BaseClass b = new BaseClass();if (typeof (TestClass) == b.GetType()){}}}}
  153. ";
  154. TestRefactoringContext context;
  155. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  156. Assert.AreEqual(1, issues.Count);
  157. CheckFix(context, issues, @"
  158. using System;
  159. using System.Reflection;
  160. namespace Demo
  161. {
  162. public sealed class TestClass
  163. {
  164. }
  165. public class BaseClass
  166. {
  167. public static void main(string[] args)
  168. {
  169. BaseClass b = new BaseClass();
  170. if (b is TestClass) {
  171. }}}}
  172. ");
  173. }
  174. [Test]
  175. public void TestInspectorCase5()
  176. {
  177. var input = @"
  178. using System;
  179. using System.Reflection;
  180. namespace Demo
  181. {
  182. public class TestClass
  183. {
  184. }
  185. public class BaseClass : TestClass
  186. {
  187. public static void main(string[] args)
  188. {
  189. BaseClass b = new BaseClass();
  190. if ((typeof (TestClass) == b.GetType()))
  191. {
  192. }
  193. }
  194. }
  195. }
  196. ";
  197. TestRefactoringContext context;
  198. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  199. Assert.AreEqual(0, issues.Count);
  200. }
  201. [Test]
  202. public void TestResharperDisable()
  203. {
  204. var input = @"using System;
  205. using System.Linq;
  206. using System.Reflection;
  207. namespace Demo
  208. {
  209. public class BaseClass
  210. {
  211. public static void main(string[] args)
  212. {
  213. int a = 1;
  214. //Resharper disable OperatorIsCanBeUsed
  215. if ((typeof (int) == a.GetType()))
  216. {
  217. }
  218. //Resharper restore OperatorIsCanBeUsed
  219. }
  220. }
  221. }";
  222. TestRefactoringContext context;
  223. var issues = GetIssues(new OperatorIsCanBeUsedIssue(), input, out context);
  224. Assert.AreEqual(0, issues.Count);
  225. }
  226. }
  227. }