Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit 8030bca

Browse files
committed
Added code fix tests
1 parent be0f368 commit 8030bca

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

PublicApiAnalyzer/PublicApiAnalyzer.Test/ApiDesign/DeclarePublicAPIAnalyzerTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
namespace PublicApiAnalyzer.Test.ApiDesign
55
{
66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Threading;
89
using System.Threading.Tasks;
10+
using Microsoft.CodeAnalysis.CodeActions;
911
using Microsoft.CodeAnalysis.CodeFixes;
1012
using Microsoft.CodeAnalysis.Diagnostics;
13+
using Microsoft.CodeAnalysis.Text;
1114
using PublicApiAnalyzer.ApiDesign;
1215
using TestHelper;
1316
using Xunit;
@@ -31,6 +34,11 @@ public class C
3134

3235
var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("C").WithLocation(2, 14);
3336
await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false);
37+
38+
string fixedApi = "C";
39+
var updatedApi = await this.GetUpdatedApiAsync(source, 0, CancellationToken.None).ConfigureAwait(false);
40+
41+
Assert.Equal(fixedApi, updatedApi.ToString());
3442
}
3543

3644
[Fact]
@@ -67,6 +75,26 @@ public void Method() { }
6775
};
6876

6977
await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false);
78+
79+
string fixedApi = "C";
80+
var updatedApi = await this.GetUpdatedApiAsync(source, 0, CancellationToken.None).ConfigureAwait(false);
81+
Assert.Equal(fixedApi, updatedApi.ToString());
82+
83+
fixedApi = "C.Field -> int";
84+
updatedApi = await this.GetUpdatedApiAsync(source, 1, CancellationToken.None).ConfigureAwait(false);
85+
Assert.Equal(fixedApi, updatedApi.ToString());
86+
87+
fixedApi = "C.Property.get -> int";
88+
updatedApi = await this.GetUpdatedApiAsync(source, 2, CancellationToken.None).ConfigureAwait(false);
89+
Assert.Equal(fixedApi, updatedApi.ToString());
90+
91+
fixedApi = "C.Property.set -> void";
92+
updatedApi = await this.GetUpdatedApiAsync(source, 3, CancellationToken.None).ConfigureAwait(false);
93+
Assert.Equal(fixedApi, updatedApi.ToString());
94+
95+
fixedApi = "C.Method() -> void";
96+
updatedApi = await this.GetUpdatedApiAsync(source, 4, CancellationToken.None).ConfigureAwait(false);
97+
Assert.Equal(fixedApi, updatedApi.ToString());
7098
}
7199

72100
[Fact]
@@ -275,5 +303,26 @@ protected override string GetUnshippedPublicApi()
275303
{
276304
return this.unshippedText;
277305
}
306+
307+
private async Task<SourceText> GetUpdatedApiAsync(string source, int diagnosticIndex, CancellationToken cancellationToken)
308+
{
309+
var fixes = await this.GetOfferedCSharpFixesAsync(source, diagnosticIndex, cancellationToken).ConfigureAwait(false);
310+
Assert.Equal(1, fixes.Item2.Length);
311+
312+
var operations = await fixes.Item2[0].GetOperationsAsync(CancellationToken.None).ConfigureAwait(false);
313+
Assert.Equal(1, operations.Length);
314+
ApplyChangesOperation operation = operations[0] as ApplyChangesOperation;
315+
Assert.NotNull(operation);
316+
317+
var oldSolution = fixes.Item1;
318+
var newSolution = operation.ChangedSolution;
319+
var solutionChanges = newSolution.GetChanges(oldSolution);
320+
var projectChanges = solutionChanges.GetProjectChanges().Single();
321+
var changedDocumentId = projectChanges.GetChangedAdditionalDocuments().Single();
322+
var newDocument = projectChanges.NewProject.GetAdditionalDocument(changedDocumentId);
323+
var newText = await newDocument.GetTextAsync(CancellationToken.None).ConfigureAwait(false);
324+
325+
return newText;
326+
}
278327
}
279328
}

PublicApiAnalyzer/PublicApiAnalyzer.Test/Verifiers/CodeFixVerifier.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public bool UseTabs
150150
/// <param name="diagnosticIndex">Index determining which diagnostic to use for determining the offered code fixes. Uses the first diagnostic if null.</param>
151151
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
152152
/// <returns>The collection of offered code actions. This collection may be empty.</returns>
153-
protected async Task<ImmutableArray<CodeAction>> GetOfferedCSharpFixesAsync(string source, int? diagnosticIndex = null, CancellationToken cancellationToken = default(CancellationToken))
153+
protected async Task<Tuple<Solution, ImmutableArray<CodeAction>>> GetOfferedCSharpFixesAsync(string source, int? diagnosticIndex = null, CancellationToken cancellationToken = default(CancellationToken))
154154
{
155155
return await this.GetOfferedFixesInternalAsync(LanguageNames.CSharp, source, diagnosticIndex, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), cancellationToken).ConfigureAwait(false);
156156
}
@@ -401,7 +401,7 @@ private async Task VerifyFixInternalAsync(
401401
Assert.Equal(newSource, actual);
402402
}
403403

404-
private async Task<ImmutableArray<CodeAction>> GetOfferedFixesInternalAsync(string language, string source, int? diagnosticIndex, ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, CancellationToken cancellationToken)
404+
private async Task<Tuple<Solution, ImmutableArray<CodeAction>>> GetOfferedFixesInternalAsync(string language, string source, int? diagnosticIndex, ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, CancellationToken cancellationToken)
405405
{
406406
var document = this.CreateDocument(source, language);
407407
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);
@@ -419,7 +419,7 @@ private async Task<ImmutableArray<CodeAction>> GetOfferedFixesInternalAsync(stri
419419
await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
420420
}
421421

422-
return actions.ToImmutableArray();
422+
return Tuple.Create(document.Project.Solution, actions.ToImmutableArray());
423423
}
424424
}
425425
}

0 commit comments

Comments
 (0)