forked from FortuneN/FineCodeCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppOptionsProvider_Tests.cs
More file actions
480 lines (413 loc) · 22 KB
/
AppOptionsProvider_Tests.cs
File metadata and controls
480 lines (413 loc) · 22 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMoq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Microsoft.VisualStudio.Settings;
using Moq;
using NUnit.Framework;
namespace FineCodeCoverageTests
{
public class AppOptionsProvider_Tests
{
private AutoMoqer autoMocker;
private AppOptionsProvider appOptionsProvider;
private Mock<WritableSettingsStore> mockWritableSettingsStore;
[SetUp]
public void Setup()
{
autoMocker = new AutoMoqer();
appOptionsProvider = autoMocker.Create<AppOptionsProvider>();
mockWritableSettingsStore = new Mock<WritableSettingsStore>();
var mockWritableUserSettingsStoreProvider = autoMocker.GetMock<IWritableUserSettingsStoreProvider>();
mockWritableUserSettingsStoreProvider.Setup(
writableSettingsStoreProvider => writableSettingsStoreProvider.Provide()
).Returns(mockWritableSettingsStore.Object);
}
[Test]
public void Should_Ensure_Store_When_LoadSettingsFromStorage()
{
appOptionsProvider.LoadSettingsFromStorage(new Mock<IAppOptions>().Object);
mockWritableSettingsStore.Verify(writableSettingsStore => writableSettingsStore.CreateCollection("FineCodeCoverage"));
}
[Test]
public void Should_Not_Create_Settings_Collection_If_Already_Exists()
{
mockWritableSettingsStore.Setup(writableSettingsStore => writableSettingsStore.CollectionExists("FineCodeCoverage")).Returns(true);
appOptionsProvider.LoadSettingsFromStorage(new Mock<IAppOptions>().Object);
mockWritableSettingsStore.Verify(writableSettingsStore => writableSettingsStore.CreateCollection("FineCodeCoverage"), Times.Never());
}
[Test]
public void Should_Ensure_Store_When_SaveSettingsToStorage()
{
appOptionsProvider.SaveSettingsToStorage(new Mock<IAppOptions>().Object);
mockWritableSettingsStore.Verify(writableSettingsStore => writableSettingsStore.CreateCollection("FineCodeCoverage"));
}
[Test]
public void Should_Not_Create_Settings_Collection_If_Already_Exists_When_SaveSettingsToStorage()
{
mockWritableSettingsStore.Setup(writableSettingsStore => writableSettingsStore.CollectionExists("FineCodeCoverage")).Returns(true);
appOptionsProvider.SaveSettingsToStorage(new Mock<IAppOptions>().Object);
mockWritableSettingsStore.Verify(writableSettingsStore => writableSettingsStore.CreateCollection("FineCodeCoverage"), Times.Never());
}
[Test]
public void Should_Have_Default_AppOptions_Property_When_Load_And_Does_Not_Exist_In_Storage()
{
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
mockJsonConvertService.Setup(
jsonConvertService =>
jsonConvertService.DeserializeObject(It.IsAny<string>(), typeof(string[]))
).Returns(new string[] { });
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", nameof(IAppOptions.AttributesExclude))
).Returns(false);
var mockAppOptions = new Mock<IAppOptions>();
mockAppOptions.DefaultValueProvider = new NullStringArrayDefaultValueProvider();
mockAppOptions.SetupAllProperties();
var appOptions = mockAppOptions.Object;
appOptionsProvider.LoadSettingsFromStorage(appOptions);
Assert.Null(appOptions.AttributesExclude);
mockWritableSettingsStore.VerifyAll();
}
[Test]
public void Should_Default_NamespacedClasses_True()
{
DefaultTest(appOptions => appOptions.NamespacedClasses = true);
}
private void DefaultTest(Action<IAppOptions> verifyOptions)
{
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", It.IsAny<string>())
).Returns(false);
var mockAppOptions = new Mock<IAppOptions>();
appOptionsProvider.LoadSettingsFromStorage(mockAppOptions.Object);
mockAppOptions.VerifySet(verifyOptions);
}
[Test]
public void Should_Default_Thresholds()
{
DefaultTest(appOptions => appOptions.ThresholdForCrapScore = 15);
DefaultTest(appOptions => appOptions.ThresholdForNPathComplexity = 200);
DefaultTest(appOptions => appOptions.ThresholdForCyclomaticComplexity = 30);
}
[Test]
public void Should_Default_RunSettingsOnly_True()
{
DefaultTest(appOptions => appOptions.RunSettingsOnly = true);
}
[Test]
public void Should_Default_RunWhenTestsFail_True()
{
DefaultTest(appOptions => appOptions.RunWhenTestsFail = true);
}
[Test]
public void Should_Default_ExcludeByAttribute_GeneratedCode()
{
DefaultTest(appOptions => appOptions.ExcludeByAttribute = new[] { "GeneratedCode" });
}
[Test]
public void Should_Default_IncludeTestAssembly_True()
{
DefaultTest(appOptions => appOptions.IncludeTestAssembly = true);
}
[Test]
public void Should_Default_ExcludeByFile_Migrations()
{
DefaultTest(appOptions => appOptions.ExcludeByFile = new[] { "**/Migrations/*" });
}
[Test]
public void Should_Default_Enabled_True()
{
DefaultTest(appOptions => appOptions.Enabled = true);
}
[Test]
public void Should_Default_DisabledNoCoverage_True()
{
DefaultTest(appOptions => appOptions.DisabledNoCoverage = true);
}
[Test]
public void Should_Default_True_ShowCoverageInOverviewMargin()
{
DefaultTest(appOptions => appOptions.ShowCoverageInOverviewMargin = true);
}
[Test]
public void Should_Default_True_ShowCoveredInOverviewMargin()
{
DefaultTest(appOptions => appOptions.ShowCoveredInOverviewMargin = true);
}
[Test]
public void Should_Default_True_ShowUncoveredInOverviewMargin()
{
DefaultTest(appOptions => appOptions.ShowUncoveredInOverviewMargin = true);
}
[Test]
public void Should_Default_True_ShowPartiallyCoveredInOverviewMargin()
{
DefaultTest(appOptions => appOptions.ShowPartiallyCoveredInOverviewMargin = true);
}
[Test]
public void Should_Not_Default_Any_Other_AppOptions_Properties()
{
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", It.IsAny<string>())
).Returns(false);
var mockAppOptions = new Mock<IAppOptions>();
appOptionsProvider.LoadSettingsFromStorage(mockAppOptions.Object);
var invocationNames = mockAppOptions.Invocations.Select(invocation => invocation.Method.Name).ToList();
var expectedSetters = new List<string>
{
nameof(IAppOptions.Enabled),
nameof(IAppOptions.ExcludeByFile),
nameof(IAppOptions.IncludeTestAssembly),
nameof(IAppOptions.ExcludeByAttribute),
nameof(IAppOptions.RunWhenTestsFail),
nameof(IAppOptions.RunSettingsOnly),
nameof(IAppOptions.ThresholdForCrapScore),
nameof(IAppOptions.ThresholdForNPathComplexity),
nameof(IAppOptions.ThresholdForCyclomaticComplexity),
nameof(IAppOptions.RunMsCodeCoverage),
nameof(IAppOptions.NamespacedClasses),
nameof(IAppOptions.ShowCoverageInOverviewMargin),
nameof(IAppOptions.ShowCoveredInOverviewMargin),
nameof(IAppOptions.ShowUncoveredInOverviewMargin),
nameof(IAppOptions.ShowPartiallyCoveredInOverviewMargin),
nameof(IAppOptions.ShowToolWindowToolbar),
nameof(IAppOptions.Hide0Coverable),
nameof(IAppOptions.DisabledNoCoverage),
nameof(IAppOptions.ShowEditorCoverage),
nameof(IAppOptions.ShowCoverageInGlyphMargin),
nameof(IAppOptions.ShowCoveredInGlyphMargin),
nameof(IAppOptions.ShowUncoveredInGlyphMargin),
nameof(IAppOptions.ShowPartiallyCoveredInGlyphMargin),
nameof(IAppOptions.ShowLineCoveredHighlighting),
nameof(IAppOptions.ShowLinePartiallyCoveredHighlighting),
nameof(IAppOptions.ShowLineUncoveredHighlighting),
nameof(IAppOptions.UseEnterpriseFontsAndColors)
};
CollectionAssert.AreEquivalent(expectedSetters.Select(s => $"set_{s}"), invocationNames);
}
[TestCase(null)]
[TestCase(" ")]
public void Should_Have_Default_AppOptions_Property_When_Load_And_Is_Null_Or_Whitespace_In_Storage(string nullOrWhitespace)
{
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
mockJsonConvertService.Setup(
jsonConvertService =>
jsonConvertService.DeserializeObject(It.IsAny<string>(), typeof(string[]))
).Returns(new string[] { });
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", nameof(IAppOptions.AttributesExclude))
).Returns(true);
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.GetString("FineCodeCoverage", nameof(IAppOptions.AttributesExclude))
).Returns(nullOrWhitespace);
var mockAppOptions = new Mock<IAppOptions>();
mockAppOptions.DefaultValueProvider = new NullStringArrayDefaultValueProvider();
mockAppOptions.SetupAllProperties();
var appOptions = mockAppOptions.Object;
appOptionsProvider.LoadSettingsFromStorage(appOptions);
Assert.Null(appOptions.AttributesExclude);
mockWritableSettingsStore.VerifyAll();
}
[Test]
public void Should_Use_Deseralized_String_From_Store_For_AppOption_Property_LoadSettingsFromStorage()
{
var mockAppOptions = new Mock<IAppOptions>();
mockAppOptions.DefaultValueProvider = new NullStringArrayDefaultValueProvider();
mockAppOptions.SetupAllProperties();
var appOptions = mockAppOptions.Object;
Should_Use_Deseralized_String_From_Store_For_AppOption_Property(() =>
{
appOptionsProvider.LoadSettingsFromStorage(appOptions);
return appOptions;
});
}
[Test]
public void Should_Use_Deseralized_String_From_Store_For_AppOption_Property_Get()
{
Should_Use_Deseralized_String_From_Store_For_AppOption_Property(() =>
{
var appOptions = appOptionsProvider.Get();
return appOptions;
});
}
internal void Should_Use_Deseralized_String_From_Store_For_AppOption_Property(Func<IAppOptions> act)
{
Dictionary<string, object> deserializedValues = new Dictionary<string, object>
{
{ nameof(IAppOptions.AdjacentBuildOutput), false},
{ nameof(IAppOptions.AttributesExclude), new string[]{ "aexclude"}},
{ nameof(IAppOptions.AttributesInclude), new string[]{ "ainclude"}},
{ nameof(IAppOptions.CompanyNamesExclude), new string[]{ "cexclude"}},
{ nameof(IAppOptions.CompanyNamesInclude), new string[]{ "cinclude"}},
{ nameof(IAppOptions.CoverletCollectorDirectoryPath), "p"},
{ nameof(IAppOptions.CoverletConsoleCustomPath), "cp"},
{ nameof(IAppOptions.CoverletConsoleGlobal), true},
{ nameof(IAppOptions.CoverletConsoleLocal), true},
{ nameof(IAppOptions.Enabled), true},
{ nameof(IAppOptions.DisabledNoCoverage), true},
{ nameof(IAppOptions.Exclude), new string[]{"exclude" } },
{ nameof(IAppOptions.ExcludeByAttribute), new string[]{ "ebyatt"} },
{ nameof(IAppOptions.ExcludeByFile), new string[]{ "ebyfile"} },
{ nameof(IAppOptions.FCCSolutionOutputDirectoryName), "FCCSolutionOutputDirectoryName"},
{ nameof(IAppOptions.FunctionsExclude), new string[]{ "FunctionsExclude" } },
{ nameof(IAppOptions.FunctionsInclude), new string[]{ "FunctionsInclude" } },
{ nameof(IAppOptions.HideFullyCovered), true },
{ nameof(IAppOptions.Hide0Coverable),true },
{ nameof(IAppOptions.Hide0Coverage),true },
{ nameof(IAppOptions.Include), new string[]{ "Include" } },
{ nameof(IAppOptions.IncludeReferencedProjects),true},
{ nameof(IAppOptions.IncludeTestAssembly),true},
{ nameof(IAppOptions.ModulePathsExclude),new string[]{ "ModulePathsExclude" }},
{ nameof(IAppOptions.ModulePathsInclude),new string[]{ "ModulePathsInclude" }},
{ nameof(IAppOptions.NamespacedClasses),true},
{ nameof(IAppOptions.OpenCoverCustomPath),"OpenCoverCustomPath"},
{ nameof(IAppOptions.PublicKeyTokensExclude),new string[]{ "PublicKeyTokensExclude" }},
{ nameof(IAppOptions.PublicKeyTokensInclude),new string[]{ "PublicKeyTokensInclude" }},
{ nameof(IAppOptions.RunInParallel),true},
{ nameof(IAppOptions.RunSettingsOnly),true},
{ nameof(IAppOptions.RunWhenTestsExceed),1},
{ nameof(IAppOptions.RunWhenTestsFail),true},
{ nameof(IAppOptions.SourcesExclude),new string[]{ "SourcesExclude" }},
{ nameof(IAppOptions.SourcesInclude),new string[]{ "SourcesInclude" }},
{ nameof(IAppOptions.StickyCoverageTable),true},
{ nameof(IAppOptions.ThresholdForCrapScore),1},
{ nameof(IAppOptions.ThresholdForCyclomaticComplexity),1},
{ nameof(IAppOptions.ThresholdForNPathComplexity),1},
{ nameof(IAppOptions.ToolsDirectory),"ToolsDirectory"},
{ nameof(IAppOptions.RunMsCodeCoverage), RunMsCodeCoverage.IfInRunSettings},
{ nameof(IAppOptions.ShowCoverageInOverviewMargin),true},
{ nameof(IAppOptions.ShowCoveredInOverviewMargin),true},
{ nameof(IAppOptions.ShowPartiallyCoveredInOverviewMargin),true},
{ nameof(IAppOptions.ShowDirtyInOverviewMargin), true },
{ nameof(IAppOptions.ShowNewInOverviewMargin), true },
{ nameof(IAppOptions.ShowUncoveredInOverviewMargin),true},
{ nameof(IAppOptions.ShowNotIncludedInOverviewMargin),true},
{ nameof(IAppOptions.ShowToolWindowToolbar),true},
{nameof(IAppOptions.ExcludeAssemblies),new string[]{ "Exclude"} },
{nameof(IAppOptions.IncludeAssemblies),new string[]{ "Include"} },
{nameof(IAppOptions.NamespaceQualification),NamespaceQualification.AlwaysUnqualified },
{nameof(IAppOptions.OpenCoverRegister),OpenCoverRegister.Default },
{nameof(IAppOptions.OpenCoverTarget),"" },
{nameof(IAppOptions.OpenCoverTargetArgs),"" },
{nameof(IAppOptions.ShowEditorCoverage),true },
{nameof(IAppOptions.ShowCoverageInGlyphMargin),true },
{nameof(IAppOptions.ShowCoveredInGlyphMargin),true },
{nameof(IAppOptions.ShowPartiallyCoveredInGlyphMargin),true },
{nameof(IAppOptions.ShowUncoveredInGlyphMargin),true },
{nameof(IAppOptions.ShowDirtyInGlyphMargin),true },
{nameof(IAppOptions.ShowNewInGlyphMargin),true },
{nameof(IAppOptions.ShowNotIncludedInGlyphMargin),true },
{nameof(IAppOptions.ShowLineCoverageHighlighting),true },
{nameof(IAppOptions.ShowLineCoveredHighlighting),true },
{nameof(IAppOptions.ShowLinePartiallyCoveredHighlighting),true },
{nameof(IAppOptions.ShowLineUncoveredHighlighting),true },
{nameof(IAppOptions.ShowLineDirtyHighlighting),true },
{nameof(IAppOptions.ShowLineNewHighlighting),true },
{nameof(IAppOptions.ShowLineNotIncludedHighlighting),true },
{nameof(IAppOptions.UseEnterpriseFontsAndColors),true },
{nameof(IAppOptions.EditorCoverageColouringMode), EditorCoverageColouringMode.UseRoslynWhenTextChanges },
{nameof(IAppOptions.BlazorCoverageLinesFromGeneratedSource), true }
};
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
mockJsonConvertService.Setup(
jsonConvertService =>
jsonConvertService.DeserializeObject(It.IsAny<string>(), It.IsAny<Type>())
).Returns<string, Type>((serializedValueFromStore, _) => {
if (deserializedValues.ContainsKey(serializedValueFromStore))
{
return deserializedValues[serializedValueFromStore];
}
return null;
});
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", It.IsAny<string>())
).Returns(true);
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.GetString("FineCodeCoverage", It.IsAny<string>())
).Returns<string, string>((_, propertyName) => propertyName);
var appOptions = act();
var appOptionsPropertyInfos = typeof(IAppOptions).GetPublicProperties();
foreach(var appOptionsPropertyInfo in appOptionsPropertyInfos)
{
if (appOptionsPropertyInfo.PropertyType.IsValueType)
{
Assert.AreEqual(deserializedValues[appOptionsPropertyInfo.Name], appOptionsPropertyInfo.GetValue(appOptions));
}
else
{
Assert.AreSame(deserializedValues[appOptionsPropertyInfo.Name], appOptionsPropertyInfo.GetValue(appOptions));
}
}
}
[Test]
public void Should_Log_Exception_Thrown_In_LoadSettingsFromStorage()
{
var exception = new Exception("msg");
string _propertyName = null;
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.PropertyExists("FineCodeCoverage", It.IsAny<string>())
).Returns(true);
mockWritableSettingsStore.Setup(
writableSettingsStore => writableSettingsStore.GetString("FineCodeCoverage", It.IsAny<string>())
).Callback<string,string>((_,propertyName) => _propertyName = propertyName).Throws(exception);
appOptionsProvider.LoadSettingsFromStorage(new Mock<IAppOptions>().Object);
autoMocker.Verify<ILogger>(logger => logger.Log($"Failed to load '{_propertyName}' setting", exception));
}
[Test]
public void IAppOptions_Should_Have_A_Getter_And_Setter_For_Each_Property()
{
var propertyInfos = typeof(IAppOptions).GetPublicProperties();
Assert.True(propertyInfos.All(pi => pi.GetMethod != null && pi.SetMethod != null));
}
[Test]
public void Should_Write_The_Serialized_Property_Value_To_The_Store()
{
var propertyValue = new string[] { "CompanyNamesExclude" };
var mockAppOptions = new Mock<IAppOptions>();
mockAppOptions.SetupGet(appOptions => appOptions.CompanyNamesExclude).Returns(propertyValue);
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
mockJsonConvertService.Setup(
jsonConvertService =>
jsonConvertService.SerializeObject(propertyValue)
).Returns("Serialized");
appOptionsProvider.SaveSettingsToStorage(mockAppOptions.Object);
mockWritableSettingsStore.Verify(
writableSettingsStore => writableSettingsStore.SetString("FineCodeCoverage", nameof(IAppOptions.CompanyNamesExclude), "Serialized")
);
}
[Test]
public void Should_Raise_Options_Changed_When_SaveSettingsToStorage()
{
var mockAppOptions = new Mock<IAppOptions>();
var appOptions = mockAppOptions.Object;
IAppOptions changedOptions = null;
appOptionsProvider.OptionsChanged += (options) =>
{
changedOptions = options;
};
appOptionsProvider.SaveSettingsToStorage(appOptions);
Assert.AreSame(appOptions, changedOptions);
}
[Test]
public void Should_Log_If_Exception_When_SaveSettingsToStorage()
{
var exception = new Exception();
mockWritableSettingsStore.Setup(
writeableSettingsStore => writeableSettingsStore.SetString("FineCodeCoverage", nameof(IAppOptions.Enabled), It.IsAny<string>())
).Throws(exception);
var mockAppOptions = new Mock<IAppOptions>();
appOptionsProvider.SaveSettingsToStorage(mockAppOptions.Object);
autoMocker.Verify<ILogger>(logger => logger.Log($"Failed to save '{nameof(IAppOptions.Enabled)}' setting", exception));
}
}
internal class NullStringArrayDefaultValueProvider : LookupOrFallbackDefaultValueProvider
{
public NullStringArrayDefaultValueProvider()
{
base.Register(typeof(string[]), (_, __) => null);
}
}
}