Skip to content

Commit 71cde21

Browse files
author
Gurpreet Singh
committed
improving code coverage
1 parent 39b701f commit 71cde21

File tree

14 files changed

+169
-75
lines changed

14 files changed

+169
-75
lines changed

coverage-report.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dotnet test ./src `
2+
--collect:"XPlat Code Coverage" `
3+
--results-directory ./TestResults
4+
5+
reportgenerator `
6+
-reports:./TestResults/**/coverage.cobertura.xml `
7+
-targetdir:./CoverageReport `
8+
-reporttypes:Html
9+
10+
Remove-Item -Recurse -Force ./TestResults

src/TestStack.BDDfy.Tests/Configuration/BatchProcessorsTests.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using Shouldly;
34
using TestStack.BDDfy.Configuration;
5+
using TestStack.BDDfy.Reporters.Diagnostics;
46
using TestStack.BDDfy.Reporters.Html;
57
using TestStack.BDDfy.Reporters.MarkDown;
8+
using TestStack.BDDfy.Tests.Concurrency;
69
using Xunit;
710

811
namespace TestStack.BDDfy.Tests.Configuration
912
{
13+
[Collection(TestCollectionName.ModifiesConfigurator)]
1014
public class BatchProcessorsTests
1115
{
1216
static bool MetroReportProcessorIsActive(IBatchProcessor batchProcessor)
1317
{
14-
return batchProcessor is HtmlReporter && ((HtmlReporter)batchProcessor).ReportBuilder is MetroReportBuilder;
18+
return batchProcessor is HtmlReporter reporter && reporter.ReportBuilder is MetroReportBuilder;
1519
}
1620

1721
[Fact]
@@ -67,5 +71,35 @@ public void ReturnsHtmlMetroReporterWhenItIsActivated()
6771

6872
Configurator.BatchProcessors.HtmlMetroReport.Disable();
6973
}
74+
75+
[Fact]
76+
public void ReturnsDianosticsReporterWhenItIsActivated()
77+
{
78+
Configurator.BatchProcessors.DiagnosticsReport.Enable();
79+
80+
var processors = Configurator.BatchProcessors.GetProcessors().ToList();
81+
82+
processors.ShouldContain(p=> p is DiagnosticsReporter, 1);
83+
84+
Configurator.BatchProcessors.DiagnosticsReport.Disable();
85+
}
86+
87+
[Fact]
88+
public void ReturnsAdditionalBatchProcessorsWhenAdded()
89+
{
90+
Configurator.BatchProcessors.Add(new FooBatchProcessor());
91+
92+
var processors = Configurator.BatchProcessors.GetProcessors().ToList();
93+
94+
processors.ShouldContain(p => p is FooBatchProcessor, 1);
95+
}
96+
97+
private class FooBatchProcessor : IBatchProcessor
98+
{
99+
public void Process(IEnumerable<Story> stories)
100+
{
101+
throw new System.NotImplementedException();
102+
}
103+
}
70104
}
71105
}

src/TestStack.BDDfy.Tests/Configuration/ProcessorPipelineTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
using TestStack.BDDfy.Configuration;
44
using TestStack.BDDfy.Processors;
55
using TestStack.BDDfy.Reporters;
6+
using TestStack.BDDfy.Tests.Concurrency;
67
using Xunit;
78

89
namespace TestStack.BDDfy.Tests.Configuration
910
{
10-
[Collection("ExclusiveAccessToConfigurator")]
11+
[Collection(TestCollectionName.ModifiesConfigurator)]
1112
public class ProcessorPipelineTests
1213
{
1314
[Fact]

src/TestStack.BDDfy.Tests/Configuration/StepExecutorTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Text;
22
using Shouldly;
33
using TestStack.BDDfy.Configuration;
4+
using TestStack.BDDfy.Tests.Concurrency;
45
using Xunit;
56

67
namespace TestStack.BDDfy.Tests.Configuration
78
{
8-
[Collection("ExclusiveAccessToConfigurator")]
9+
[Collection(TestCollectionName.ModifiesConfigurator)]
910
public class StepExecutorTests
1011
{
1112
private class TestStepExecutor : StepExecutor

src/TestStack.BDDfy.Tests/ExclusiveAccessToConfiguratorFixture.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using TestStack.BDDfy.Tests.Concurrency;
2+
using Xunit;
3+
4+
namespace TestStack.BDDfy.Tests
5+
{
6+
[CollectionDefinition(TestCollectionName.ModifiesConfigurator, DisableParallelization = true)]
7+
public class ModifiesConfiguratorFixture
8+
{
9+
}
10+
}

src/TestStack.BDDfy.Tests/Scanner/FluentScanner/ComplexStepsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
22
using System.Linq;
33
using Shouldly;
4+
using TestStack.BDDfy.Tests.Concurrency;
45
using TestStack.BDDfy.Tests.Configuration;
56
using Xunit;
67

78
namespace TestStack.BDDfy.Tests.Scanner.FluentScanner
89
{
9-
[Collection("ExclusiveAccessToConfigurator")]
10+
[Collection(TestCollectionName.ModifiesConfigurator)]
1011
public class ComplexStepsTests
1112
{
1213
private int count;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Shouldly;
2+
using System.Linq;
3+
using TestStack.BDDfy.Configuration;
4+
using Xunit;
5+
6+
namespace TestStack.BDDfy.Tests.Scanner.ReflectiveScanner
7+
{
8+
public class AnyExecutableAttributeWhichIsEmptyOrWhitespaceWillResultInMethodName
9+
{
10+
private class TypeWithDecoratedMethods
11+
{
12+
public TypeWithDecoratedMethods()
13+
{
14+
this.WithExamples(new ExampleTable("Example")
15+
{
16+
{1},
17+
{2}
18+
});
19+
}
20+
21+
[Given("")]
22+
public void GivenWithEmptyString() { }
23+
[When(" ")]
24+
public void WhenWithWhitespace() { }
25+
[Then(null)]
26+
public void ThenWithNull() { }
27+
28+
[AndThen("Then with title should work too")]
29+
public void AndThenWithTitle() { }
30+
}
31+
32+
[Fact]
33+
public void WhenUsingReflectiveScanner_MethodNamesAreUsedAsStepTitles_When_StepTitlesAreWhiteSpace()
34+
{
35+
var testObject = new TypeWithDecoratedMethods();
36+
37+
var stepScanners = Configurator.Scanners.GetStepScanners(testObject).ToArray();
38+
var scanner = new ReflectiveScenarioScanner(stepScanners);
39+
var scenario = scanner.Scan(TestContext.GetContext(testObject)).First();
40+
var steps = scenario.Steps;
41+
42+
steps[0].Title.ShouldBe("Given with empty string");
43+
steps[1].Title.ShouldBe("When with whitespace");
44+
steps[2].Title.ShouldBe("Then with null");
45+
steps[3].Title.ShouldBe("Then with title should work too");
46+
}
47+
}
48+
}

src/TestStack.BDDfy/Configuration/BatchProcessors.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using TestStack.BDDfy.Processors;
43
using TestStack.BDDfy.Reporters.Diagnostics;
54
using TestStack.BDDfy.Reporters.Html;
@@ -9,7 +8,9 @@ namespace TestStack.BDDfy.Configuration
98
{
109
public class BatchProcessors
1110
{
12-
IEnumerable<IBatchProcessor> _GetProcessors()
11+
readonly List<IBatchProcessor> _addedProcessors = [];
12+
13+
private IEnumerable<IBatchProcessor> YieldProcessors()
1314
{
1415
var htmlReporter = HtmlReport.ConstructFor(StoryCache.Stories);
1516
if (htmlReporter != null)
@@ -33,19 +34,14 @@ IEnumerable<IBatchProcessor> _GetProcessors()
3334
}
3435
}
3536

36-
private readonly BatchProcessorFactory _htmlReportFactory = new(() => new HtmlReporter(new DefaultHtmlReportConfiguration()));
37-
public BatchProcessorFactory HtmlReport { get { return _htmlReportFactory; } }
38-
39-
private readonly BatchProcessorFactory _htmlMetroReportFactory = new(() => new HtmlReporter(new DefaultHtmlReportConfiguration(), new MetroReportBuilder()), false);
40-
public BatchProcessorFactory HtmlMetroReport { get { return _htmlMetroReportFactory; } }
37+
public BatchProcessorFactory HtmlReport { get; } = new(() => new HtmlReporter(new DefaultHtmlReportConfiguration()));
4138

42-
private readonly BatchProcessorFactory _markDownFactory = new(() => new MarkDownReporter(), false);
43-
public BatchProcessorFactory MarkDownReport { get { return _markDownFactory; } }
39+
public BatchProcessorFactory HtmlMetroReport { get; } = new(()
40+
=> new HtmlReporter(new DefaultHtmlReportConfiguration(), new MetroReportBuilder()), false);
4441

45-
private readonly BatchProcessorFactory _diagnosticsFactory = new(() => new DiagnosticsReporter(), false);
46-
public BatchProcessorFactory DiagnosticsReport { get { return _diagnosticsFactory; } }
42+
public BatchProcessorFactory MarkDownReport { get; } = new(() => new MarkDownReporter(), false);
4743

48-
readonly List<IBatchProcessor> _addedProcessors = new();
44+
public BatchProcessorFactory DiagnosticsReport { get; } = new(() => new DiagnosticsReporter(), false);
4945

5046
public BatchProcessors Add(IBatchProcessor processor)
5147
{
@@ -55,7 +51,7 @@ public BatchProcessors Add(IBatchProcessor processor)
5551

5652
public IEnumerable<IBatchProcessor> GetProcessors()
5753
{
58-
return _GetProcessors().ToList();
54+
return [.. YieldProcessors()];
5955
}
6056
}
6157
}

src/TestStack.BDDfy/Configuration/ComponentFactory.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace TestStack.BDDfy.Configuration
44
{
5-
public class ComponentFactory<TComponent, TMaterial> where TComponent : class
5+
public abstract class ComponentFactory<TComponent, TMaterial> where TComponent : class
66
{
7-
private bool _active = true;
7+
private const bool DefaultState = true;
8+
private bool _active = DefaultState;
89
private Predicate<TMaterial> _runsOn = o => true;
910
readonly Func<TComponent> _factory;
10-
internal ComponentFactory(Func<TComponent> factory)
11-
{
12-
_factory = factory;
13-
}
11+
internal ComponentFactory(Func<TComponent> factory):this(factory, DefaultState) { }
1412

1513
internal ComponentFactory(Func<TComponent> factory, bool active)
1614
{
@@ -30,10 +28,10 @@ public void Enable()
3028

3129
public TComponent ConstructFor(TMaterial material)
3230
{
33-
if (!_active || !_runsOn(material))
34-
return null;
31+
if (_active && _runsOn(material))
32+
return _factory();
3533

36-
return _factory();
34+
return null;
3735
}
3836

3937
public void RunsOn(Predicate<TMaterial> runOn)

0 commit comments

Comments
 (0)