Skip to content

Commit

Permalink
Resolve #140 - Move all C# expressions into the model class and repla…
Browse files Browse the repository at this point in the history
…ce with path expressions
  • Loading branch information
craigfowler committed Apr 29, 2018
1 parent e255717 commit 5e14ff9
Show file tree
Hide file tree
Showing 8 changed files with 703 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<Compile Include="StreamSourceInfo.cs" />
<Compile Include="Views\ResourceBundler.cs" />
<Compile Include="HardCodedZptSharpConfigurationProvider.cs" />
<Compile Include="Models\ReportModel.cs" />
<Compile Include="Models\ScenarioModel.cs" />
<Compile Include="Models\FeatureModel.cs" />
<Compile Include="Models\ReportableModel.cs" />
<Compile Include="Models\ReportConstants.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -100,6 +105,10 @@
<Project>{18010B4E-22A7-4462-B057-7199B3386D48}</Project>
<Name>CSF.Screenplay.Reporting</Name>
</ProjectReference>
<ProjectReference Include="..\CSF.Screenplay\CSF.Screenplay.csproj">
<Project>{46E6DEAA-E6D5-4EE6-A552-17376BEA80DC}</Project>
<Name>CSF.Screenplay</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
Expand Down
102 changes: 102 additions & 0 deletions CSF.Screenplay.Reporting.Html/Models/FeatureModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// FeatureModel.cs
//
// Author:
// Craig Fowler <[email protected]>
//
// Copyright (c) 2018 Craig Fowler
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;

namespace CSF.Screenplay.Reporting.Models
{
/// <summary>
/// Model for HTML report features (wraps a <see cref="Feature"/> instance).
/// </summary>
public class FeatureModel
{
readonly Feature feature;
readonly IObjectFormattingService formattingService;

/// <summary>
/// Gets the identity of this feature.
/// </summary>
public string Id => feature.Id;

/// <summary>
/// Gets the human-readable friendly-name of this feature.
/// </summary>
public string FriendlyName => feature.FriendlyName;

/// <summary>
/// Gets a collection of the scenarios in the current feature.
/// </summary>
public IReadOnlyCollection<ScenarioModel> Scenarios
=> feature.Scenarios.Select(x => new ScenarioModel(x, formattingService)).ToArray();

/// <summary>
/// Gets or sets a value indicating whether this <see cref="Feature"/> contains any scenarios which are themselves failures.
/// </summary>
/// <value><c>true</c> if this feature contains any failures; otherwise, <c>false</c>.</value>
public bool HasFailures => feature.Scenarios.Any(x => x.IsFailure);

/// <summary>
/// Gets or sets a value indicating whether every <see cref="Scenario"/> within this <see cref="Feature"/> is a success.
/// </summary>
/// <value><c>true</c> if the feature contains only successful scenarios; otherwise, <c>false</c>.</value>
public bool IsSuccess => feature.Scenarios.All(x => x.IsSuccess);

/// <summary>
/// Gets the HTML class attribute value indicating the outcome of a given feature.
/// </summary>
/// <returns>The feature outcome class.</returns>
public string GetOutcomeClass()
{
if(feature == null) return String.Empty;
var outcome = GetOutcomeClass(feature.IsSuccess, feature.HasFailures);
return $"feature {outcome}";
}

string GetOutcomeClass(bool success, bool failure)
{
if(success) return ReportConstants.SuccessClass;
if(failure) return ReportConstants.FailureClass;
return ReportConstants.InconclusiveClass;
}

/// <summary>
/// Initializes a new instance of the <see cref="T:CSF.Screenplay.Reporting.Models.FeatureModel"/> class.
/// </summary>
/// <param name="feature">Feature.</param>
/// <param name="formattingService">Formatting service.</param>
public FeatureModel(Feature feature, IObjectFormattingService formattingService)
{
if(formattingService == null)
throw new ArgumentNullException(nameof(formattingService));
if(feature == null)
throw new ArgumentNullException(nameof(feature));

this.feature = feature;
this.formattingService = formattingService;
}
}
}
45 changes: 45 additions & 0 deletions CSF.Screenplay.Reporting.Html/Models/ReportConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ReportConstants.cs
//
// Author:
// Craig Fowler <[email protected]>
//
// Copyright (c) 2018 Craig Fowler
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace CSF.Screenplay.Reporting.Models
{
/// <summary>
/// A number of constants used by HTML reports.
/// </summary>
public static class ReportConstants
{
internal const string
SuccessClass = "success",
FailureClass = "failure",
InconclusiveClass = "inconclusive",
AbilityClass = "ability",
PerformanceClass = "performance",
AbilityMacro = "ability",
PerformanceWithResultMacro = "performance_success_result",
PerformanceWithExceptionMacro = "performance_failure_exception",
PerformanceMacro = "performance";
}
}
133 changes: 3 additions & 130 deletions CSF.Screenplay.Reporting.Html/Models/ReportDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,15 @@ namespace CSF.Screenplay.Reporting.Models
/// </summary>
public class ReportDocument
{
const string
SuccessClass = "success",
FailureClass = "failure",
InconclusiveClass = "inconclusive",
AbilityClass = "ability",
PerformanceClass = "performance",
AbilityMacro = "ability",
PerformanceWithResultMacro = "performance_success_result",
PerformanceWithExceptionMacro = "performance_failure_exception",
PerformanceMacro = "performance";

readonly IObjectFormattingService formattingService;
readonly IZptDocument document;
readonly Report report;
readonly ReportModel report;

/// <summary>
/// Gets the report to be rendered.
/// </summary>
/// <value>The report.</value>
public Report Report => report;
public ReportModel Report => report;

/// <summary>
/// Gets a reference to the ZPT template being used to render the report.
Expand Down Expand Up @@ -59,122 +48,6 @@ public IMetalMacroContainer GetMacros()
return output;
}

