Skip to content

Commit 8f066e1

Browse files
committed
Introduced InspectionProvider
1 parent d9ae966 commit 8f066e1

File tree

9 files changed

+102
-70
lines changed

9 files changed

+102
-70
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using Rubberduck.Parsing.Inspections.Abstract;
3+
4+
namespace Rubberduck.Inspections
5+
{
6+
public interface IInspectionProvider
7+
{
8+
IEnumerable<IInspection> Inspections { get; }
9+
}
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Parsing.Inspections.Abstract;
4+
using Rubberduck.Settings;
5+
6+
namespace Rubberduck.Inspections
7+
{
8+
public class InspectionProvider : IInspectionProvider
9+
{
10+
public InspectionProvider(IEnumerable<IInspection> inspections)
11+
{
12+
var defaultSettings = new DefaultSettings<CodeInspectionSettings>().Default;
13+
var defaultNames = defaultSettings.CodeInspections.Select(x => x.Name);
14+
var defaultInspections = inspections.Where(inspection => defaultNames.Contains(inspection.Name));
15+
16+
foreach (var inspection in defaultInspections)
17+
{
18+
inspection.InspectionType = defaultSettings.CodeInspections.First(setting => setting.Name == inspection.Name).InspectionType;
19+
}
20+
21+
Inspections = inspections;
22+
}
23+
24+
public IEnumerable<IInspection> Inspections { get; }
25+
}
26+
}

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@
345345
<Compile Include="Common\WinAPI\WindowLongFlags.cs" />
346346
<Compile Include="Common\WindowsOperatingSystem.cs" />
347347
<Compile Include="Common\UndocumentedAttribute.cs" />
348+
<Compile Include="Inspections\IInspectionProvider.cs" />
349+
<Compile Include="Inspections\InspectionProvider.cs" />
348350
<Compile Include="Navigation\CodeExplorer\ICodeExplorerDeclarationViewModel.cs" />
349351
<Compile Include="Navigation\CodeMetrics\CodeMetricsAnalyst.cs" />
350352
<Compile Include="Navigation\CodeMetrics\CodeMetricsResult.cs" />

RetailCoder.VBE/Settings/CodeInspectionConfigProvider.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Rubberduck.Inspections;
34
using Rubberduck.SettingsProvider;
4-
using Rubberduck.Parsing.Inspections.Abstract;
55
using Rubberduck.Parsing.VBA;
66

77
namespace Rubberduck.Settings
@@ -12,21 +12,14 @@ public class CodeInspectionConfigProvider : IConfigProvider<CodeInspectionSettin
1212
private readonly CodeInspectionSettings _defaultSettings;
1313
private readonly HashSet<string> _foundInspectionNames;
1414

15-
public CodeInspectionConfigProvider(IPersistanceService<CodeInspectionSettings> persister, IEnumerable<IInspection> foundInspections)
15+
public CodeInspectionConfigProvider(IPersistanceService<CodeInspectionSettings> persister, IInspectionProvider inspectionProvider)
1616
{
1717
_persister = persister;
18-
_foundInspectionNames = foundInspections.Select(inspection => inspection.Name).ToHashSet();
18+
_foundInspectionNames = inspectionProvider.Inspections.Select(inspection => inspection.Name).ToHashSet();
1919
_defaultSettings = new DefaultSettings<CodeInspectionSettings>().Default;
2020

21-
var defaultNames = _defaultSettings.CodeInspections.Select(x => x.Name).ToHashSet();
22-
23-
var defaultInspections = foundInspections.Where(inspection => defaultNames.Contains(inspection.Name));
24-
var nonDefaultInspections = foundInspections.Except(defaultInspections);
25-
26-
foreach (var inspection in defaultInspections)
27-
{
28-
inspection.InspectionType = _defaultSettings.CodeInspections.First(setting => setting.Name == inspection.Name).InspectionType;
29-
}
21+
var defaultNames = _defaultSettings.CodeInspections.Select(x => x.Name);
22+
var nonDefaultInspections = inspectionProvider.Inspections.Where(inspection => !defaultNames.Contains(inspection.Name));
3023

3124
_defaultSettings.CodeInspections.UnionWith(nonDefaultInspections.Select(inspection => new CodeInspectionSetting(inspection)));
3225
}
@@ -40,10 +33,10 @@ public CodeInspectionSettings Create()
4033
return _defaultSettings;
4134
}
4235

36+
// Loaded settings don't contain defaults, so we need to combine user settings with defaults.
4337
var settings = new HashSet<CodeInspectionSetting>();
4438

