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

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddUsing/AddUsingActionTests.cs

https://github.com/pombredanne/SharpDevelop
C# | 320 lines | 295 code | 25 blank | 0 comment | 0 complexity | e46a0fc0e61ee9384cf1572472ecc15f 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. using ICSharpCode.NRefactory.CSharp.CodeIssues;
  2. using NUnit.Framework;
  3. using ICSharpCode.NRefactory.CSharp.CodeActions;
  4. using ICSharpCode.NRefactory.CSharp.Refactoring;
  5. using System.Linq;
  6. namespace ICSharpCode.NRefactory.CSharp.CodeActions.AddUsing
  7. {
  8. [TestFixture]
  9. public class AddUsingActionTests : ContextActionTestBase
  10. {
  11. void UnresolvedTypeName(string code, string typeName, params string[] namespaces)
  12. {
  13. TestActionDescriptions(
  14. new AddUsingAction(), code,
  15. namespaces.SelectMany(ns => new[] {
  16. "using " + ns + ";",
  17. ns + "." + typeName
  18. }).ToArray());
  19. }
  20. #region Field Declarations
  21. [Test]
  22. public void ShouldReturnAnIssueForUnresolvedFieldDeclarations()
  23. {
  24. UnresolvedTypeName(@"class Foo {
  25. private $TextWriter textWriter;
  26. }", "TextWriter", "System.IO");
  27. }
  28. [Test]
  29. public void ShouldNotReturnAnyIssuesIfFieldTypeIsResolved()
  30. {
  31. TestWrongContext<AddUsingAction>(@"using System.IO;
  32. class Foo {
  33. private $TextWriter textWriter;
  34. }");
  35. }
  36. [Test]
  37. public void ShouldReturnAnIssueIfFieldTypeArgumentIsNotResolvable()
  38. {
  39. UnresolvedTypeName(
  40. @"using System.Collections.Generic;
  41. class Foo
  42. {
  43. private List<$AttributeTargets> targets;
  44. }", "AttributeTargets", "System");
  45. }
  46. [Test]
  47. public void ShouldNotReturnAnIssueIfFieldTypeArgumentIsResolvable()
  48. {
  49. TestWrongContext<AddUsingAction>(
  50. @"using System;
  51. using System.Collections.Generic;
  52. class Foo
  53. {
  54. private List<$AttributeTargets> notifiers;
  55. }");
  56. }
  57. [Test]
  58. public void ShouldNotReturnAnIssueIfFieldTypeArgumentIsPrimitiveType()
  59. {
  60. TestWrongContext<AddUsingAction>(
  61. @"using System.Collections.Generic;
  62. class Foo
  63. {
  64. private List<$string> notifiers;
  65. }");
  66. }
  67. #endregion
  68. #region Method Return Types
  69. [Test]
  70. public void ShouldReturnIssueForUnresolvedReturnType()
  71. {
  72. UnresolvedTypeName(
  73. @"class Foo
  74. {
  75. $TextWriter Bar ()
  76. {
  77. return null;
  78. }
  79. }", "TextWriter", "System.IO");
  80. }
  81. [Test]
  82. public void ShouldNotReturnIssueForResolvedReturnType()
  83. {
  84. TestWrongContext<AddUsingAction>(
  85. @"using System.IO;
  86. class Foo
  87. {
  88. $TextWriter Bar ()
  89. {
  90. return null;
  91. }
  92. }");
  93. }
  94. #endregion
  95. #region Local Variables
  96. [Test]
  97. public void ShouldReturnIssueForUnresolvedLocalVariableDeclaration()
  98. {
  99. UnresolvedTypeName(
  100. @"class Foo
  101. {
  102. void Bar ()
  103. {
  104. $TextWriter writer;
  105. }
  106. }", "TextWriter", "System.IO");
  107. }
  108. [Test]
  109. public void ShouldNotReturnIssueForResolvedLocalVariableDeclaration()
  110. {
  111. TestWrongContext<AddUsingAction>(
  112. @"using System.IO;
  113. class Foo
  114. {
  115. void Bar ()
  116. {
  117. $TextWriter writer;
  118. }
  119. }");
  120. }
  121. #endregion
  122. #region Method Parameters
  123. [Test]
  124. public void ShouldReturnIssueIfMethodParameterIsNotResolvable()
  125. {
  126. UnresolvedTypeName(
  127. @"class Foo
  128. {
  129. void Bar ($TextWriter writer)
  130. {
  131. }
  132. }", "TextWriter", "System.IO");
  133. }
  134. [Test]
  135. public void ShouldNotReturnAnIssueIfMethodParameterIsResolvable()
  136. {
  137. TestWrongContext<AddUsingAction>(
  138. @"using System.IO;
  139. class Foo
  140. {
  141. void Bar ($TextWriter writer)
  142. {
  143. }
  144. }");
  145. }
  146. #endregion
  147. #region Base Types
  148. [Test]
  149. public void ShouldReturnIssueIfBaseClassIsNotResolvable()
  150. {
  151. UnresolvedTypeName(
  152. @"class Foo : $List<string>
  153. {
  154. }", "List<>", "System.Collections.Generic");
  155. }
  156. [Test]
  157. public void ShouldNotReturnIssueIfBaseClassIsResolvable()
  158. {
  159. TestWrongContext<AddUsingAction>(
  160. @"using System.Collections.Generic;
  161. class Foo : $List<string>
  162. {
  163. }");
  164. }
  165. [Test]
  166. public void ShouldReturnIssueIfGenericInterfaceIsMissingButNonGenericIsPresent()
  167. {
  168. UnresolvedTypeName(
  169. @"using System.Collections;
  170. class Foo : $IEnumerable<string>
  171. {
  172. }", "IEnumerable<>", "System.Collections.Generic");
  173. }
  174. [Test]
  175. public void ShouldReturnIssueIfNonGenericInterfaceIsMissingButGenericIsPresent()
  176. {
  177. UnresolvedTypeName(
  178. @"using System.Collections.Generic;
  179. class Foo : $IEnumerable
  180. {
  181. }", "IEnumerable", "System.Collections");
  182. }
  183. #endregion
  184. #region Member Access
  185. [Test]
  186. public void ShouldReturnIssueIfEnumValueIsNotResolvable()
  187. {
  188. UnresolvedTypeName(
  189. @"class Foo
  190. {
  191. void Bar ()
  192. {
  193. var support = $AttributeTargets.Assembly;
  194. }
  195. }", "AttributeTargets", "System");
  196. }
  197. [Test]
  198. public void ShouldNotReturnIssueIfEnumValueIsResolvable()
  199. {
  200. TestWrongContext<AddUsingAction>(
  201. @"using System;
  202. class Foo
  203. {
  204. void Bar ()
  205. {
  206. var support = $AttributeTargets.Assembly;
  207. }
  208. }");
  209. }
  210. #endregion
  211. [Test]
  212. public void ShouldReturnIssueIfAttributeIsNotResolvable()
  213. {
  214. UnresolvedTypeName(
  215. @"[$Serializable]
  216. class Foo
  217. {
  218. }", "SerializableAttribute", "System");
  219. }
  220. [Test]
  221. public void ShouldNotReturnIssueIfAttributeIsResolvable()
  222. {
  223. TestWrongContext<AddUsingAction>(
  224. @"using System;
  225. [$Serializable]
  226. class Foo
  227. {
  228. }");
  229. }
  230. [Test]
  231. public void ShouldReturnIssueIfTypeArgumentIsNotResolvable()
  232. {
  233. UnresolvedTypeName(
  234. @"using System.Collections.Generic;
  235. class Test
  236. {
  237. void TestMethod()
  238. {
  239. var list = new List<$Stream>();
  240. }
  241. }", "Stream", "System.IO");
  242. }
  243. [Test]
  244. public void ShouldReturnIssueForUnresolvedExtensionMethod()
  245. {
  246. TestActionDescriptions(
  247. new AddUsingAction(),
  248. @"using System.Collections.Generic;
  249. class Test
  250. {
  251. void TestMethod()
  252. {
  253. var list = new List<string>();
  254. var first = list.$First();
  255. }
  256. }", "using System.Linq;");
  257. }
  258. [Test]
  259. public void ShouldReturnMultipleNamespaceSuggestions()
  260. {
  261. UnresolvedTypeName(
  262. @"namespace A { public class TestClass { } }
  263. namespace B { public class TestClass { } }
  264. namespace C
  265. {
  266. public class Test
  267. {
  268. private $TestClass testClass;
  269. }
  270. }", "TestClass", "A", "B");
  271. }
  272. [Test]
  273. public void InnerTypeCanOnlyBeReferredToByFullName()
  274. {
  275. TestActionDescriptions(
  276. new AddUsingAction(),
  277. @"class Outer { public class Inner {} }
  278. public class Test
  279. {
  280. private $Inner t;
  281. }
  282. ", "Outer.Inner");
  283. }
  284. }
  285. }