Skip to content

Commit d9ae966

Browse files
committed
Inspections will have their InspectionType assigned from application settings.
1 parent 54f0c3f commit d9ae966

File tree

3 files changed

+126
-12
lines changed

3 files changed

+126
-12
lines changed

RetailCoder.VBE/Settings/CodeInspectionConfigProvider.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,63 @@
22
using System.Linq;
33
using Rubberduck.SettingsProvider;
44
using Rubberduck.Parsing.Inspections.Abstract;
5+
using Rubberduck.Parsing.VBA;
56

67
namespace Rubberduck.Settings
78
{
89
public class CodeInspectionConfigProvider : IConfigProvider<CodeInspectionSettings>
910
{
1011
private readonly IPersistanceService<CodeInspectionSettings> _persister;
1112
private readonly CodeInspectionSettings _defaultSettings;
13+
private readonly HashSet<string> _foundInspectionNames;
1214

1315
public CodeInspectionConfigProvider(IPersistanceService<CodeInspectionSettings> persister, IEnumerable<IInspection> foundInspections)
1416
{
1517
_persister = persister;
18+
_foundInspectionNames = foundInspections.Select(inspection => inspection.Name).ToHashSet();
1619
_defaultSettings = new DefaultSettings<CodeInspectionSettings>().Default;
1720

18-
var nonDefaultInspections = foundInspections
19-
.Where(inspection => !_defaultSettings.CodeInspections.Select(x => x.Name).Contains(inspection.Name));
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+
}
2030

2131
_defaultSettings.CodeInspections.UnionWith(nonDefaultInspections.Select(inspection => new CodeInspectionSetting(inspection)));
2232
}
2333

2434
public CodeInspectionSettings Create()
2535
{
26-
// Loaded settings don't contain defaults, so we need to combine user settings with defaults.
2736
var loaded = _persister.Load(_defaultSettings);
28-
loaded?.CodeInspections.UnionWith(_defaultSettings.CodeInspections);
2937

30-
return loaded ?? _defaultSettings;
38+
if (loaded == null)
39+
{
40+
return _defaultSettings;
41+
}
42+
43+
var settings = new HashSet<CodeInspectionSetting>();
44+
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())
47+
{
48+
var matchingDefaultSetting = _defaultSettings.CodeInspections.FirstOrDefault(inspection => inspection.Equals(loadedSetting));
49+
if (matchingDefaultSetting != null)
50+
{
51+
loadedSetting.InspectionType = matchingDefaultSetting.InspectionType;
52+
}
53+
54+
settings.Add(loadedSetting);
55+
}
56+
57+
settings.UnionWith(_defaultSettings.CodeInspections.Where(inspection => !settings.Contains(inspection)));
58+
59+
loaded.CodeInspections = settings;
60+
61+
return loaded;
3162
}
3263

3364
public CodeInspectionSettings CreateDefaults()

RetailCoder.VBE/Settings/CodeInspectionSettings.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public CodeInspectionSetting(IInspectionModel inspection)
154154

155155
public override bool Equals(object obj)
156156
{
157-
return obj is CodeInspectionSetting inspectionSetting &&
158-
inspectionSetting.InspectionType == InspectionType &&
159-
inspectionSetting.Name == Name;
157+
return obj is CodeInspectionSetting inspectionSetting && inspectionSetting.Name == Name;
160158
}
161159

162160
public override int GetHashCode()

RubberduckTests/Settings/CodeInspectionConfigProviderTests.cs

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ public void SettingsForFoundInspectionsAreAddedToDefaultSettings()
2929
[Test]
3030
public void UserSettingsAreCombinedWithDefaultSettings()
3131
{
32+
var inspectionMock = new Mock<IInspection>();
33+
inspectionMock.Setup(inspection => inspection.Name).Returns("Foo");
34+
3235
var userSetting = new CodeInspectionSetting("Foo", CodeInspectionType.CodeQualityIssues);
33-
var userSettings = new CodeInspectionSettings { CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})};
36+
var userSettings = new CodeInspectionSettings
37+
{
38+
CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})
39+
};
40+
3441
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
3542
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
36-
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, Enumerable.Empty<IInspection>());
43+
44+
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, new[] {inspectionMock.Object});
3745

3846
var settings = configProvider.Create().CodeInspections;
3947
var defaultSettings = configProvider.CreateDefaults().CodeInspections;
@@ -47,16 +55,93 @@ public void UserSettingsAreCombinedWithDefaultSettings()
4755
public void UserSettingsAreNotDuplicatedWithDefaultSettings()
4856
{
4957
var defaultSettings = new CodeInspectionConfigProvider(null, Enumerable.Empty<IInspection>()).CreateDefaults().CodeInspections;
50-
var userSetting = defaultSettings.First();
51-
var userSettings = new CodeInspectionSettings { CodeInspections = new HashSet<CodeInspectionSetting>(new[] { userSetting }) };
58+
var defaultSetting = defaultSettings.First();
59+
60+
var userSetting = new CodeInspectionSetting(defaultSetting.Name, defaultSetting.InspectionType);
61+
var userSettings = new CodeInspectionSettings
62+
{
63+
CodeInspections = new HashSet<CodeInspectionSetting>(new[] {userSetting})
64+
};
65+
5266
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
5367
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
68+
5469
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, Enumerable.Empty<IInspection>());
5570

5671
var settings = configProvider.Create().CodeInspections;
5772

5873
Assert.AreEqual(defaultSettings.Count, settings.Count);
5974
Assert.Contains(userSetting, settings.ToArray());
6075
}
76+
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+
}
100+
101+
[Category("Settings")]
102+
[Test]
103+
public void UserSettingForUnknownInspectionIsIgnored()
104+
{
105+
var inspectionMock = new Mock<IInspection>();
106+
inspectionMock.Setup(inspection => inspection.Name).Returns("Foo");
107+
108+
var userSetting = new CodeInspectionSetting("Bar", CodeInspectionType.CodeQualityIssues);
109+
var userSettings = new CodeInspectionSettings
110+
{
111+
CodeInspections = new HashSet<CodeInspectionSetting>(new[] { userSetting })
112+
};
113+
114+
var persisterMock = new Mock<IPersistanceService<CodeInspectionSettings>>();
115+
persisterMock.Setup(persister => persister.Load(It.IsAny<CodeInspectionSettings>())).Returns(userSettings);
116+
117+
var configProvider = new CodeInspectionConfigProvider(persisterMock.Object, new[] {inspectionMock.Object});
118+
119+
var settings = configProvider.Create().CodeInspections;
120+
121+
Assert.IsNull(settings.FirstOrDefault(setting => setting.Name == "Bar"));
122+
}
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+
}
61146
}
62147
}

0 commit comments

Comments
 (0)