PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs

https://github.com/EkardNT/Roslyn
C# | 362 lines | 307 code | 25 blank | 30 comment | 0 complexity | 1981b9479c23a2e314e321eab489bf9d MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Collections.Immutable;
  3. using System.Linq;
  4. using Microsoft.CodeAnalysis.CSharp.Syntax;
  5. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  6. using Microsoft.CodeAnalysis.Test.Utilities;
  7. using Roslyn.Test.Utilities;
  8. using Roslyn.Utilities;
  9. using Xunit;
  10. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  11. {
  12. public class GetUnusedImportDirectivesTests : SemanticModelTestBase
  13. {
  14. [Fact]
  15. public void UnusedUsing1()
  16. {
  17. var text = @"
  18. using System;
  19. class C
  20. {
  21. void Foo()
  22. {
  23. }
  24. }";
  25. var tree = Parse(text);
  26. var comp = CreateCompilationWithMscorlib(tree);
  27. comp.VerifyDiagnostics(
  28. // (2,1): info CS8019: Unnecessary using directive.
  29. // using System;
  30. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;"));
  31. }
  32. [WorkItem(865627, "DevDiv")]
  33. [Fact]
  34. public void TestUnusedExtensionMarksImportsAsUsed()
  35. {
  36. string class1Source = @"using System;
  37. namespace ClassLibrary1
  38. {
  39. public class Class1
  40. {
  41. public void Method1(string arg1)
  42. {
  43. Console.WriteLine(arg1);
  44. }
  45. }
  46. }
  47. ";
  48. var classLib1 = CreateCompilationWithMscorlib(text: class1Source, references: new[] { SystemRef }, assemblyName: "ClassLibrary1");
  49. string class2Source = @"using System;
  50. using ClassLibrary1;
  51. namespace ClassLibrary2
  52. {
  53. public static class Class2
  54. {
  55. public static void Method1(this Class1 arg0, string arg1)
  56. {
  57. Console.Write(""Erroneous: "" + arg1);
  58. }
  59. }
  60. }";
  61. var classLib2 = CreateCompilationWithMscorlib(text: class2Source, assemblyName: "ClassLibrary2", references: new[] { SystemRef, SystemCoreRef, classLib1.ToMetadataReference() });
  62. string consoleApplicationSource = @"using ClassLibrary2;
  63. using ClassLibrary1;
  64. namespace ConsoleApplication
  65. {
  66. class Program
  67. {
  68. static void Main(string[] args)
  69. {
  70. var instance1 = new Class1();
  71. instance1.Method1(""Argument1"");
  72. }
  73. }
  74. }";
  75. var tree = Parse(consoleApplicationSource);
  76. var comp = CreateCompilationWithMscorlib(tree, new[] { SystemRef, SystemCoreRef, classLib1.ToMetadataReference(), classLib2.ToMetadataReference() }, assemblyName: "ConsoleApplication");
  77. var model = comp.GetSemanticModel(tree) as CSharpSemanticModel;
  78. var syntax = tree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>().Single().Expression;
  79. //This is the crux of the test.
  80. //Without this line, with or without the fix, the model never gets pushed to evaluate extension method candidates
  81. //and therefor never marked ClassLibrary2 as a used import in consoleApplication.
  82. //Without the fix, this call used to result in ClassLibrary2 getting marked as used, after the fix, this call does not
  83. //result in changing ClassLibrary2's used status.
  84. model.GetMemberGroup(syntax);
  85. model.GetDiagnostics().Verify(Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using ClassLibrary2;"));
  86. }
  87. [WorkItem(747219, "DevDiv2/DevDiv")]
  88. [Fact]
  89. public void UnusedUsing747219()
  90. {
  91. var text = @"
  92. using System;
  93. using System.Collections.Generic;
  94. using System.Linq;
  95. using System.Threading.Tasks;
  96. class Program
  97. {
  98. static void Main(string[] args)
  99. {
  100. Enumerable.Repeat(1, 1);
  101. }
  102. }";
  103. var tree = Parse(text);
  104. var comp = CreateCompilationWithMscorlib(tree);
  105. //all unused because system.core was not included and Eunmerable didn't bind
  106. comp.VerifyDiagnostics(
  107. // (4,14): error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)
  108. // using System.Linq;
  109. Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "Linq").WithArguments("Linq", "System"),
  110. // (11,9): error CS0103: The name 'Enumerable' does not exist in the current context
  111. // Enumerable.Repeat(1, 1);
  112. Diagnostic(ErrorCode.ERR_NameNotInContext, "Enumerable").WithArguments("Enumerable"),
  113. // (4,1): info CS8019: Unnecessary using directive.
  114. // using System.Linq;
  115. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Linq;"),
  116. // (2,1): info CS8019: Unnecessary using directive.
  117. // using System;
  118. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;"),
  119. // (3,1): info CS8019: Unnecessary using directive.
  120. // using System.Collections.Generic;
  121. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Collections.Generic;"),
  122. // (5,1): info CS8019: Unnecessary using directive.
  123. // using System.Threading.Tasks;
  124. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Threading.Tasks;")
  125. );
  126. comp = comp.WithReferences(comp.References.Concat(SystemCoreRef));
  127. comp.VerifyDiagnostics(
  128. // (2,1): info CS8019: Unnecessary using directive.
  129. // using System;
  130. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;"),
  131. // (3,1): info CS8019: Unnecessary using directive.
  132. // using System.Collections.Generic;
  133. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Collections.Generic;"),
  134. // (5,1): info CS8019: Unnecessary using directive.
  135. // using System.Threading.Tasks;
  136. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Threading.Tasks;")
  137. );
  138. }
  139. [Fact]
  140. public void UsedUsing1()
  141. {
  142. var text = @"
  143. using System;
  144. class C
  145. {
  146. void Foo()
  147. {
  148. Console.WriteLine();
  149. }
  150. }";
  151. var tree = Parse(text);
  152. var comp = CreateCompilationWithMscorlib(tree);
  153. comp.VerifyDiagnostics();
  154. }
  155. [Fact]
  156. public void SpeculativeBindingDoesNotAffectResult()
  157. {
  158. var text = @"
  159. using System;
  160. class C
  161. {
  162. void Foo()
  163. {
  164. /*here*/
  165. }
  166. }";
  167. var tree = Parse(text);
  168. var comp = CreateCompilationWithMscorlib(tree);
  169. var model = comp.GetSemanticModel(tree);
  170. var position = text.IndexOf("/*here*/");
  171. var info = model.GetSpeculativeSymbolInfo(position, SyntaxFactory.IdentifierName("Console"), SpeculativeBindingOption.BindAsTypeOrNamespace);
  172. Assert.NotNull(info.Symbol);
  173. Assert.Equal(SymbolKind.NamedType, info.Symbol.Kind);
  174. Assert.Equal("Console", info.Symbol.Name);
  175. Assert.Equal(SymbolKind.Namespace, info.Symbol.ContainingSymbol.Kind);
  176. Assert.Equal("System", info.Symbol.ContainingSymbol.Name);
  177. comp.VerifyDiagnostics(
  178. // (2,1): info CS8019: Unnecessary using directive.
  179. // using System;
  180. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;")
  181. );
  182. }
  183. [Fact]
  184. public void AllAssemblyLevelAttributesMustBeBound()
  185. {
  186. var snkPath = Temp.CreateFile().WriteAllBytes(TestResources.SymbolsTests.General.snKey).Path;
  187. var signing = Parse(@"
  188. using System.Reflection;
  189. [assembly: AssemblyVersion(""1.2.3.4"")]
  190. [assembly: AssemblyKeyFile(@""" + snkPath + @""")]
  191. ");
  192. var ivtCompilation = CreateCompilationWithMscorlib(
  193. assemblyName: "IVT",
  194. compOptions: TestOptions.Dll.WithStrongNameProvider(new DesktopStrongNameProvider()),
  195. references: new[] {SystemCoreRef},
  196. trees: new[]
  197. {
  198. Parse(@"
  199. using System.Runtime.CompilerServices;
  200. [assembly: InternalsVisibleTo(""Lib, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb"")]
  201. namespace NamespaceContainingInternalsOnly
  202. {
  203. internal static class Extensions
  204. {
  205. internal static void Foo(this int x) {}
  206. }
  207. }
  208. "),
  209. signing
  210. });
  211. var libCompilation = CreateCompilationWithMscorlib(
  212. assemblyName: "Lib",
  213. compOptions: TestOptions.Dll.WithStrongNameProvider(new DesktopStrongNameProvider()),
  214. references: new[] { ivtCompilation.ToMetadataReference() },
  215. trees: new[]
  216. {
  217. Parse(@"
  218. using NamespaceContainingInternalsOnly;
  219. public class C
  220. {
  221. internal static void F(int x)
  222. {
  223. x.Foo();
  224. }
  225. }
  226. "),
  227. signing
  228. });
  229. libCompilation.VerifyDiagnostics();
  230. }
  231. [WorkItem(747219, "DevDiv")]
  232. [Fact]
  233. public void SemanticModelCallDoesNotCountsAsUse()
  234. {
  235. var source = @"
  236. using System.Collections;
  237. using System.Collections.Generic;
  238. class C
  239. {
  240. void M()
  241. {
  242. return;
  243. }
  244. }";
  245. var comp = CreateCompilationWithMscorlib(source);
  246. comp.VerifyDiagnostics(
  247. // (2,1): info CS8019: Unnecessary using directive.
  248. // using System.Collections;
  249. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Collections;"),
  250. // (3,1): info CS8019: Unnecessary using directive.
  251. // using System.Collections.Generic;
  252. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Collections.Generic;")
  253. );
  254. }
  255. [WorkItem(747219, "DevDiv")]
  256. [Fact]
  257. public void INF_UnusedUsingDirective()
  258. {
  259. var source = @"
  260. using System.Collections;
  261. using C = System.Console;
  262. ";
  263. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  264. // (2,1): info CS8019: Unnecessary using directive.
  265. // using System.Collections;
  266. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Collections;"),
  267. // (3,1): info CS8019: Unnecessary using directive.
  268. // using C = System.Console;
  269. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using C = System.Console;"));
  270. }
  271. [WorkItem(747219, "DevDiv")]
  272. [Fact]
  273. public void INF_UnusedExternAlias()
  274. {
  275. var source = @"
  276. extern alias A;
  277. ";
  278. var lib = CreateCompilation("", assemblyName: "lib");
  279. var comp = CreateCompilationWithMscorlib(source, new[] { new CSharpCompilationReference(lib, aliases: ImmutableArray.Create("A")) });
  280. comp.VerifyDiagnostics(
  281. // (2,1): info CS8020: Unused extern alias.
  282. // extern alias A;
  283. Diagnostic(ErrorCode.INF_UnusedExternAlias, "extern alias A;"));
  284. }
  285. [WorkItem(747219, "DevDiv")]
  286. [Fact]
  287. public void CrefCountsAsUse()
  288. {
  289. var source = @"
  290. using System;
  291. /// <see cref='Console'/>
  292. public class C { }
  293. ";
  294. // Not binding doc comments.
  295. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  296. // (2,1): info CS8019: Unnecessary using directive.
  297. // using System;
  298. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;"));
  299. // Binding doc comments.
  300. CreateCompilationWithMscorlibAndDocumentationComments(source).VerifyDiagnostics();
  301. }
  302. [WorkItem(770147, "DevDiv")]
  303. [Fact]
  304. public void InfoAndWarnAsError()
  305. {
  306. var source = @"
  307. using System;
  308. ";
  309. var comp = CreateCompilationWithMscorlib(source, compOptions: TestOptions.Dll.WithGeneralDiagnosticOption(ReportDiagnostic.Error));
  310. comp.VerifyEmitDiagnostics(
  311. // (2,1): info CS8019: Unnecessary using directive.
  312. // using System;
  313. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System;").WithWarningAsError(false));
  314. }
  315. }
  316. }