1 /** 2 * Module to handle output to the console. 3 * 4 * License: 5 * MIT. See LICENSE for full details. 6 */ 7 module dunit.output.console; 8 9 /** 10 * Imports. 11 */ 12 import dunit.error; 13 import dunit.result.moduleresultcollection; 14 import std.array; 15 import std.conv; 16 import std.range; 17 import std.stdio; 18 import std..string; 19 20 /** 21 * Format output to the console. 22 */ 23 class Console 24 { 25 /** 26 * Write a line to the console. 27 * 28 * params: 29 * line = The line to write. 30 */ 31 public void write(string line) 32 { 33 writefln("%s", line); 34 } 35 36 /** 37 * Write an indented line to the console. 38 * 39 * params: 40 * line = The line to write. 41 * indent = The space indent before the line. 42 */ 43 public void write(string line, int indent) 44 { 45 this.write(format("%s%s", " ".repeat(indent).join(), line)); 46 } 47 48 /** 49 * Write a prefixed line to the console. 50 * 51 * params: 52 * prefix = The prefix of the line. 53 * line = The line to write. 54 */ 55 public void write(string prefix, string line) 56 { 57 this.write(format("%s %s", prefix, line)); 58 } 59 60 /** 61 * Write an intented, prefixed line to the console. 62 * 63 * params: 64 * prefix = The prefix of the line. 65 * line = The line to write. 66 * indent = The space indent before the line. 67 */ 68 public void write(string prefix, string line, int indent) 69 { 70 this.write(format("%s%s %s", " ".repeat(indent).join(), prefix, line)); 71 } 72 73 /** 74 * Write the header. 75 */ 76 public void writeHeader() 77 { 78 this.write(""); 79 this.write(">", "Running unit tests"); 80 } 81 82 /** 83 * Format and write an error to the console. 84 * 85 * Params: 86 * ex = The exception to output. 87 */ 88 private void writeError(DUnitAssertError ex) 89 { 90 this.write(""); 91 this.write("+----------------------------------------------------------------------", 2); 92 this.write("|", ex.msg, 2); 93 this.write("+----------------------------------------------------------------------", 2); 94 this.write("| File:", ex.file, 2); 95 this.write("| Line:", ex.line.text(), 2); 96 this.write("+----------------------------------------------------------------------", 2); 97 foreach (info; ex.log) 98 { 99 this.write("|", info, 2); 100 } 101 this.write(""); 102 } 103 104 /** 105 * Output a detailed report. 106 * 107 * Params: 108 * results = A module result collection. 109 */ 110 public void writeReport(ModuleResultCollection results) 111 { 112 foreach (result; results) 113 { 114 this.write("-", result.source); 115 116 if (result.error) 117 { 118 this.writeError(result.error); 119 } 120 } 121 122 if (results.failedCount()) 123 { 124 this.write(">", format("%s tests run. %s passed, %s failed", 125 results.totalCount(), 126 results.passedCount(), 127 results.failedCount() 128 )); 129 } 130 else 131 { 132 this.write(">", format("All %s tests passed", results.totalCount())); 133 } 134 } 135 }