Skip to content

Commit ba9cc53

Browse files
committed
refactored the html report to use the same report writer as other file reports.
1 parent 6356877 commit ba9cc53

File tree

6 files changed

+51
-39
lines changed

6 files changed

+51
-39
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void ShouldCreateReportIfProcessingSucceeds()
2424

2525
sut.Process(new List<Core.Story>());
2626

27-
_writer.Received().OutputReport("Report Data", Arg.Any<string>());
27+
_writer.Received().OutputReport("Report Data", Arg.Any<string>(), Arg.Any<string>());
2828
}
2929

3030
[Test]
@@ -35,7 +35,8 @@ public void ShouldPrintErrorInReportIfProcessingFails()
3535

3636
sut.Process(new List<Core.Story>());
3737

38-
_writer.Received().OutputReport("There was an error compiling the json report: Error occurred.", Arg.Any<string>());
38+
_writer.Received().OutputReport("There was an error compiling the json report: Error occurred.",
39+
Arg.Any<string>(), Arg.Any<string>());
3940
}
4041

4142
private DiagnosticsReporter CreateSut()

TestStack.BDDfy.Tests/Processors/Reports/MarkDown/MarkDownReporterSpecs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void ShouldCreateReportIfProcessingSucceeds()
2323

2424
sut.Process(new List<Core.Story>());
2525

26-
_writer.Received().OutputReport("Report Data", Arg.Any<string>());
26+
_writer.Received().OutputReport("Report Data", Arg.Any<string>(), Arg.Any<string>());
2727
}
2828

2929
[Test]
@@ -34,7 +34,8 @@ public void ShouldPrintErrorInReportIfProcessingFails()
3434

3535
sut.Process(new List<Core.Story>());
3636

37-
_writer.Received().OutputReport("There was an error compiling the markdown report: Error occurred.", Arg.Any<string>());
37+
_writer.Received().OutputReport("There was an error compiling the markdown report: Error occurred.",
38+
Arg.Any<string>(), Arg.Any<string>());
3839
}
3940

4041
private MarkDownReporter CreateSut()

TestStack.BDDfy/Processors/HtmlReporter/HtmlReportBuilder.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22
using System.Linq;
33
using System.Text;
44
using TestStack.BDDfy.Core;
5+
using TestStack.BDDfy.Processors.Reports;
56