45-
// Loaded settings don't contain defaults, so we need to combine user settings with defaults.
46-
foreach (var loadedSetting in loaded.CodeInspections.Where(inspection => _foundInspectionNames.Contains(inspection.Name)).Distinct())
39+
foreach (var loadedSetting in loaded.CodeInspections.Where(inspection => _foundInspectionNames.Contains(inspection.Name)))
4740
{
4841
var matchingDefaultSetting = _defaultSettings.CodeInspections.FirstOrDefault(inspection => inspection.Equals(loadedSetting));
4942
if (matchingDefaultSetting != null)

Rubberduck.Inspections/Inspector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class Inspector : IInspector
2727
private readonly List<IInspection> _inspections;
2828
private const int AGGREGATION_THRESHOLD = 128;
2929

30-
public Inspector(IGeneralConfigService configService, IEnumerable<IInspection> inspections)
30+
public Inspector(IGeneralConfigService configService, IInspectionProvider inspectionProvider)
3131
{
32-
_inspections = inspections.ToList();
32+
_inspections = inspectionProvider.Inspections.ToList();
3333

3434
_configService = configService;
3535
configService.SettingsChanged += ConfigServiceSettingsChanged;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Linq;
2+
using Moq;
3+
using NUnit.Framework;
4+
using Rubberduck.Inspections;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Parsing.Inspections.Resources;
7+
using Rubberduck.Settings;
8+
9+
namespace RubberduckTests.Inspections
10+
{
11+
[TestFixture]
12+
public class InspectionProviderTests
13+
{
14+
[Category("Inspections")]
15+
[Test]
16+
public void InspectionTypeIsAssignedFromDefaultSettingInConstructor()
17+
{
18+
var defaultSettings = new DefaultSettings<CodeInspectionSettings>().Default;
19+
var defaultSetting = defaultSettings.CodeInspections.First();
20+
defaultSetting.InspectionType = CodeInspectionType.Performance;
21+
22+
var inspectionMock = new Mock<IInspection>();
23+
inspectionMock.Setup(inspection => inspection.Name).Returns(defaultSetting.Name);
24+
inspectionMock.Setup(inspection => inspection.InspectionType).Returns(CodeInspectionType.CodeQualityIssues);
25+
26+
new InspectionProvider(new[] {inspectionMock.Object});
27+
28+
inspectionMock.VerifySet(inspection => inspection.InspectionType = CodeInspectionType.Performance);
29+
}
30+
}
31+
}

RubberduckTests/Inspections/InspectionsHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
using Rubberduck.Parsing.Inspections.Abstract;
44
using Rubberduck.Settings;
55
using System.Linq;
6+
using Rubberduck.Inspections;
67

78
namespace RubberduckTests.Inspections
89
{
910
public static class InspectionsHelper
1011
{
1112
public static IInspector GetInspector(IInspection inspection, params IInspection[] otherInspections)
1213
{
13-
return new Inspector(GetSettings(inspection), otherInspections.Union(new[] {inspection}));
14+
var inspectionProviderMock = new Mock<IInspectionProvider>();
15+
inspectionProviderMock.Setup(provider => provider.Inspections).Returns(otherInspections.Union(new[] {inspection}));
16+
17+
return new Inspector(GetSettings(inspection), inspectionProviderMock.Object);
1418
}
1519

1620
public static IGeneralConfigService GetSettings(IInspection inspection)

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="CodeExplorer\CodeExplorerTests.cs" />
107107
<Compile Include="Settings\CodeInspectionConfigProviderTests.cs" />
108108
<Compile Include="Inspections\DefTypeStatementInspectionTests.cs" />
109+
<Compile Include="Inspections\InspectionProviderTests.cs" />
109110
<Compile Include="VBEditor\ComSafeManagerTests.cs" />
110111
<Compile Include="VBEditor\ReferenceEqualityComparerTests.cs" />
111112
<Compile Include="VBEditor\ComSafeTests.cs" />

RubberduckTests/Settings/CodeInspectionConfigProviderTests.cs

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Moq;
44
using NUnit.Framework;
5+
using Rubberduck.Inspections;
56
using Rubberduck.Parsing.Inspections.Abstract;
67
using Rubberduck.Parsing.Inspections.Resources;
78
using Rubberduck.Settings;
@@ -18,7 +19,10 @@ public void SettingsForFoundInspectionsAreAddedToDefaultSettings()
1819
{
1920
var inspectionMock = new Mock<IInspection>();
2021
inspectionMock.Setup(inspection => inspection.Name).Returns(inspectionMock.Object.GetType().FullName);
21-
var configProvider = new CodeInspectionConfigProvider(null, new[] {inspectionMock.Object});
22+
var inspectionProviderMock = new Mock<IInspectionProvider>();
23+
inspectionProviderMock.Setup(provider => provider.Inspections).Returns(new[] {inspectionMock.Object});
24+
25+
var configProvider = new CodeInspectionConfigProvider(null, inspectionProviderMock.Object);
2226

2327
var defaults = configProvider.CreateDefaults();
2428

@@ -31,17 +35,19 @@ public void UserSettingsAreCombinedWithDefaultSettings()
3135
{
3236
var inspectionMock = new Mock<IInspection>();
3337
inspectionMock.Setup(inspection => inspection.Name).Returns("Foo");
38+
var inspectionProviderMock = new Mock<IInspectionProvider>();
39+
inspectionProviderMock.Setup(provider => provider.Inspections).Returns(new[] { inspectionMock.Object });
3440

3541
var userSetting = new CodeInspectionSetting("Foo", CodeInspectionType.CodeQualityIssues);
3642
var userSettings = new CodeInspectionSettings
3743
{
38-
CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})
44+
CodeInspections = new HashSet<CodeInspectionSetting>(new[] { userSetting })
3945
};
4046

4147
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
4248
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
4349

44-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, new[] {inspectionMock.Object});
50+
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, inspectionProviderMock.Object);
4551

4652
var settings = configProvider.Create().CodeInspections;
4753
var defaultSettings = configProvider.CreateDefaults().CodeInspections;
@@ -54,56 +60,38 @@ public void UserSettingsAreCombinedWithDefaultSettings()
5460
[Test]
5561
public void UserSettingsAreNotDuplicatedWithDefaultSettings()
5662
{
57-
var defaultSettings = new CodeInspectionConfigProvider(null, Enumerable.Empty<IInspection>()).CreateDefaults().CodeInspections;
63+
var inspectionProviderMock = new Mock<IInspectionProvider>();
64+
inspectionProviderMock.Setup(provider => provider.Inspections).Returns(Enumerable.Empty<IInspection>());
65+
66+
var defaultSettings = new CodeInspectionConfigProvider(null, inspectionProviderMock.Object).CreateDefaults().CodeInspections;
5867
var defaultSetting = defaultSettings.First();
5968

6069
var userSetting = new CodeInspectionSetting(defaultSetting.Name, defaultSetting.InspectionType);
6170
var userSettings = new CodeInspectionSettings
6271
{
63-
CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})
72+
CodeInspections = new HashSet<CodeInspectionSetting>(new[] { userSetting })
6473
};
6574

6675
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
6776
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
6877

69-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, Enumerable.Empty<IInspection>());
78+
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, inspectionProviderMock.Object);
7079

