trial.reporters.writer 96/139(69%) 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
1381
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1591
1600
1611
1621
1630
1641
1650
1660
1670
1680
1696060
1700
1710
1720
1730
1743030
1753030
1760
1773030
1780
1791063
1801063
1811063
1820
183221
184221
185221
1860
187221
188221
189221
1900
1914
1924
1934
1940
1951
1961
1971
1980
1995
2005
2015
2020
2031515
2041515
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2511515
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
26815258
2690
2701515
2710
2721515
2731515
2741515
2751515
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
293342
2940
2950
2960
2970
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
33650
33750
3380
3390
3400
3410
3420
34322
3440
3450
3460
3470
3480
3495311
350491
3510
352491
3530
3542310
3550
356664
3570
358238
3590
3600
361875
3620
363211
3640
3650
366664
3670
368664
3690
370576
3710
3720
3730
37488
3750
376664
3770
3780
379491
380491
381491
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
393180
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4171
4182
4190
4200
4210
4220
4230
4241
4251
4262
4270
4280
4290
4300
4310
4321
4331
4341
4352
4360
4370
4380
4390
4400
4411
4421
4431
4442
4450
4460
4470
4480
4490
4501
4511
4521
4531
4541
4552
4560
4570
4580
4590
4600
4611
4621
4631
4641
4651
4662
4670
4680
4690
4700
4710
4721
4731
4741
4751
4761
4771
4782
4790
4800
4810
4820
4830
4841
4851
4861
4871
4881
4891
4902
4910
/++ 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; /// 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) { 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"); }