67
namespace TestStack.BDDfy.Processors.HtmlReporter
78
{
8-
public class HtmlReportBuilder
9+
public class HtmlReportBuilder : IReportBuilder
910
{
10-
readonly HtmlReportViewModel _viewModel;
11+
private HtmlReportViewModel _viewModel;
1112
readonly StringBuilder _html;
1213
const int TabIndentation = 2;
1314
int _tabCount;
1415

15-
public HtmlReportBuilder(HtmlReportViewModel viewModel)
16+
public HtmlReportBuilder()
1617
{
17-
_viewModel = viewModel;
1818
_html = new StringBuilder();
1919
}
2020

21-
public string BuildReportHtml()
21+
string IReportBuilder.CreateReport(FileReportModel model)
2222
{
23+
return CreateReport(model as HtmlReportViewModel);
24+
}
25+
26+
public string CreateReport(HtmlReportViewModel model)
27+
{
28+
_viewModel = model;
2329
AddLine("<!DOCTYPE html>");
2430
using(OpenTag(HtmlTag.html))
2531
{

TestStack.BDDfy/Processors/HtmlReporter/HtmlReporter.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,52 @@
33
using System.IO;
44
using System.Linq;
55
using TestStack.BDDfy.Core;
6+
using TestStack.BDDfy.Processors.Reports;
7+
using TestStack.BDDfy.Processors.Reports.Writers;
68

79
namespace TestStack.BDDfy.Processors.HtmlReporter
810
{
911
public class HtmlReporter : IBatchProcessor
1012
{
13+
private readonly IReportBuilder _builder;
14+
private readonly IReportWriter _writer;
15+
readonly IHtmlReportConfiguration _configuration;
16+
17+
public HtmlReporter(IHtmlReportConfiguration configuration) : this(configuration, new HtmlReportBuilder(), new FileWriter()) { }
18+
19+
public HtmlReporter(IHtmlReportConfiguration configuration, IReportBuilder builder, IReportWriter writer)
20+
{
21+
_configuration = configuration;
22+
_builder = builder;
23+
_writer = writer;
24+
}
25+
26+
public void Process(IEnumerable<Story> stories)
27+
{
28+
WriteOutScriptFiles();
29+
30+
var allowedStories = stories.Where(s => _configuration.RunsOn(s)).ToList();
31+
WriteOutHtmlReport(allowedStories);
32+
}
33+
34+
1135
void WriteOutHtmlReport(IEnumerable<Story> stories)
1236
{
1337
const string error = "There was an error compiling the html report: ";
14-
var htmlFullFileName = Path.Combine(_configuration.OutputPath, _configuration.OutputFileName);
1538
var viewModel = new HtmlReportViewModel(_configuration, stories);
1639
ShouldTheReportUseCustomization(viewModel);
1740
string report;
1841

1942
try
2043
{
21-
report = new HtmlReportBuilder(viewModel).BuildReportHtml();
44+
report = _builder.CreateReport(viewModel);
2245
}
2346
catch (Exception ex)
2447
{
2548
report = error + ex.Message;
2649
}
2750

28-
File.WriteAllText(htmlFullFileName, report);
51+
_writer.OutputReport(report, _configuration.OutputFileName, _configuration.OutputPath);
2952
}
3053

3154
private void ShouldTheReportUseCustomization(HtmlReportViewModel viewModel)
@@ -39,29 +62,9 @@ private void ShouldTheReportUseCustomization(HtmlReportViewModel viewModel)
3962

4063
void WriteOutScriptFiles()
4164
{
42-
var cssFullFileName = Path.Combine(_configuration.OutputPath, "BDDfy.css");
43-
File.WriteAllText(cssFullFileName, HtmlReportResources.BDDfy_css);
44-
45-
var jqueryFullFileName = Path.Combine(_configuration.OutputPath, "jquery-1.7.1.min.js");
46-
File.WriteAllText(jqueryFullFileName, HtmlReportResources.jquery_1_7_1_min);
47-
48-
var bddifyJavascriptFileName = Path.Combine(_configuration.OutputPath, "BDDfy.js");
49-
File.WriteAllText(bddifyJavascriptFileName, HtmlReportResources.BDDfy_js);
50-
}
51-
52-
readonly IHtmlReportConfiguration _configuration;
53-
54-
public HtmlReporter(IHtmlReportConfiguration configuration)
55-
{
56-
_configuration = configuration;
57-
}
58-
59-
public void Process(IEnumerable<Story> stories)
60-
{
61-
WriteOutScriptFiles();
62-
63-
var allowedStories = stories.Where(s => _configuration.RunsOn(s)).ToList();
64-
WriteOutHtmlReport(allowedStories);
65+
_writer.OutputReport(HtmlReportResources.BDDfy_css, "BDDfy.css", _configuration.OutputPath);
66+
_writer.OutputReport(HtmlReportResources.jquery_1_7_1_min, "jquery-1.7.1.min.js", _configuration.OutputPath);
67+
_writer.OutputReport(HtmlReportResources.BDDfy_js, "BDDfy.js", _configuration.OutputPath);
6568
}
6669
}
6770
}

TestStack.BDDfy/Processors/Reports/Writers/FileWriter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ namespace TestStack.BDDfy.Processors.Reports.Writers
66
{
77
public class FileWriter : IReportWriter
88
{
9-
public void OutputReport(string reportData, string reportName)
9+
public void OutputReport(string reportData, string reportName, string outputDirectory = null)
1010
{
11-
var path = Path.Combine(OutputDirectory, reportName);
11+
string directory = outputDirectory ?? GetDefaultOutputDirectory;
12+
var path = Path.Combine(directory, reportName);
1213

1314
if (File.Exists(path))
1415
File.Delete(path);
1516
File.WriteAllText(path, reportData);
1617
}
1718

18-
private static string OutputDirectory
19+
private static string GetDefaultOutputDirectory
1920
{
2021
get
2122
{

TestStack.BDDfy/Processors/Reports/Writers/IReportWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ namespace TestStack.BDDfy.Processors.Reports.Writers
22
{
33
public interface IReportWriter
44
{
5-
void OutputReport(string reportData, string reportName);
5+
void OutputReport(string reportData, string reportName, string outputDirectory = null);
66
}
77
}

0 commit comments

Comments
 (0)