7180
var settings = configProvider.Create().CodeInspections;
7281

7382
Assert.AreEqual(defaultSettings.Count, settings.Count);
7483
Assert.Contains(userSetting, settings.ToArray());
7584
}
7685

77-
[Category("Settings")]
78-
[Test]
79-
public void UserSettingsInspectionTypeIsAssignedFromDefaultSetting()
80-
{
81-
var defaultSettings = new CodeInspectionConfigProvider(null, Enumerable.Empty<IInspection>()).CreateDefaults().CodeInspections;
82-
var defaultSetting = defaultSettings.First();
83-
defaultSetting.InspectionType = CodeInspectionType.CodeQualityIssues;
84-
85-
var userSetting = new CodeInspectionSetting(defaultSetting.Name, CodeInspectionType.LanguageOpportunities);
86-
var userSettings = new CodeInspectionSettings
87-
{
88-
CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})
89-
};
90-
91-
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
92-
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
93-
94-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, Enumerable.Empty<IInspection>());
95-
96-
var setting = configProvider.Create().CodeInspections.First(inspection => inspection.Equals(userSetting));
97-
98-
Assert.AreEqual(CodeInspectionType.CodeQualityIssues, setting.InspectionType);
99-
}
10086

10187
[Category("Settings")]
10288
[Test]
10389
public void UserSettingForUnknownInspectionIsIgnored()
10490
{
10591
var inspectionMock = new Mock<IInspection>();
10692
inspectionMock.Setup(inspection => inspection.Name).Returns("Foo");
93+
var inspectionProviderMock = new Mock<IInspectionProvider>();
94+
inspectionProviderMock.Setup(provider => provider.Inspections).Returns(new[] {inspectionMock.Object});
10795

10896
var userSetting = new CodeInspectionSetting("Bar", CodeInspectionType.CodeQualityIssues);
10997
var userSettings = new CodeInspectionSettings
@@ -114,34 +102,11 @@ public void UserSettingForUnknownInspectionIsIgnored()
114102
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
115103
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
116104

117-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, new[] {inspectionMock.Object});
105+
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, inspectionProviderMock.Object);
118106

119107
var settings = configProvider.Create().CodeInspections;
120108

121109
Assert.IsNull(settings.FirstOrDefault(setting => setting.Name == "Bar"));
122110
}
123-
124-
[Category("Settings")]
125-
[Test]
126-
public void DuplicateUserSettingIsIgnored()
127-
{
128-
var inspectionMock = new Mock<IInspection>();
129-
inspectionMock.Setup(inspection => inspection.Name).Returns("Foo");
130-
131-
var userSetting = new CodeInspectionSetting("Foo", CodeInspectionType.CodeQualityIssues);
132-
var userSettings = new CodeInspectionSettings
133-
{
134-
CodeInspections = new HashSet<CodeInspectionSetting>(new[] { userSetting })
135-
};
136-
137-
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
138-
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
139-
140-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, new[] { inspectionMock.Object, inspectionMock.Object });
141-
142-
var settings = configProvider.Create().CodeInspections;
143-
144-
Assert.AreEqual(1, settings.Count(setting => setting.Name == "Foo"));
145-
}
146111
}
147112
}

0 commit comments

Comments
 (0)