PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/pombredanne/SharpDevelop
C# | 301 lines | 255 code | 21 blank | 25 comment | 0 complexity | d0fa3d95aa4ee5050772fefccdcd755f 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. // RedundantUsingDirectiveIssueTests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2012 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. namespace ICSharpCode.NRefactory.CSharp.CodeIssues
  31. {
  32. [TestFixture]
  33. public class RedundantUsingDirectiveIssueTests : InspectionActionTestBase
  34. {
  35. [Test]
  36. public void TestInspectorCase1 ()
  37. {
  38. var input = @"using System;
  39. class Foo
  40. {
  41. void Bar (string str)
  42. {
  43. }
  44. }";
  45. TestRefactoringContext context;
  46. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  47. Assert.AreEqual (1, issues.Count);
  48. CheckFix (context, issues, @"
  49. class Foo
  50. {
  51. void Bar (string str)
  52. {
  53. }
  54. }");
  55. }
  56. [Test]
  57. public void TestInspectorCase2 ()
  58. {
  59. var input = @"using System;
  60. class Foo
  61. {
  62. void Bar (string str)
  63. {
  64. }
  65. }";
  66. TestRefactoringContext context;
  67. var issueProvider = new RedundantUsingDirectiveIssue ();
  68. issueProvider.NamespacesToKeep.Add("System");
  69. var issues = GetIssues (issueProvider, input, out context);
  70. Assert.AreEqual (0, issues.Count);
  71. }
  72. [Test]
  73. public void TestInspectorCase3 ()
  74. {
  75. var input = @"using System;
  76. using System.Collections.Generic;
  77. namespace Foo
  78. {
  79. class Bar
  80. {
  81. List<String> list;
  82. }
  83. }";
  84. TestRefactoringContext context;
  85. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  86. Assert.AreEqual (0, issues.Count);
  87. }
  88. [Test]
  89. public void Linq1 ()
  90. {
  91. var input = @"using System;
  92. using System.Collections.Generic;
  93. using System.Linq;
  94. class Bar
  95. {
  96. public object M(List<String> list)
  97. {
  98. return list.Where(t => !String.IsNullOrEmpty(t));
  99. }
  100. }";
  101. TestRefactoringContext context;
  102. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  103. Assert.AreEqual (0, issues.Count);
  104. }
  105. [Test]
  106. public void Linq2 ()
  107. {
  108. var input = @"using System;
  109. using System.Collections.Generic;
  110. using System.Linq;
  111. class Bar
  112. {
  113. public object M(List<String> list)
  114. {
  115. return from t in list where !String.IsNullOrEmpty(t) select t;
  116. }
  117. }";
  118. TestRefactoringContext context;
  119. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  120. Assert.AreEqual (0, issues.Count);
  121. }
  122. [Test]
  123. public void TestResharperDisableRestore ()
  124. {
  125. var input = @"// ReSharper disable RedundantUsingDirective
  126. using System;
  127. // ReSharper restore RedundantUsingDirective
  128. using System.IO;
  129. class Foo
  130. {
  131. }";
  132. TestRefactoringContext context;
  133. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  134. Assert.AreEqual (1, issues.Count);
  135. }
  136. [Test]
  137. public void TestResharperDisableOnce ()
  138. {
  139. var input = @"using System;
  140. // ReSharper disable once RedundantUsingDirective
  141. using System.IO;
  142. using System.Text;
  143. class Foo
  144. {
  145. }";
  146. TestRefactoringContext context;
  147. var issues = GetIssues (new RedundantUsingDirectiveIssue (), input, out context);
  148. Assert.AreEqual (2, issues.Count);
  149. }
  150. [Test]
  151. public void TestSubnamespace ()
  152. {
  153. Test<RedundantUsingDirectiveIssue>(@"namespace Foo
  154. {
  155. using System;
  156. using System.Collections.Generic;
  157. class Bar
  158. {
  159. public static void Main (string[] args)
  160. {
  161. Console.WriteLine ();
  162. }
  163. }
  164. }", @"namespace Foo
  165. {
  166. using System;
  167. class Bar
  168. {
  169. public static void Main (string[] args)
  170. {
  171. Console.WriteLine ();
  172. }
  173. }
  174. }");
  175. }
  176. [Test]
  177. public void TestSubnamespaceCase1 ()
  178. {
  179. Test<RedundantUsingDirectiveIssue>(@"namespace Foo
  180. {
  181. using System;
  182. namespace Sub {
  183. using System;
  184. }
  185. namespace Sub2 {
  186. class Bar
  187. {
  188. public static void Main (string[] args)
  189. {
  190. Console.WriteLine ();
  191. }
  192. }
  193. }
  194. }", @"namespace Foo
  195. {
  196. using System;
  197. namespace Sub {
  198. }
  199. namespace Sub2 {
  200. class Bar
  201. {
  202. public static void Main (string[] args)
  203. {
  204. Console.WriteLine ();
  205. }
  206. }
  207. }
  208. }");
  209. }
  210. [Test]
  211. public void TestSubnamespaceCase3 ()
  212. {
  213. Test<RedundantUsingDirectiveIssue>(@"namespace Foo
  214. {
  215. using System;
  216. namespace Sub2 {
  217. using System;
  218. class Bar
  219. {
  220. public static void Main (string[] args)
  221. {
  222. Console.WriteLine ();
  223. }
  224. }
  225. }
  226. }", @"namespace Foo
  227. {
  228. namespace Sub2 {
  229. using System;
  230. class Bar
  231. {
  232. public static void Main (string[] args)
  233. {
  234. Console.WriteLine ();
  235. }
  236. }
  237. }
  238. }");
  239. }
  240. [Test]
  241. public void TestKeywordsInNamespace ()
  242. {
  243. TestWrongContext<RedundantUsingDirectiveIssue>(@"
  244. namespace org.eclipse.jgit.@internal.storage.file
  245. {
  246. public class File {}
  247. }
  248. namespace Foo
  249. {
  250. using org.eclipse.jgit.@internal.storage.file;
  251. class Bar
  252. {
  253. public static void Main (string[] args)
  254. {
  255. File file;
  256. }
  257. }
  258. }
  259. ");
  260. }
  261. }
  262. }