/// <summary>
/// Formats a given object using a formatting service.
/// </summary>
/// <param name="obj">Object.</param>
public string Format(object obj) => formattingService.Format(obj);

/// <summary>
/// Gets a value which indicates whether the formatting service has an explicit formatter for the given object or not.
/// </summary>
/// <returns><c>true</c>, if the formatting service has an explicit format for the object, <c>false</c> otherwise.</returns>
/// <param name="obj">Object.</param>
public bool HasFormat(object obj) => formattingService.HasExplicitSupport(obj);

/// <summary>
/// Gets the HTML class attribute value indicating the outcome of a given scenario.
/// </summary>
/// <returns>The scenario outcome class.</returns>
/// <param name="scenario">A test sscenario.</param>
public string GetOutcomeClass(Scenario scenario)
{
if(scenario == null) return String.Empty;
var outcome = GetOutcomeClass(scenario.IsSuccess, scenario.IsFailure);
return $"scenario {outcome}";
}

/// <summary>
/// Gets the HTML class attribute value indicating the outcome of a given feature.
/// </summary>
/// <returns>The feature outcome class.</returns>
/// <param name="feature">A test feature.</param>
public string GetOutcomeClass(Feature feature)
{
if(feature == null) return String.Empty;
var outcome = GetOutcomeClass(feature.IsSuccess, feature.HasFailures);
return $"feature {outcome}";
}

/// <summary>
/// Gets the HTML class attribute value indicating the outcome of a given reportable step within a scenario.
/// </summary>
/// <returns>The reportable's outcome class.</returns>
/// <param name="reportable">A reportable step, part of a scenario.</param>
public string GetOutcomeClass(Reportable reportable)
{

switch(reportable.Outcome)
{
case PerformanceOutcome.Success:
case PerformanceOutcome.SuccessWithResult:
return GetOutcomeClass(true, false);

default:
return GetOutcomeClass(false, true);
}
}

string GetOutcomeClass(bool success, bool failure)
{
if(success) return SuccessClass;
if(failure) return FailureClass;
return InconclusiveClass;
}

/// <summary>
/// Gets an HTML class attribute value which describes a reportable step within a scenario.
/// </summary>
/// <returns>The reportable class.</returns>
/// <param name="reportable">A reportable step, part of a scenario.</param>
public string GetReportableClass(Reportable reportable)
{
if(reportable == null) return String.Empty;

if(reportable is GainAbility)
return AbilityClass;

if(reportable is Performance)
return PerformanceClass;

return String.Empty;
}

/// <summary>
/// Gets the name of the METAL macro to use for rendering the specified reportable.
/// </summary>
/// <returns>The macro name.</returns>
/// <param name="reportable">A reportable step, part of a scenario.</param>
public string GetMacroName(Reportable reportable)
{
if(reportable == null) return null;

if(reportable is GainAbility)
return GetMacroName((GainAbility) reportable);

if(reportable is Performance)
return GetMacroName((Performance) reportable);

return null;
}

string GetMacroName(GainAbility reportable) => AbilityMacro;

string GetMacroName(Performance reportable)
{
switch(reportable.Outcome)
{
case PerformanceOutcome.SuccessWithResult:
return PerformanceWithResultMacro;

case PerformanceOutcome.FailureWithException:
return PerformanceWithExceptionMacro;

default:
return PerformanceMacro;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="T:CSF.Screenplay.Reporting.Models.ReportDocument"/> class.
/// </summary>
Expand All @@ -192,7 +65,7 @@ public ReportDocument(Report report,
if(report == null)
throw new ArgumentNullException(nameof(report));

this.report = report;
this.report = new ReportModel(report, formattingService);
this.formattingService = formattingService;
this.document = document;
}
Expand Down
Loading

0 comments on commit 5e14ff9

Please sign in to comment.