Skip to content

Commit 7946d06

Browse files
authored
Merge pull request #2957 from Hosch250/refactorInspections
Add check to ensure an attempted fix can fix the result
2 parents f23f935 + 6745681 commit 7946d06

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

Rubberduck.Inspections/QuickFixProvider.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ public IEnumerable<IQuickFix> QuickFixes(IInspectionResult result)
3636
return _quickFixes[result.Inspection.GetType()];
3737
}
3838

39+
private bool CanFix(IQuickFix fix, Type inspection)
40+
{
41+
return _quickFixes.ContainsKey(inspection) && _quickFixes[inspection].Contains(fix);
42+
}
43+
3944
public void Fix(IQuickFix fix, IInspectionResult result)
4045
{
46+
if (!CanFix(fix, result.Inspection.GetType()))
47+
{
48+
throw new ArgumentException("Fix does not support this inspection.", nameof(result));
49+
}
50+
4151
fix.Fix(result);
4252
_state.GetRewriter(result.QualifiedSelection.QualifiedName).Rewrite();
4353
_state.OnParseRequested(this);
@@ -46,11 +56,21 @@ public void Fix(IQuickFix fix, IInspectionResult result)
4656
public void FixInProcedure(IQuickFix fix, QualifiedSelection selection, Type inspectionType,
4757
IEnumerable<IInspectionResult> results)
4858
{
59+
if (!CanFix(fix, inspectionType))
60+
{
61+
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
62+
}
63+
4964
throw new NotImplementedException("A qualified selection does not state which proc we are in, so we could really only use this on inspection results that expose a Declaration.");
5065
}
5166

5267
public void FixInModule(IQuickFix fix, QualifiedSelection selection, Type inspectionType, IEnumerable<IInspectionResult> results)
5368
{
69+
if (!CanFix(fix, inspectionType))
70+
{
71+
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
72+
}
73+
5474
var filteredResults = results
5575
.Where(result => result.Inspection.GetType() == inspectionType
5676
&& result.QualifiedSelection.QualifiedName == selection.QualifiedName)
@@ -71,6 +91,11 @@ public void FixInModule(IQuickFix fix, QualifiedSelection selection, Type inspec
7191
public void FixInProject(IQuickFix fix, QualifiedSelection selection, Type inspectionType,
7292
IEnumerable<IInspectionResult> results)
7393
{
94+
if (!CanFix(fix, inspectionType))
95+
{
96+
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
97+
}
98+
7499
var filteredResults = results
75100
.Where(result => result.Inspection.GetType() == inspectionType
76101
&& result.QualifiedSelection.QualifiedName.ProjectId == selection.QualifiedName.ProjectId)
@@ -95,6 +120,11 @@ public void FixInProject(IQuickFix fix, QualifiedSelection selection, Type inspe
95120

96121
public void FixAll(IQuickFix fix, Type inspectionType, IEnumerable<IInspectionResult> results)
97122
{
123+
if (!CanFix(fix, inspectionType))
124+
{
125+
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
126+
}
127+
98128
var filteredResults = results.Where(result => result.Inspection.GetType() == inspectionType);
99129

100130
foreach (var result in filteredResults)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Linq;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Rubberduck.Inspections;
5+
using Rubberduck.Inspections.Concrete;
6+
using Rubberduck.Inspections.QuickFixes;
7+
using Rubberduck.Parsing.Inspections.Abstract;
8+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
9+
using RubberduckTests.Mocks;
10+
11+
namespace RubberduckTests.Inspections
12+
{
13+
[TestClass]
14+
public class QuickFixProviderTests
15+
{
16+
[TestMethod]
17+
[ExpectedException(typeof(ArgumentException))]
18+
[TestCategory("Inspections")]
19+
public void ProviderDoesNotKnowAboutInspection()
20+
{
21+
const string inputCode =
22+
@"Public Sub Foo()
23+
Const const1 As Integer = 9
24+
End Sub";
25+
26+
IVBComponent component;
27+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
28+
var state = MockParser.CreateAndParse(vbe.Object);
29+
30+
var inspection = new ConstantNotUsedInspection(state);
31+
var inspectionResults = inspection.GetInspectionResults();
32+
33+
var quickFixProvider = new QuickFixProvider(state, new IQuickFix[] {});
34+
quickFixProvider.Fix(new OptionExplicitQuickFix(state), inspectionResults.First());
35+
}
36+
37+
[TestMethod]
38+
[ExpectedException(typeof(ArgumentException))]
39+
[TestCategory("Inspections")]
40+
public void ProviderKnowsAboutInspection()
41+
{
42+
const string inputCode =
43+
@"Public Sub Foo()
44+
Const const1 As Integer = 9
45+
End Sub";
46+
47+
IVBComponent component;
48+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
49+
var state = MockParser.CreateAndParse(vbe.Object);
50+
51+
var inspection = new ConstantNotUsedInspection(state);
52+
var inspectionResults = inspection.GetInspectionResults();
53+
54+
var quickFixProvider = new QuickFixProvider(state, new IQuickFix[] {new RemoveUnusedDeclarationQuickFix(state)});
55+
quickFixProvider.Fix(new OptionExplicitQuickFix(state), inspectionResults.First());
56+
}
57+
}
58+
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Inspections\MemberNotOnInterfaceInspectionTests.cs" />
101101
<Compile Include="Inspections\OptionBaseZeroInspectionTests.cs" />
102102
<Compile Include="Inspections\PassParameterByReferenceQuickFixTests.cs" />
103+
<Compile Include="Inspections\QuickFixProviderTests.cs" />
103104
<Compile Include="Inspections\UndeclaredVariableInspectionTests.cs" />
104105
<Compile Include="PostProcessing\ModuleRewriterTests.cs" />
105106
<Compile Include="SmartIndenter\VerticalSpacingTests.cs" />

0 commit comments

Comments
 (0)