Skip to content

Commit 28db924

Browse files
committed
Added tests for diagnostics
1 parent 67ddc39 commit 28db924

File tree

11 files changed

+231
-37
lines changed

11 files changed

+231
-37
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using TestStack.BDDfy.Core;
4+
5+
namespace TestStack.BDDfy.Tests.Processors.Diagnostics
6+
{
7+
public class DiagnosticTestData
8+
{
9+
public IEnumerable<Core.Story> CreateTwoStoriesEachWithTwoScenariosWithTwoStepsOfFiveMilliseconds()
10+
{
11+
var storyMetaData1 = new StoryMetaData(typeof(DummyStory1), "As a person", "I want ice cream", "So that I can be happy", "Happiness");
12+
var storyMetaData2 = new StoryMetaData(typeof(DummyStory2), "As an account holder", "I want to withdraw cash", "So that I can get money when the bank is closed", "Account holder withdraws cash");
13+
var stories = new List<Core.Story>()
14+
{
15+
new Core.Story(storyMetaData1, GetScenarios()),
16+
new Core.Story(storyMetaData2, GetScenarios())
17+
};
18+
19+
return stories;
20+
}
21+
22+
private Scenario[] GetScenarios()
23+
{
24+
var scenarios = new List<Scenario>()
25+
{
26+
new Scenario(typeof(DummyScenario1), GetExecutionSteps(), "scenario1"),
27+
new Scenario(typeof(DummyScenario2), GetExecutionSteps(), "scenario2")
28+
};
29+
return scenarios.ToArray();
30+
}
31+
32+
private IEnumerable<ExecutionStep> GetExecutionSteps()
33+
{
34+
var steps = new List<ExecutionStep>()
35+
{
36+
new ExecutionStep(null, null, true, ExecutionOrder.Assertion, true) {Duration = new TimeSpan(0, 0, 0, 0, 5)},
37+
new ExecutionStep(null, null, true, ExecutionOrder.Assertion, true) {Duration = new TimeSpan(0, 0, 0, 0, 5)},
38+
};
39+
return steps;
40+
}
41+
42+
public class DummyStory1 { }
43+
public class DummyStory2 { }
44+
public class DummyScenario1 { }
45+
public class DummyScenario2 { }
46+
}
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using TestStack.BDDfy.Processors;
6+
using TestStack.BDDfy.Processors.Diagnostics;
7+
8+
namespace TestStack.BDDfy.Tests.Processors.Diagnostics
9+
{
10+
[TestFixture]
11+
public class DiagnosticsCalculatorSpecs
12+
{
13+
private DiagnosticsCalculator _sut;
14+
private IEnumerable<Core.Story> _stories;
15+
private IList<StoryDiagnostic> _result;
16+
17+
public void GivenADiagnosticsCalculator()
18+
{
19+
_sut = new DiagnosticsCalculator();
20+
}
21+
22+
public void AndGivenTwoStoriesEachWithTwoScenariosWithTwoStepsOfFiveMilliseconds()
23+
{
24+
_stories = new DiagnosticTestData().CreateTwoStoriesEachWithTwoScenariosWithTwoStepsOfFiveMilliseconds();
25+
}
26+
27+
public void WhenTheDiagnosticDataIsCalculated()
28+
{
29+
_result = _sut.GetDiagnosticData(new FileReportModel(_stories));
30+
}
31+
32+
public void ThenTwoStoriesShouldBeReturned()
33+
{
34+
Assert.AreEqual(2, _result.Count);
35+
}
36+
37+
public void AndThenEachStoryShouldHaveDurationOf20Milliseconds()
38+
{
39+
_result.ToList().ForEach(story => Assert.AreEqual(20, story.Duration));
40+
}
41+
42+
public void AndThenEachScenarioShouldHaveDurationOf10Milliseconds()
43+
{
44+
_result[0].Scenarios.ForEach(scenario => Assert.AreEqual(10, scenario.Duration));
45+
_result[1].Scenarios.ForEach(scenario => Assert.AreEqual(10, scenario.Duration));
46+
}
47+
48+
public void AndThenEachStepShouldHaveDurationOf5Milliseconds()
49+
{
50+
_result[0].Scenarios.ForEach(scenario => scenario.Steps.ForEach(step => Assert.AreEqual(5, step.Duration)));
51+
_result[1].Scenarios.ForEach(scenario => scenario.Steps.ForEach(step => Assert.AreEqual(5, step.Duration)));
52+
}
53+
54+
[Test]
55+
public void RunSpecs()
56+
{
57+
this.BDDfy();
58+
}
59+
}
60+
}

TestStack.BDDfy.Tests/Processors/Diagnostics/DiagnosticsReporterSpecs.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using System.Collections.Generic;
33
using NSubstitute;
44
using NUnit.Framework;
5-
using TestStack.BDDfy.Core;
5+
using TestStack.BDDfy.Processors;
66
using TestStack.BDDfy.Processors.Diagnostics;
77

88
namespace TestStack.BDDfy.Tests.Processors.Diagnostics
99
{
1010
[TestFixture]
1111
public class DiagnosticsReporterSpecs
1212
{
13+
private IDiagnosticsCalculator _calculator;
1314
private ISerializer _serializer;
1415
private IReportWriter _writer;
1516

@@ -35,11 +36,28 @@ public void ShouldPrintErrorInReportIfProcessingFails()
3536
_writer.Received().Create("There was an error compiling the json report: Error occurred.", Arg.Any<string>());
3637
}
3738

39+
[Test]
40+
public void ShouldGetDiagnosticDataFromStories()
41+
{
42+
var sut = CreateSut();
43+
sut.Process(new List<Core.Story>());
44+
_calculator.Received().GetDiagnosticData(Arg.Any<FileReportModel>());
45+
}
46+
47+
[Test]
48+
public void ShouldSerializeDiagnosticDataToSpecifiedFormat()
49+
{
50+
var sut = CreateSut();
51+
sut.Process(new List<Core.Story>());
52+
_serializer.Received().Serialize(Arg.Any<IList<StoryDiagnostic>>());
53+
}
54+
3855
private DiagnosticsReporter CreateSut()
3956
{
57+
_calculator = Substitute.For<IDiagnosticsCalculator>();
4058
_serializer = Substitute.For<ISerializer>();
4159
_writer = Substitute.For<IReportWriter>();
42-
return new DiagnosticsReporter(_serializer, _writer);
60+
return new DiagnosticsReporter(_calculator, _serializer, _writer);
4361
}
4462
}
4563
}

TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
<SubType>Code</SubType>
7878
</Compile>
7979
<Compile Include="HumanizerTests.cs" />
80+
<Compile Include="Processors\Diagnostics\DiagnosticsCalculatorSpecs.cs" />
8081
<Compile Include="Processors\Diagnostics\DiagnosticsReporterSpecs.cs" />
82+
<Compile Include="Processors\Diagnostics\DiagnosticTestData.cs" />
8183
<Compile Include="Scanner\ExecutableAttributeOrderOrdersTheStepsCorrectly.cs" />
8284
<Compile Include="Scanner\PropertiesAreNotConsideredAsStepsEvenWhenTheirNameMatchesConventions.cs" />
8385
<Compile Include="Scanner\ExpressionExtensionsTests.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace TestStack.BDDfy.Processors.Diagnostics
5+
{
6+
public class DiagnosticsCalculator : IDiagnosticsCalculator
7+
{
8+
public IList<StoryDiagnostic> GetDiagnosticData(FileReportModel viewModel)
9+
{
10+
var graph = new List<StoryDiagnostic>();
11+
foreach (var story in viewModel.Stories)
12+
{
13+
graph.Add(new StoryDiagnostic()
14+
{
15+
Name = story.MetaData.Title,
16+
Duration = story.Scenarios.Sum(x => x.Duration.Milliseconds),
17+
Scenarios = story.Scenarios.Select(scenario => new StoryDiagnostic.Scenario()
18+
{
19+
Name = scenario.Title,
20+
Duration = scenario.Duration.Milliseconds,
21+
Steps = scenario.Steps.Select(step => new StoryDiagnostic.Step()
22+
{
23+
Name = step.StepTitle,
24+
Duration = step.Duration.Milliseconds
25+
}).ToList()
26+
}).ToList()
27+
});
28+
}
29+
30+
return graph;
31+
}
32+
}
33+
}

TestStack.BDDfy/Processors/Diagnostics/DiagnosticsReporter.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,34 @@
2525

