trial.reporters.writer 109/139(78%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1421
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1631
1640
1651
1661
1670
1681
1690
1700
1710
1720
1738549
1740
175356
1760
1770
1783919
1793919
1800
1813919
1820
1831378
1841378
1851378
1860
187221
188221
189221
1900
191234
192234
193234
1940
195116
196116
197116
1980
1992
2002
2012
2020
2038
2048
2058
2060
2071960
2081960
2090
2100
2110
2120
2130
2142
2150
2160
2170
2180
2191
2201
2210
2221
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2441
2451
2461
2470
2480
2490
2500
2510
2520
2530
2540
2552138
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
27218695
2730
2742137
2750
2762137
2772137
2782137
2792137
2800
2810
2820
2830
2840
2858
2860
2871
2880
2891
2901
2911
2920
2930
2940
2950
2960
297347
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
34050
34150
3420
3430
3440
3450
3460
34722
3480
3490
3500
3510
3520
3535311
354475
3550
356475
3570
3582246
3590
360648
3610
362238
3630
3640
365859
3660
367211
3680
3690
370648
3710
372648
3730
374560
3750
3760
3770
37888
3790
380648
3810
3820
383475
384475
385475
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
397180
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4211
4222
4230
4240
4250
4260
4270
4281
4291
4302
4310
4320
4330
4340
4350
4361
4371
4381
4392
4400
4410
4420
4430
4440
4451
4461
4471
4482
4490
4500
4510
4520
4530
4541
4551
4561
4571
4581
4592
4600
4610
4620
4630
4640
4651
4661
4671
4681
4691
4702
4710
4720
4730
4740
4750
4761
4771
4781
4791
4801
4811
4822
4830
4840
4850
4860
4870
4881
4891
4901
4911
4921
4931
4942
4950
/++ A module containing utilities for presenting information to the user Copyright: © 2017 Szabo Bogdan License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Szabo Bogdan +/ module trial.reporters.writer; import std.stdio; import std.algorithm; import std.string; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /// The default writer is initialized at the test run initialization with the right /// class, depending on the hosts capabilities. ReportWriter defaultWriter; /// The writer interface is used to present information to the user. interface ReportWriter { /// The information type. /// Convey meaning through color with a handful of emphasis utility classes. enum Context { /// Some important information active, /// Less important information inactive, /// success, /// Something that the user should notice info, /// Something that the user should be aware of warning, /// Something that the user must notice danger, /// _default } /// Go back a few lines void goTo(int); /// Write a string void write(string, Context = Context.active); /// Write a string with reversed colors void writeReverse(string, Context = Context.active); /// Write a string and go to a new line void writeln(string, Context = Context.active); /// Show the cursor from user void showCursor(); /// Hide the cursor from user void hideCursor(); /// Get how many characters you can print on a line uint width(); } /// The console writer outputs data to the standard output. It does not /// support colors and cursor moving. /// This is the default writer if arsd.terminal is not present. class ConsoleWriter : ReportWriter { /// not supported void goTo(int) { } /// void write(string text, Context) { std.stdio.write(text); } /// void writeReverse(string text, Context) { std.stdio.write(text); } /// void writeln(string text, Context) { std.stdio.writeln(text); } /// not supported void showCursor() { } /// not supported void hideCursor() { } /// returns 80 uint width() { return 80; } } import trial.terminal; shared static this() { version (Windows) { import core.sys.windows.windows; SetConsoleCP(65001); SetConsoleOutputCP(65001); auto consoleType = GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)); if(consoleType == 2) { writeln("using the color console."); defaultWriter = new ColorConsoleWriter; } else { writeln("using the standard console."); defaultWriter = new ConsoleWriter; } std.stdio.stdout.flush; } else { defaultWriter = new ColorConsoleWriter; } } /// This writer uses arsd.terminal and it's used if you add this dependency to your project /// It supports all the features and you should use it if you want to get the best experience /// from this project class ColorConsoleWriter : ReportWriter { private { int[string] cues; Terminal terminal; int lines = 0; bool movedToBottom = false; Context currentContext = Context._default; bool isReversed = false; } this() { this.terminal = Terminal(ConsoleOutputType.linear); this.terminal._suppressDestruction = true; lines = this.terminal.cursorY; } void setColor(Context context) { if (!isReversed && context == currentContext) { return; } isReversed = false; currentContext = context; switch (context) { case Context.active: terminal.color(Color.white | Bright, Color.DEFAULT); break; case Context.inactive: terminal.color(Color.black | Bright, Color.DEFAULT); break; case Context.success: terminal.color(Color.green | Bright, Color.DEFAULT); break; case Context.info: terminal.color(Color.cyan, Color.DEFAULT); break; case Context.warning: terminal.color(Color.yellow, Color.DEFAULT); break; case Context.danger: terminal.color(Color.red, Color.DEFAULT); break; default: terminal.reset(); } } void setColorReverse(Context context) { if (!isReversed && context == currentContext) { return; } currentContext = context; isReversed = true; switch (context) { case Context.active: terminal.color(Color.DEFAULT, Color.white | Bright); break; case Context.inactive: terminal.color(Color.DEFAULT, Color.black | Bright); break; case Context.success: terminal.color(Color.DEFAULT, Color.green | Bright); break; case Context.info: terminal.color(Color.DEFAULT, Color.cyan); break; case Context.warning: terminal.color(Color.DEFAULT, Color.yellow); break; case Context.danger: terminal.color(Color.DEFAULT, Color.red); break; default: terminal.reset(); } } void resetColor() { setColor(Context._default); } /// Go up `y` lines void goTo(int y) { if (!movedToBottom) { movedToBottom = true; terminal.moveTo(0, terminal.height - 1); } terminal.moveTo(0, terminal.cursorY - y, ForceOption.alwaysSend); } /// writes a string void write(string text, Context context) { lines += text.count!(a => a == '\n'); setColor(context); terminal.write(text); terminal.flush; resetColor; terminal.flush; } /// writes a string with reversed colors void writeReverse(string text, Context context) { lines += text.count!(a => a == '\n'); setColorReverse(context); terminal.write(text); resetColor; terminal.flush; } /// writes a string and go to a new line void writeln(string text, Context context) { this.write(text ~ "\n", context); } /// show the terminal cursor void showCursor() { terminal.showCursor; } /// hide the terminal cursor void hideCursor() { terminal.hideCursor; } /// returns the terminal width uint width() { return terminal.width; } } /// You can use this writer if you don't want to keep the data in memmory /// It's useful for unit testing. It supports line navigation, with no color /// The context info might be added in the future, once a good format is found. class BufferedWriter : ReportWriter { /// The buffer used to write the data string buffer = ""; private { size_t line = 0; size_t charPos = 0; bool replace; string[] screen; } /// go uo y lines void goTo(int y) { line = line - y; charPos = 0; } /// returns 80 uint width() { return 80; } /// void write(string text, Context) { auto lines = text.count!(a => a == '\n'); auto pieces = buffer.split("\n"); auto newLines = text.split("\n"); for (auto i = line; i < line + newLines.length; i++) { if (i != line) { charPos = 0; } while (i >= screen.length) { screen ~= ""; } auto newLine = newLines[i - line]; if (charPos + newLine.length >= screen[i].length) { screen[i] = screen[i][0 .. charPos] ~ newLine; } else { screen[i] = screen[i][0 .. charPos] ~ newLine ~ screen[i][charPos + newLine.length .. $]; } charPos = charPos + newLine.length; } buffer = screen.join("\n"); screen = buffer.split("\n"); line += lines; } /// void writeReverse(string text, Context c) { write(text, c); } /// void writeln(string text, Context c) { write(text ~ '\n', c); } /// does nothing void showCursor() { } /// does nothing void hideCursor() { } } version (unittest) { version(Have_fluent_asserts_core) { import fluent.asserts; } } @("Buffered writer should return an empty buffer") unittest { auto writer = new BufferedWriter; writer.buffer.should.equal(""); } @("Buffered writer should print text") unittest { auto writer = new BufferedWriter; writer.write("1", ReportWriter.Context._default); writer.buffer.should.equal("1"); } @("Buffered writer should print text and add a new line") unittest { auto writer = new BufferedWriter; writer.write("1", ReportWriter.Context._default); writer.writeln("2", ReportWriter.Context._default); writer.buffer.should.equal("12\n"); } @("Buffered writer should print text and a new line") unittest { auto writer = new BufferedWriter; writer.writeln("1", ReportWriter.Context._default); writer.write("2", ReportWriter.Context._default); writer.buffer.should.equal("1\n2"); } @("Buffered writer should go back 1 line") unittest { auto writer = new BufferedWriter; writer.writeln("1", ReportWriter.Context._default); writer.writeln("2", ReportWriter.Context._default); writer.goTo(2); writer.writeln("3", ReportWriter.Context._default); writer.buffer.should.equal("3\n2\n"); } @("Buffered writer should not replace a line if the new text is shorter") unittest { auto writer = new BufferedWriter; writer.writeln("11", ReportWriter.Context._default); writer.writeln("2", ReportWriter.Context._default); writer.goTo(2); writer.writeln("3", ReportWriter.Context._default); writer.buffer.should.equal("31\n2\n"); } @("Buffered writer should keep the old line number") unittest { auto writer = new BufferedWriter; writer.writeln("1", ReportWriter.Context._default); writer.writeln("2", ReportWriter.Context._default); writer.goTo(2); writer.writeln("", ReportWriter.Context._default); writer.writeln("3", ReportWriter.Context._default); writer.buffer.should.equal("1\n3\n"); } @("Buffered writer should keep the old line char position") unittest { auto writer = new BufferedWriter; writer.writeln("1", ReportWriter.Context._default); writer.writeln("2", ReportWriter.Context._default); writer.goTo(2); writer.write("3", ReportWriter.Context._default); writer.write("3", ReportWriter.Context._default); writer.buffer.should.equal("33\n2\n"); }