1 /++
2   A module containing the DotMatrixReporter
3 
4   This is an example of how this reporter looks
5   <script type="text/javascript" src="https://asciinema.org/a/aorvsrruse34n2xym8y7885m1.js" id="asciicast-aorvsrruse34n2xym8y7885m1" async></script>
6 
7   Copyright: © 2017 Szabo Bogdan
8   License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
9   Authors: Szabo Bogdan
10 +/
11 module trial.reporters.dotmatrix;
12 
13 import std.stdio;
14 import std.array;
15 import std.conv;
16 import std.datetime;
17 import std..string;
18 import std.algorithm;
19 
20 import trial.interfaces;
21 import trial.reporters.writer;
22 
23 ///
24 struct DotMatrixGlyphs {
25   string success = ".";
26   string failure = "!";
27   string unknown = "?";
28   string pending = "-";
29 }
30 
31 ///
32 string dotMatrixGlyphsToCode(DotMatrixGlyphs glyphs) {
33   return "DotMatrixGlyphs(`"~ glyphs.success ~"`,`"~ glyphs.failure ~"`,`"~ glyphs.unknown ~"`)";
34 }
35 
36 /// The dot matrix reporter is simply a series of characters which represent test cases.
37 /// Failures highlight in red exclamation marks (!).
38 /// Good if you prefer minimal output.
39 class DotMatrixReporter : ITestCaseLifecycleListener
40 {
41   private {
42     ReportWriter writer;
43     DotMatrixGlyphs glyphs;
44   }
45 
46   this(DotMatrixGlyphs glyphs)
47   {
48     writer = defaultWriter;
49     this.glyphs = glyphs;
50   }
51 
52   this(ReportWriter writer)
53   {
54     this.writer = writer;
55   }
56 
57   void begin(string suite, ref TestResult test)
58   {
59   }
60 
61   void end(string suite, ref TestResult test)
62   {
63     switch (test.status)
64     {
65     case TestResult.Status.success:
66       writer.write(glyphs.success, ReportWriter.Context.inactive);
67       break;
68 
69     case TestResult.Status.failure:
70       writer.write(glyphs.failure, ReportWriter.Context.danger);
71       break;
72 
73     case TestResult.Status.pending:
74       writer.write(glyphs.pending, ReportWriter.Context.info);
75       break;
76 
77     default:
78       writer.write(glyphs.unknown, ReportWriter.Context.warning);
79     }
80   }
81 }
82 
83 version (unittest)
84 {
85   version(Have_fluent_asserts) {
86     import fluent.asserts;
87   }
88 }
89 
90 @("it should print a success test")
91 unittest
92 {
93   auto writer = new BufferedWriter;
94   auto reporter = new DotMatrixReporter(writer);
95 
96   auto suite = SuiteResult("some suite");
97   auto test = new TestResult("some test");
98   test.status = TestResult.Status.success;
99 
100   reporter.begin("some suite", test);
101   writer.buffer.should.equal("");
102 
103   reporter.end("some suite", test);
104   writer.buffer.should.equal(".");
105 }
106 
107 @("it should print a failing test")
108 unittest
109 {
110   auto writer = new BufferedWriter;
111   auto reporter = new DotMatrixReporter(writer);
112 
113   auto suite = SuiteResult("some suite");
114   auto test = new TestResult("some test");
115   test.status = TestResult.Status.failure;
116 
117   reporter.begin("some suite", test);
118   writer.buffer.should.equal("");
119 
120   reporter.end("some suite", test);
121   writer.buffer.should.equal("!");
122 }
123 
124 @("it should print a pending test")
125 unittest
126 {
127   auto writer = new BufferedWriter;
128   auto reporter = new DotMatrixReporter(writer);
129 
130   auto suite = SuiteResult("some suite");
131   auto test = new TestResult("some test");
132   test.status = TestResult.Status.pending;
133 
134   reporter.begin("some suite", test);
135   writer.buffer.should.equal("");
136 
137   reporter.end("some suite", test);
138   writer.buffer.should.equal("-");
139 }