-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCSharpCodeFixProviderTest.cs
More file actions
77 lines (66 loc) · 2.11 KB
/
CSharpCodeFixProviderTest.cs
File metadata and controls
77 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Testing.Model;
using Shouldly;
namespace Roslyn.Testing.CodeFix;
public abstract class CSharpCodeFixProviderTest<TDiagnosticAnalyzer, TCodeFixProvider> : FileReaderTest
where TDiagnosticAnalyzer : DiagnosticAnalyzer, new()
where TCodeFixProvider : CodeFixProvider, new()
{
#region To be implemented by Test classes
/// <inheritdoc />
public override string Filepath =>
_codeFixProvider.GetType()
.Name;
/// <inheritdoc />
public override string PathToTestData => "./TestData/CodeFix/";
protected virtual IReadOnlyCollection<MetadataReference> GetAdditionalReferences() => [];
#endregion
private readonly TCodeFixProvider _codeFixProvider = new();
private readonly TDiagnosticAnalyzer _diagnosticAnalyzer = new();
/// <summary>
/// Called to test a C# codefix when applied on the inputted string as a source
/// </summary>
/// <param name="oldSource">
/// A class in the form of a string before the CodeFix was
/// applied to it
/// </param>
/// <param name="newSource">
/// A class in the form of a string after the CodeFix was
/// applied to it
/// </param>
/// <param name="codeFixIndex">
/// Index determining which codefix to apply if there
/// are multiple
/// </param>
/// <param name="allowNewCompilerDiagnostics">
/// A bool controlling whether or not the test will fail if the CodeFix introduces
/// other warnings after being applied
/// </param>
protected void VerifyFix(string oldSource,
string newSource,
int? codeFixIndex = null,
bool allowNewCompilerDiagnostics = false)
{
var result = _codeFixProvider.VerifyFix(LanguageNames.CSharp,
_diagnosticAnalyzer,
oldSource,
newSource,
codeFixIndex,
allowNewCompilerDiagnostics,
GetAdditionalReferences());
if (result.Success)
{
return;
}
if (string.IsNullOrEmpty(result.ErrorMessage))
{
result.NewSource.ShouldContainWithoutWhitespace(result.ActualSource);
} else
{
result.Success.ShouldBeTrue(result.ErrorMessage);
}
}
}