1 module cucumber.formatter.base;
2 
3 import std.array : join;
4 import std.string : format;
5 import std.stdio : write, writeln;
6 import std.typecons : tuple;
7 
8 import cucumber.result : Result, ResultSummary, RunResult, ScenarioResult, StepResult;
9 import gherkin : Feature, Scenario, Step, Examples, TableRow, Comment;
10 
11 ///
12 interface Formatter
13 {
14     ///
15     void feature(Feature);
16 
17     ///
18     void scenario(ref Scenario);
19 
20     ///
21     void examples(Examples);
22 
23     ///
24     void tableRow(TableRow, ref TableRow[], string);
25 
26     ///
27     void tableRow(TableRow, ref TableRow[], ScenarioResult);
28 
29     ///
30     void step(Step, StepResult);
31 
32     ///
33     void comment(Comment);
34 
35     ///
36     void summarizeResult(RunResult);
37 
38     ///
39     string color(string);
40 
41     ///
42     final string resultSummary(ResultSummary summary)
43     {
44         string[] result;
45         foreach (t; [
46                 tuple("failed", summary.failed), tuple("skipped", summary.skipped),
47                 tuple("undefined", summary.undefined),
48                 tuple("passed", summary.passed)
49             ])
50         {
51             if (t[1] > 0)
52             {
53                 result ~= "%s%d %s%s".format(color(t[0]), t[1], t[0], color("reset"));
54             }
55         }
56         return result.join(", ");
57     }
58 
59     ///
60     final void runResult(RunResult runResult, bool noSource)
61     {
62         string result;
63         foreach (k; ["scenario", "step"])
64         {
65             auto v = runResult.resultSummaries.get(k, ResultSummary());
66             result ~= "%s %s".format(v.total, k);
67             if (v.total != 1)
68             {
69                 result ~= "s";
70             }
71             if (v.total > 0)
72             {
73                 result ~= " (%s)".format(resultSummary(v));
74             }
75             result ~= "\n";
76         }
77         if (!noSource)
78         {
79             result ~= runResult.time.toString();
80         }
81 
82         write(result);
83     }
84 }