2626
using System;
2727
using System.Collections.Generic;
28-
using System.Linq;
2928
using TestStack.BDDfy.Core;
3029

3130
namespace TestStack.BDDfy.Processors.Diagnostics
3231
{
3332
public class DiagnosticsReporter : IBatchProcessor
3433
{
34+
private readonly IDiagnosticsCalculator _calculator;
3535
private readonly ISerializer _serializer;
3636
private readonly IReportWriter _writer;
3737

38-
public DiagnosticsReporter() : this(new JsonSerializer(), new FileWriter()) { }
38+
public DiagnosticsReporter() : this(new DiagnosticsCalculator(), new JsonSerializer(), new FileWriter()) { }
3939

40-
public DiagnosticsReporter(ISerializer serializer, IReportWriter writer)
40+
public DiagnosticsReporter(IDiagnosticsCalculator calculator, ISerializer serializer, IReportWriter writer)
4141
{
42+
_calculator = calculator;
4243
_serializer = serializer;
4344
_writer = writer;
4445
}
4546

46-
public void Process(IEnumerable<Story> stories)
47+
public void Process(IEnumerable<Core.Story> stories)
4748
{
4849
const string error = "There was an error compiling the json report: ";
4950
var viewModel = new FileReportModel(stories);
5051
string report;
5152

5253
try
5354
{
54-
var data = GetDiagnosticData(viewModel);
55+
var data = _calculator.GetDiagnosticData(viewModel);
5556
report = _serializer.Serialize(data);
5657
}
5758
catch (Exception ex)
@@ -61,30 +62,5 @@ public void Process(IEnumerable<Story> stories)
6162

6263
_writer.Create(report, "Diagnostics.json");
6364
}
64-
65-
public List<object> GetDiagnosticData(FileReportModel viewModel)
66-
{
67-
var graph = new List<object>();
68-
foreach (var story in viewModel.Stories)
69-
{
70-
graph.Add(new
71-
{
72-
StoryName = story.MetaData.Title,
73-
StoryDuration = story.Scenarios.Sum(x => x.Duration.Milliseconds),
74-
Scenarios = story.Scenarios.Select(scenario => new
75-
{
76-
ScenarioName = scenario.Title,
77-
ScenarioDuration = scenario.Duration.Milliseconds,
78-
Steps = scenario.Steps.Select(step => new
79-
{
80-
StepName = step.StepTitle,
81-
StepDuration = step.Duration.Milliseconds
82-
})
83-
})
84-
});
85-
}
86-
87-
return graph;
88-
}
8965
}
9066
}

TestStack.BDDfy/Processors/Diagnostics/FileWriter.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
using System;
1+
// Copyright (C) 2011, Mehdi Khalili
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
// * Redistributions of source code must retain the above copyright
7+
// notice, this list of conditions and the following disclaimer.
8+
// * Redistributions in binary form must reproduce the above copyright
9+
// notice, this list of conditions and the following disclaimer in the
10+
// documentation and/or other materials provided with the distribution.
11+
// * Neither the name of the <organization> nor the
12+
// names of its contributors may be used to endorse or promote products
13+
// derived from this software without specific prior written permission.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19+
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
using System;
227
using System.IO;
328

429
namespace TestStack.BDDfy.Processors.Diagnostics
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace TestStack.BDDfy.Processors.Diagnostics
4+
{
5+
public interface IDiagnosticsCalculator
6+
{
7+
IList<StoryDiagnostic> GetDiagnosticData(FileReportModel model);
8+
}
9+
}

TestStack.BDDfy/Processors/Diagnostics/JsonFormatter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Text;
52

63
namespace TestStack.BDDfy.Processors.Diagnostics
74
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace TestStack.BDDfy.Processors.Diagnostics
4+
{
5+
public class StoryDiagnostic
6+
{
7+
public string Name { get; set; }
8+
public int Duration { get; set; }
9+
public List<Scenario> Scenarios { get; set; }
10+
11+
public class Scenario
12+
{
13+
public string Name { get; set; }
14+
public int Duration { get; set; }
15+
public List<Step> Steps { get; set; }
16+
}
17+
18+
public class Step
19+
{
20+
public string Name { get; set; }
21+
public int Duration { get; set; }
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)