|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using System.IO; |
| 3 | +using CodeDocumentor.Vsix2022; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 6 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | + |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace CodeDocumentor.Test |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// The class unit test. |
| 14 | + /// </summary> |
| 15 | + |
| 16 | + public partial class ClassUnitTest : CodeFixVerifier, IClassFixture<TestFixure> |
| 17 | + { |
| 18 | + private readonly TestFixure _fixture; |
| 19 | + |
| 20 | + public ClassUnitTest(TestFixure fixture) |
| 21 | + { |
| 22 | + _fixture = fixture; |
| 23 | + TestFixture.BuildOptionsPageGrid(); |
| 24 | + CodeDocumentorPackage.Options.DefaultDiagnosticSeverity = DiagnosticSeverity.Warning; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Nos diagnostics show. |
| 29 | + /// </summary> |
| 30 | + /// <param name="testCode">The test code.</param> |
| 31 | + [Theory] |
| 32 | + [InlineData("")] |
| 33 | + [InlineData("ClassTesterInheritDoc.cs")] |
| 34 | + public void NoDiagnosticsShow(string testCode) |
| 35 | + { |
| 36 | + var file = string.IsNullOrEmpty(testCode)? testCode : _fixture.LoadTestFile($@"./Classes/{testCode}"); |
| 37 | + this.VerifyCSharpDiagnostic(file); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Shows diagnostic and fix. |
| 42 | + /// </summary> |
| 43 | + /// <param name="testCode">The test code.</param> |
| 44 | + /// <param name="fixCode">The fix code.</param> |
| 45 | + /// <param name="line">The line.</param> |
| 46 | + /// <param name="column">The column.</param> |
| 47 | + [Theory] |
| 48 | + [InlineData("ClassTester.cs", "ClassTesterFix.cs", 7, 19, TestFixure.DIAG_TYPE_PRIVATE)] |
| 49 | + [InlineData("PublicClassTester.cs", "PublicClassTesterFix.cs", 7, 26, TestFixure.DIAG_TYPE_PUBLIC)] |
| 50 | + public void ShowDiagnosticAndFix(string testCode, string fixCode, int line, int column, string diagType) |
| 51 | + { |
| 52 | + var fix = _fixture.LoadTestFile($@"./Classes/{fixCode}"); |
| 53 | + var test = _fixture.LoadTestFile($@"./Classes/{testCode}"); |
| 54 | + |
| 55 | + var expected = new DiagnosticResult |
| 56 | + { |
| 57 | + Id = ClassAnalyzerSettings.DiagnosticId, |
| 58 | + Message = ClassAnalyzerSettings.MessageFormat, |
| 59 | + Severity = DiagnosticSeverity.Warning, |
| 60 | + Locations = |
| 61 | + new[] { |
| 62 | + new DiagnosticResultLocation("Test0.cs", line, column) |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + this.VerifyCSharpDiagnostic(test, diagType, expected); |
| 67 | + |
| 68 | + this.VerifyCSharpFix(test, fix, diagType); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets c sharp code fix provider. |
| 73 | + /// </summary> |
| 74 | + /// <returns>A CodeFixProvider.</returns> |
| 75 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 76 | + { |
| 77 | + return new ClassCodeFixProvider(); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets c sharp diagnostic analyzer. |
| 82 | + /// </summary> |
| 83 | + /// <returns>A DiagnosticAnalyzer.</returns> |
| 84 | + protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer(string diagType) |
| 85 | + { |
| 86 | + if (diagType == TestFixure.DIAG_TYPE_PRIVATE) |
| 87 | + { |
| 88 | + CodeDocumentorPackage.Options.IsEnabledForPublishMembersOnly = false; |
| 89 | + return new NonPublicClassAnalyzer(); |
| 90 | + } |
| 91 | + CodeDocumentorPackage.Options.IsEnabledForPublishMembersOnly = true; |
| 92 | + return new ClassAnalyzer(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments