Skip to content

Commit a77c8b6

Browse files
committed
Use SourceText as the content representation of both sources and additional files
1 parent d766801 commit a77c8b6

4 files changed

Lines changed: 41 additions & 33 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/GenericAnalyzerTest.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public string TestCode
9292

9393
public SourceFileList TestSources { get; }
9494

95-
public List<(string filename, string content)> AdditionalFiles { get; } = new List<(string filename, string content)>();
95+
public SourceFileCollection AdditionalFiles { get; } = new SourceFileCollection();
9696

97-
public List<(string filename, string content)> FixedAdditionalFiles { get; } = new List<(string filename, string content)>();
97+
public SourceFileCollection FixedAdditionalFiles { get; } = new SourceFileCollection();
9898

9999
public Dictionary<string, string> XmlReferences { get; } = new Dictionary<string, string>();
100100

@@ -150,14 +150,14 @@ public string BatchFixedCode
150150

151151
public List<Func<Solution, ProjectId, Solution>> SolutionTransforms { get; } = new List<Func<Solution, ProjectId, Solution>>();
152152

153-
public List<Func<(string filename, string content)[]>> AdditionalFilesFactories { get; } = new List<Func<(string filename, string content)[]>>();
153+
public List<Func<IEnumerable<(string filename, SourceText content)>>> AdditionalFilesFactories { get; } = new List<Func<IEnumerable<(string filename, SourceText content)>>>();
154154

155155
public async Task RunAsync(CancellationToken cancellationToken)
156156
{
157157
Assert.NotEmpty(this.TestSources);
158158

159159
var expected = this.ExpectedDiagnostics.ToArray();
160-
(string filename, string content)[] additionalFiles = this.AdditionalFiles.Concat(this.AdditionalFilesFactories.SelectMany(factory => factory())).ToArray();
160+
(string filename, SourceText content)[] additionalFiles = this.AdditionalFiles.Concat(this.AdditionalFilesFactories.SelectMany(factory => factory())).ToArray();
161161
await this.VerifyDiagnosticsAsync(this.TestSources.ToArray(), additionalFiles, expected, cancellationToken).ConfigureAwait(false);
162162
if (this.HasFixableDiagnostics())
163163
{
@@ -254,7 +254,7 @@ private static bool IsSubjectToExclusion(DiagnosticResult result)
254254
/// is run on the sources.</param>
255255
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
256256
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
257-
private async Task VerifyDiagnosticsAsync((string filename, SourceText content)[] sources, (string filename, string content)[] additionalFiles, DiagnosticResult[] expected, CancellationToken cancellationToken)
257+
private async Task VerifyDiagnosticsAsync((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, DiagnosticResult[] expected, CancellationToken cancellationToken)
258258
{
259259
var analyzers = this.GetDiagnosticAnalyzers().ToImmutableArray();
260260
VerifyDiagnosticResults(await this.GetSortedDiagnosticsAsync(sources, additionalFiles, analyzers, cancellationToken).ConfigureAwait(false), analyzers, expected);
@@ -351,7 +351,7 @@ private static Diagnostic[] SortDistinctDiagnostics(IEnumerable<Diagnostic> diag
351351
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
352352
/// <returns>A collection of <see cref="Diagnostic"/>s that surfaced in the source code, sorted by
353353
/// <see cref="Diagnostic.Location"/>.</returns>
354-
private Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsAsync((string filename, SourceText content)[] sources, (string filename, string content)[] additionalFiles, ImmutableArray<DiagnosticAnalyzer> analyzers, CancellationToken cancellationToken)
354+
private Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsAsync((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, ImmutableArray<DiagnosticAnalyzer> analyzers, CancellationToken cancellationToken)
355355
{
356356
return GetSortedDiagnosticsFromDocumentsAsync(analyzers, this.GetDocuments(sources, additionalFiles), cancellationToken);
357357
}
@@ -362,7 +362,7 @@ private Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsAsync((string filen
362362
/// </summary>
363363
/// <param name="sources">Classes in the form of strings.</param>
364364
/// <returns>A collection of <see cref="Document"/>s representing the sources.</returns>
365-
private Document[] GetDocuments((string filename, SourceText content)[] sources, (string filename, string content)[] additionalFiles)
365+
private Document[] GetDocuments((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles)
366366
{
367367
if (this.Language != LanguageNames.CSharp && this.Language != LanguageNames.VisualBasic)
368368
{
@@ -393,7 +393,7 @@ private Document[] GetDocuments((string filename, SourceText content)[] sources,
393393
/// <see cref="LanguageNames"/> class.</param>
394394
/// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
395395
/// strings.</returns>
396-
protected Project CreateProject((string filename, SourceText content)[] sources, (string filename, string content)[] additionalFiles, string language)
396+
protected Project CreateProject((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, string language)
397397
{
398398
Project project = this.CreateProjectImpl(sources, additionalFiles, language);
399399
return this.ApplyCompilationOptions(project);
@@ -408,7 +408,7 @@ protected Project CreateProject((string filename, SourceText content)[] sources,
408408
/// <see cref="LanguageNames"/> class.</param>
409409
/// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
410410
/// strings.</returns>
411-
protected virtual Project CreateProjectImpl((string filename, SourceText content)[] sources, (string filename, string content)[] additionalFiles, string language)
411+
protected virtual Project CreateProjectImpl((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, string language)
412412
{
413413
var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
414414
var solution = this.CreateSolution(projectId, language);
@@ -849,9 +849,9 @@ private async Task VerifyFixInternalAsync(
849849
ImmutableArray<DiagnosticAnalyzer> analyzers,
850850
ImmutableArray<CodeFixProvider> codeFixProviders,
851851
(string filename, SourceText content)[] oldSources,
852-
(string filename, string content)[] additionalFiles,
852+
(string filename, SourceText content)[] additionalFiles,
853853
(string filename, SourceText content)[] newSources,
854-
(string filename, string content)[] fixedAdditionalFiles,
854+
(string filename, SourceText content)[] fixedAdditionalFiles,
855855
int numberOfIterations,
856856
Func<ImmutableArray<DiagnosticAnalyzer>, ImmutableArray<CodeFixProvider>, int?, Project, int, CancellationToken, Task<Project>> getFixedProject,
857857
CancellationToken cancellationToken)
@@ -907,9 +907,9 @@ private async Task VerifyFixInternalAsync(
907907
for (int i = 0; i < updatedAdditionalDocuments.Length; i++)
908908
{
909909
var actual = await updatedAdditionalDocuments[i].GetTextAsync(cancellationToken).ConfigureAwait(false);
910-
Assert.Equal(fixedAdditionalFiles[i].content, actual.ToString());
911-
////Assert.Equal(fixedAdditionalFiles[i].content.Encoding, actual.Encoding);
912-
////Assert.Equal(fixedAdditionalFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm);
910+
Assert.Equal(fixedAdditionalFiles[i].content.ToString(), actual.ToString());
911+
Assert.Equal(fixedAdditionalFiles[i].content.Encoding, actual.Encoding);
912+
Assert.Equal(fixedAdditionalFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm);
913913
Assert.Equal(fixedAdditionalFiles[i].filename, updatedAdditionalDocuments[i].Name);
914914
}
915915
}
@@ -919,11 +919,6 @@ private static bool HasAnyChange((string filename, SourceText content)[] oldSour
919919
return !oldSources.SequenceEqual(newSources, SourceFileEqualityComparer.Instance);
920920
}
921921

922-
private static bool HasAnyChange((string filename, string content)[] additionalFiles, (string filename, string content)[] fixedAdditionalFiles)
923-
{
924-
return !additionalFiles.SequenceEqual(fixedAdditionalFiles);
925-
}
926-
927922
private static async Task<Project> FixEachAnalyzerDiagnosticAsync(ImmutableArray<DiagnosticAnalyzer> analyzers, ImmutableArray<CodeFixProvider> codeFixProviders, int? codeFixIndex, Project project, int numberOfIterations, CancellationToken cancellationToken)
928923
{
929924
var codeFixProvider = codeFixProviders.Single();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.Verifiers
5+
{
6+
using System.Collections.Generic;
7+
using Microsoft.CodeAnalysis.Text;
8+
9+
public class SourceFileCollection : List<(string filename, SourceText content)>
10+
{
11+
public void Add((string filename, string content) file)
12+
{
13+
this.Add((file.filename, SourceText.From(file.content)));
14+
}
15+
}
16+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/SourceFileList.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
namespace StyleCop.Analyzers.Test.Verifiers
55
{
6-
using System.Collections.Generic;
76
using Microsoft.CodeAnalysis.Text;
87

9-
public class SourceFileList : List<(string filename, SourceText content)>
8+
public class SourceFileList : SourceFileCollection
109
{
1110
private readonly string defaultPrefix;
1211
private readonly string defaultExtension;
@@ -26,10 +25,5 @@ public void Add(SourceText content)
2625
{
2726
this.Add(($"{this.defaultPrefix}{this.Count}.{this.defaultExtension}", content));
2827
}
29-
30-
public void Add((string filename, string content) file)
31-
{
32-
this.Add((file.filename, SourceText.From(file.content)));
33-
}
3428
}
3529
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopDiagnosticVerifier`1.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
namespace StyleCop.Analyzers.Test.Verifiers
55
{
66
using System;
7+
using System.Collections.Generic;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using global::LightJson;
1011
using global::LightJson.Serialization;
1112
using Microsoft.CodeAnalysis;
1213
using Microsoft.CodeAnalysis.Diagnostics;
1314
using Microsoft.CodeAnalysis.Formatting;
15+
using Microsoft.CodeAnalysis.Text;
1416
using StyleCop.Analyzers.Settings.ObjectModel;
1517
using TestHelper;
1618

@@ -57,7 +59,12 @@ public CSharpTest()
5759
.WithChangedOption(FormattingOptions.TabSize, this.Language, this.TabSize)
5860
.WithChangedOption(FormattingOptions.UseTabs, this.Language, this.UseTabs));
5961

60-
this.AdditionalFilesFactories.Add(() =>
62+
this.AdditionalFilesFactories.Add(GenerateSettingsFile);
63+
64+
return;
65+
66+
// Local function
67+
IEnumerable<(string filename, SourceText content)> GenerateSettingsFile()
6168
{
6269
var settings = this.Settings;
6370

@@ -96,13 +103,9 @@ public CSharpTest()
96103

97104
if (!string.IsNullOrEmpty(settings))
98105
{
99-
return new[] { (this.SettingsFileName, settings) };
100-
}
101-
else
102-
{
103-
return new (string filename, string content)[0];
106+
yield return (this.SettingsFileName, SourceText.From(settings));
104107
}
105-
});
108+
}
106109
}
107110

108111
/// <summary>

0 commit comments

Comments
 (0)