/src/Diagnostics/CodeAnalysis/Test/MetaAnalyzers/StartActionWithOnlyEndActionRuleTests.cs

https://gitlab.com/sharadag/TestProject2 · C# · 344 lines · 322 code · 21 blank · 1 comment · 1 complexity · e59cdb6f60541d779a4d419077fc5074 MD5 · raw file

  1. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using Microsoft.CodeAnalysis.Analyzers;
  3. using Microsoft.CodeAnalysis.CodeFixes;
  4. using Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers;
  5. using Microsoft.CodeAnalysis.Diagnostics;
  6. using Microsoft.CodeAnalysis.VisualBasic.Analyzers.MetaAnalyzers;
  7. using Xunit;
  8. namespace Microsoft.CodeAnalysis.UnitTests.Analyzers.MetaAnalyzers
  9. {
  10. public class StartActionWithOnlyEndActionRuleTests : CodeFixTestBase
  11. {
  12. [Fact]
  13. public void CSharp_VerifyDiagnostic()
  14. {
  15. var source = @"
  16. using System;
  17. using System.Collections.Immutable;
  18. using Microsoft.CodeAnalysis;
  19. using Microsoft.CodeAnalysis.CSharp;
  20. using Microsoft.CodeAnalysis.Diagnostics;
  21. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  22. class MyAnalyzer : DiagnosticAnalyzer
  23. {
  24. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
  25. {
  26. get
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31. public override void Initialize(AnalysisContext context)
  32. {
  33. context.RegisterCompilationStartAction(compilationContext =>
  34. {
  35. compilationContext.RegisterCompilationEndAction(null);
  36. });
  37. context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.InvocationExpression);
  38. context.RegisterCodeBlockStartAction<SyntaxKind>(AnalyzeCodeBlockStart);
  39. }
  40. private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
  41. {
  42. }
  43. private static void AnalyzeCodeBlockStart(CodeBlockStartAnalysisContext<SyntaxKind> codeBlockContext)
  44. {
  45. codeBlockContext.RegisterCodeBlockEndAction(null);
  46. }
  47. }";
  48. var expected = new[]
  49. {
  50. GetCSharpExpectedDiagnostic(21, 48, parameterName: "compilationContext", isCompilationStartAction: true),
  51. GetCSharpExpectedDiagnostic(34, 47, parameterName: "codeBlockContext", isCompilationStartAction: false)
  52. };
  53. VerifyCSharp(source, addLanguageSpecificCodeAnalysisReference: true, expected: expected);
  54. }
  55. [Fact]
  56. public void VisualBasic_VerifyDiagnostic()
  57. {
  58. var source = @"
  59. Imports System
  60. Imports System.Collections.Immutable
  61. Imports Microsoft.CodeAnalysis
  62. Imports Microsoft.CodeAnalysis.Diagnostics
  63. Imports Microsoft.CodeAnalysis.VisualBasic
  64. <DiagnosticAnalyzer(LanguageNames.VisualBasic)>
  65. Class MyAnalyzer
  66. Inherits DiagnosticAnalyzer
  67. Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor)
  68. Get
  69. Throw New NotImplementedException()
  70. End Get
  71. End Property
  72. Public Overrides Sub Initialize(context As AnalysisContext)
  73. context.RegisterCompilationStartAction(
  74. Sub(compilationContext As CompilationStartAnalysisContext)
  75. compilationContext.RegisterCompilationEndAction(Nothing)
  76. End Sub
  77. )
  78. context.RegisterSyntaxNodeAction(AddressOf AnalyzeSyntax, SyntaxKind.InvocationExpression)
  79. context.RegisterCodeBlockStartAction(Of SyntaxKind)(AddressOf AnalyzeCodeBlockStart)
  80. End Sub
  81. Private Shared Sub AnalyzeSyntax(context As SyntaxNodeAnalysisContext)
  82. End Sub
  83. Private Shared Sub AnalyzeCodeBlockStart(codeBlockContext As CodeBlockStartAnalysisContext(Of SyntaxKind))
  84. codeBlockContext.RegisterCodeBlockEndAction(Nothing)
  85. End Sub
  86. End Class
  87. ";
  88. var expected = new[]
  89. {
  90. GetBasicExpectedDiagnostic(19, 17, parameterName: "compilationContext", isCompilationStartAction: true),
  91. GetBasicExpectedDiagnostic(31, 46, parameterName: "codeBlockContext", isCompilationStartAction: false)
  92. };
  93. VerifyBasic(source, addLanguageSpecificCodeAnalysisReference: true, expected: expected);
  94. }
  95. [Fact]
  96. public void CSharp_NoDiagnosticCases()
  97. {
  98. var source = @"
  99. using System;
  100. using System.Collections.Immutable;
  101. using Microsoft.CodeAnalysis;
  102. using Microsoft.CodeAnalysis.CSharp;
  103. using Microsoft.CodeAnalysis.Diagnostics;
  104. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  105. abstract class MyAnalyzer<T> : DiagnosticAnalyzer
  106. where T : struct
  107. {
  108. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
  109. {
  110. get
  111. {
  112. throw new NotImplementedException();
  113. }
  114. }
  115. public override void Initialize(AnalysisContext context)
  116. {
  117. context.RegisterCompilationStartAction(compilationContext =>
  118. {
  119. compilationContext.RegisterCodeBlockStartAction<SyntaxKind>(AnalyzeCodeBlockStart);
  120. compilationContext.RegisterCompilationEndAction(null);
  121. });
  122. }
  123. private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
  124. {
  125. }
  126. private static void AnalyzeCodeBlockStart(CodeBlockStartAnalysisContext<SyntaxKind> context)
  127. {
  128. context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.InvocationExpression);
  129. context.RegisterCodeBlockEndAction(null);
  130. }
  131. }";
  132. VerifyCSharp(source, addLanguageSpecificCodeAnalysisReference: true);
  133. }
  134. [Fact]
  135. public void CSharp_NoDiagnosticCases_2()
  136. {
  137. var source = @"
  138. using System;
  139. using System.Collections.Immutable;
  140. using Microsoft.CodeAnalysis;
  141. using Microsoft.CodeAnalysis.CSharp;
  142. using Microsoft.CodeAnalysis.Diagnostics;
  143. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  144. abstract class MyAnalyzer<T> : DiagnosticAnalyzer
  145. where T : struct
  146. {
  147. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
  148. {
  149. get
  150. {
  151. throw new NotImplementedException();
  152. }
  153. }
  154. public override void Initialize(AnalysisContext context)
  155. {
  156. context.RegisterCompilationStartAction(compilationContext =>
  157. {
  158. compilationContext.RegisterCodeBlockStartAction<SyntaxKind>(codeBlockContext =>
  159. {
  160. AnalyzeCodeBlockStart(codeBlockContext);
  161. });
  162. compilationContext.RegisterCompilationEndAction(null);
  163. });
  164. }
  165. private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
  166. {
  167. }
  168. private static void AnalyzeCodeBlockStart(CodeBlockStartAnalysisContext<SyntaxKind> context)
  169. {
  170. context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.InvocationExpression);
  171. context.RegisterCodeBlockEndAction(null);
  172. }
  173. }";
  174. VerifyCSharp(source, addLanguageSpecificCodeAnalysisReference: true);
  175. }
  176. [Fact]
  177. public void VisualBasic_NoDiagnosticCases()
  178. {
  179. var source = @"
  180. Imports System
  181. Imports System.Collections.Immutable
  182. Imports Microsoft.CodeAnalysis
  183. Imports Microsoft.CodeAnalysis.Diagnostics
  184. Imports Microsoft.CodeAnalysis.VisualBasic
  185. <DiagnosticAnalyzer(LanguageNames.VisualBasic)>
  186. Class MyAnalyzer(Of T As Structure)
  187. Inherits DiagnosticAnalyzer
  188. Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor)
  189. Get
  190. Throw New NotImplementedException()
  191. End Get
  192. End Property
  193. Public Overrides Sub Initialize(context As AnalysisContext)
  194. context.RegisterCompilationStartAction(
  195. Sub(compilationContext As CompilationStartAnalysisContext)
  196. compilationContext.RegisterCodeBlockStartAction(Of SyntaxKind)(AddressOf AnalyzeCodeBlockStart)
  197. compilationContext.RegisterCompilationEndAction(Nothing)
  198. End Sub
  199. )
  200. End Sub
  201. Private Shared Sub AnalyzeSyntax(context As SyntaxNodeAnalysisContext)
  202. End Sub
  203. Private Shared Sub AnalyzeCodeBlockStart(context As CodeBlockStartAnalysisContext(Of SyntaxKind))
  204. context.RegisterSyntaxNodeAction(AddressOf AnalyzeSyntax, SyntaxKind.InvocationExpression)
  205. context.RegisterCodeBlockEndAction(Nothing)
  206. End Sub
  207. End Class
  208. ";
  209. VerifyBasic(source, addLanguageSpecificCodeAnalysisReference: true);
  210. }
  211. [Fact]
  212. public void VisualBasic_NoDiagnosticCases_2()
  213. {
  214. var source = @"
  215. Imports System
  216. Imports System.Collections.Immutable
  217. Imports Microsoft.CodeAnalysis
  218. Imports Microsoft.CodeAnalysis.Diagnostics
  219. Imports Microsoft.CodeAnalysis.VisualBasic
  220. <DiagnosticAnalyzer(LanguageNames.VisualBasic)>
  221. Class MyAnalyzer(Of T As Structure)
  222. Inherits DiagnosticAnalyzer
  223. Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor)
  224. Get
  225. Throw New NotImplementedException()
  226. End Get
  227. End Property
  228. Public Overrides Sub Initialize(context As AnalysisContext)
  229. context.RegisterCompilationStartAction(
  230. Sub(compilationContext As CompilationStartAnalysisContext)
  231. compilationContext.RegisterCodeBlockStartAction(Of SyntaxKind)(
  232. Sub(codeBlockContext As CodeBlockStartAnalysisContext(Of SyntaxKind))
  233. AnalyzeCodeBlockStart(codeBlockContext)
  234. End Sub
  235. )
  236. compilationContext.RegisterCompilationEndAction(Nothing)
  237. End Sub
  238. )
  239. End Sub
  240. Private Shared Sub AnalyzeSyntax(context As SyntaxNodeAnalysisContext)
  241. End Sub
  242. Private Shared Sub AnalyzeCodeBlockStart(context As CodeBlockStartAnalysisContext(Of SyntaxKind))
  243. context.RegisterSyntaxNodeAction(AddressOf AnalyzeSyntax, SyntaxKind.InvocationExpression)
  244. context.RegisterCodeBlockEndAction(Nothing)
  245. End Sub
  246. End Class
  247. ";
  248. VerifyBasic(source, addLanguageSpecificCodeAnalysisReference: true);
  249. }
  250. protected override CodeFixProvider GetCSharpCodeFixProvider()
  251. {
  252. return null;
  253. }
  254. protected override CodeFixProvider GetBasicCodeFixProvider()
  255. {
  256. return null;
  257. }
  258. protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
  259. {
  260. return new CSharpRegisterActionAnalyzer();
  261. }
  262. protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()
  263. {
  264. return new BasicRegisterActionAnalyzer();
  265. }
  266. private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column, string parameterName, bool isCompilationStartAction)
  267. {
  268. return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, parameterName, isCompilationStartAction);
  269. }
  270. private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column, string parameterName, bool isCompilationStartAction)
  271. {
  272. return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, parameterName, isCompilationStartAction);
  273. }
  274. private static DiagnosticResult GetExpectedDiagnostic(string language, int line, int column, string parameterName, bool isCompilationStartAction)
  275. {
  276. var endActionName = isCompilationStartAction ? "CompilationEndAction" : "CodeBlockEndAction";
  277. var statelessActionName = isCompilationStartAction ? "RegisterCompilationAction" : "RegisterCodeBlockAction";
  278. var arg4 = isCompilationStartAction ? "Initialize" : "Initialize, CompilationStartAction";
  279. var message = string.Format(CodeAnalysisDiagnosticsResources.StartActionWithOnlyEndActionMessage, parameterName, endActionName, statelessActionName, arg4);
  280. var fileName = language == LanguageNames.CSharp ? "Test0.cs" : "Test0.vb";
  281. return new DiagnosticResult
  282. {
  283. Id = DiagnosticIds.StartActionWithOnlyEndActionRuleId,
  284. Message = message,
  285. Severity = DiagnosticSeverity.Warning,
  286. Locations = new[]
  287. {
  288. new DiagnosticResultLocation(fileName, line, column)
  289. }
  290. };
  291. }
  292. }
  293. }