Skip to content

Commit

Permalink
Merge pull request #2 from gman-au/line-chart
Browse files Browse the repository at this point in the history
Line chart
  • Loading branch information
gubpalma authored Apr 3, 2024
2 parents 34bbe2b + e2d473d commit 32e90b3
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,19 @@ public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type)

while (currInstr.OpCode == OpCodes.Ldstr)
{
tags.Add(currInstr.Operand.ToString());
currInstr =
if (currInstr.Operand != null)
{
tags.Add(
currInstr
.Operand.ToString()?
.Replace(
",",
""
)
);
}

currInstr =
currInstr
.Previous
.Previous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace SpecFlowToMarkdown.Infrastructure.Markdown
{
public class ColourSorter : IColourSorter
{
private const string PassColour = "#16c60c88";
private const string FailColour = "#f03a1788";
private const string OtherColour = "#fff8";
public const string PassColour = "#16c60c88";
public const string FailColour = "#f03a1788";
public const string OtherColour = "#fff8";

public ICollection<ChartLegendItem> Sort(int passCount, int failCount, int otherCount)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,80 @@ namespace SpecFlowToMarkdown.Infrastructure.Markdown.Extensions
{
public static class StringBuilderEx
{
public static StringBuilder AppendChart(
public static StringBuilder AppendTagChart(
this StringBuilder stringBuilder,
string title,
IDictionary<string, TestSummary> results
)
{
stringBuilder
.AppendLine()
.AppendLine("```mermaid")
.AppendLine("%%{")
.AppendLine("\tinit: {")
.AppendLine("\t\t'theme': 'base',")
.AppendLine("\t\t'themeVariables': {")
.AppendLine("\t\t\t'xyChart': {")
.AppendLine("\t\t\t\t'titleColor': \"#fff\",")
.AppendLine("\t\t\t\t'xAxisLabelColor': \"#fff\",")
.AppendLine("\t\t\t\t'xAxisTitleColor': \"#fff\",")
.AppendLine("\t\t\t\t'xAxisTickColor': \"#fff\",")
.AppendLine("\t\t\t\t'xAxisLineColor': \"#fff\",")
.AppendLine("\t\t\t\t'yAxisLabelColor': \"#fff\",")
.AppendLine("\t\t\t\t'yAxisTitleColor': \"#fff\",")
.AppendLine("\t\t\t\t'yAxisTickColor': \"#fff\",")
.AppendLine("\t\t\t\t'yAxisLineColor': \"#fff\",")
.AppendLine($"\t\t\t\t'backgroundColor': \"#0000\",")
.AppendLine($"\t\t\t\t'plotColorPalette': \"{ColourSorter.OtherColour}, {ColourSorter.PassColour}, {ColourSorter.FailColour}\"");

stringBuilder
.AppendLine("\t\t\t}")
.AppendLine("\t\t}")
.AppendLine("\t}")
.AppendLine("}%%");

stringBuilder
.AppendLine("xychart-beta")
.AppendLine($"title {title}");

var xAxis = string.Join(
", ",
results.Keys
);

stringBuilder
.AppendLine($"x-axis [{xAxis}]")
.AppendLine($"y-axis \"Tests\"");

var values = results.Values;

var totalValues = string.Join(
", ",
values.Select(o => o.Successes + o.Failures + o.Others)
);

var successValues = string.Join(
", ",
values.Select(o => o.Successes)
);

var failureValues = string.Join(
", ",
values.Select(o => o.Failures)
);

stringBuilder
.AppendLine($"bar [{totalValues}]")
.AppendLine($"bar [{successValues}]")
.AppendLine($"bar [{failureValues}]");

stringBuilder
.AppendLine("```");

return stringBuilder;
}

public static StringBuilder AppendPieChart(
this StringBuilder stringBuilder,
string title,
ICollection<ChartLegendItem> legend
Expand All @@ -26,10 +99,10 @@ ICollection<ChartLegendItem> legend
.AppendLine("\t\t\t'pieStrokeColor': '#8888',")
.AppendLine("\t\t\t'pieOuterStrokeColor': '#8888',");

for(var i=0; i < legend.Count; i++)
for (var i = 0; i < legend.Count; i++)
{
var legendItem = legend.ElementAt(i);

stringBuilder
.Append($"\t\t\t'pie{i + 1}': '{legendItem.Colour}'");

Expand All @@ -51,7 +124,7 @@ ICollection<ChartLegendItem> legend
stringBuilder
.AppendLine($"\t\"{legendItem.Title}\": {legendItem.Value}");
}

stringBuilder
.AppendLine("```")
.AppendLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using SpecFlowToMarkdown.Domain;
using System.Collections.Generic;
using SpecFlowToMarkdown.Domain;
using SpecFlowToMarkdown.Domain.Result;
using SpecFlowToMarkdown.Domain.TestAssembly;
using SpecFlowToMarkdown.Infrastructure.Markdown.Definition;

namespace SpecFlowToMarkdown.Infrastructure.Markdown
Expand All @@ -11,6 +13,8 @@ public interface IResultSummariser
public TestSummary SummariseAllScenarios(TestExecution execution);

public TestSummary SummariseAllSteps(TestExecution execution);

public IDictionary<string, TestSummary> SummariseAllTags(TestExecution execution, SpecFlowAssembly assembly);

public TestStatusEnum Assess(int successes, int failures, int others);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ TestExecution execution
_resultSummariser
.SummariseAllSteps(execution);

var tagSummary =
_resultSummariser
.SummariseAllTags(
execution,
assembly
);

// Render header
headerBuilder
.AppendLine($"# {assembly.AssemblyName}");
Expand All @@ -54,7 +61,7 @@ TestExecution execution
.AppendLine("<table>")
.AppendLine("<tr>")
.AppendLine("<td>")
.AppendChart(
.AppendPieChart(
"Features",
_colourSorter
.Sort(
Expand All @@ -65,7 +72,7 @@ TestExecution execution
)
.AppendLine("</td>")
.AppendLine("<td>")
.AppendChart(
.AppendPieChart(
"Scenarios",
_colourSorter
.Sort(
Expand All @@ -76,7 +83,7 @@ TestExecution execution
)
.AppendLine("</td>")
.AppendLine("<td>")
.AppendChart(
.AppendPieChart(
"Steps",
_colourSorter
.Sort(
Expand All @@ -86,6 +93,12 @@ TestExecution execution
)
)
.AppendLine("</td>")
.AppendLine("<td>")
.AppendTagChart(
"Tags",
tagSummary
)
.AppendLine("</td>")
.AppendLine("</tr>")
.AppendLine("</table>")
.AppendLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using SpecFlowToMarkdown.Domain;
using SpecFlowToMarkdown.Domain.Result;
using SpecFlowToMarkdown.Domain.TestAssembly;
using SpecFlowToMarkdown.Infrastructure.Markdown.Definition;

namespace SpecFlowToMarkdown.Infrastructure.Markdown
Expand Down Expand Up @@ -113,6 +115,77 @@ public TestSummary SummariseAllSteps(TestExecution execution)
};
}

public IDictionary<string, TestSummary> SummariseAllTags(TestExecution execution, SpecFlowAssembly assembly)
{
var results = new Dictionary<string, TestSummary>();

var allTags =
assembly
.Features
.SelectMany(o => o.Scenarios.SelectMany(x => x.Tags))
.Distinct();

foreach (var tag in allTags)
{
var result = new TestSummary
{
Successes = 0,
Failures = 0,
Others = 0,
Duration = 0
};

foreach (var feature in assembly.Features)
{
var allTaggedScenarios =
feature
.Scenarios
.Where(x => x.Tags.Contains(tag));

foreach (var taggedScenario in allTaggedScenarios)
{
var executionResult =
execution
.ExecutionResults
.FirstOrDefault(
o => o.FeatureTitle == feature.Title &&
o.ScenarioTitle == taggedScenario.Title
);

if (executionResult != null)
{
switch (Assess(executionResult.Status))
{
case TestStatusEnum.Success:
result.Successes++;
break;
case TestStatusEnum.Failure:
result.Failures++;
break;
default:
result.Others++;
break;
}
}
}
}

results
.Add(
tag,
result
);
}

return
results
.OrderBy(o => o.Key)
.ToDictionary(
o => o.Key,
o => o.Value
);
}

public TestStatusEnum Assess(int successes, int failures, int others)
{
if (failures > 0) return TestStatusEnum.Failure;
Expand Down

0 comments on commit 32e90b3

Please sign in to comment.