| 1 |
/***************************************************************************************** |
|---|
| 2 |
* Support for consistancy of output from unittests. |
|---|
| 3 |
* |
|---|
| 4 |
* $(CASHEW_HEAD) |
|---|
| 5 |
* |
|---|
| 6 |
* Version: 0.2.4 |
|---|
| 7 |
* |
|---|
| 8 |
* Authors: |
|---|
| 9 |
* Reiner Pope <i>(initial concept and code)</i><br /> |
|---|
| 10 |
* Christopher Nicholson-Sauls |
|---|
| 11 |
* |
|---|
| 12 |
* Date: Aug 18, 2007 |
|---|
| 13 |
* |
|---|
| 14 |
*---------------------------------------------------------------------------------------- |
|---|
| 15 |
* module foo .bar ; |
|---|
| 16 |
* |
|---|
| 17 |
* static import UTest = cashew .utils .utest ; |
|---|
| 18 |
* |
|---|
| 19 |
* unittest { UTest.beginModule("foo.bar"); } |
|---|
| 20 |
* |
|---|
| 21 |
* int doStuff () { |
|---|
| 22 |
* // ... |
|---|
| 23 |
* } |
|---|
| 24 |
* unittest { |
|---|
| 25 |
* UTest.begin("doStuff()"); |
|---|
| 26 |
* assert(doStuff() == 0); |
|---|
| 27 |
* UTest.end; |
|---|
| 28 |
* } |
|---|
| 29 |
* |
|---|
| 30 |
* unittest { UTest.beginSection("special"); } |
|---|
| 31 |
* |
|---|
| 32 |
* int specialFunc () { |
|---|
| 33 |
* // ... |
|---|
| 34 |
* } |
|---|
| 35 |
* unittest { |
|---|
| 36 |
* UTest.begin("specialFunc()"); |
|---|
| 37 |
* assert(specialFunc() == 42); |
|---|
| 38 |
* UTest.end; |
|---|
| 39 |
* } |
|---|
| 40 |
* |
|---|
| 41 |
* unittest { |
|---|
| 42 |
* UTest.endSection; |
|---|
| 43 |
* UTest.endModule; |
|---|
| 44 |
* } |
|---|
| 45 |
*---------------------------------------------------------------------------------------- |
|---|
| 46 |
*/ |
|---|
| 47 |
module cashew .utils .UTest ; |
|---|
| 48 |
|
|---|
| 49 |
/***************************************************************************************** |
|---|
| 50 |
* Source for depth tabs. |
|---|
| 51 |
*/ |
|---|
| 52 |
private const STR_TABS = "\t\t\t\t\t\t\t\t"c ; |
|---|
| 53 |
|
|---|
| 54 |
/***************************************************************************************** |
|---|
| 55 |
* Current depth. |
|---|
| 56 |
*/ |
|---|
| 57 |
private size_t depth = 0_U ; |
|---|
| 58 |
|
|---|
| 59 |
/***************************************************************************************** |
|---|
| 60 |
* Source for item arrows. |
|---|
| 61 |
*/ |
|---|
| 62 |
private const ARROW = "\t-----------------------------> "c ; |
|---|
| 63 |
private char[] itemArrow ; |
|---|
| 64 |
|
|---|
| 65 |
/***************************************************************************************** |
|---|
| 66 |
* Current module name. |
|---|
| 67 |
*/ |
|---|
| 68 |
private char[] moduleName ; |
|---|
| 69 |
|
|---|
| 70 |
/***************************************************************************************** |
|---|
| 71 |
* Import the preferred output functionality. With Tango it uses io.Stdout, and with |
|---|
| 72 |
* Phobos it uses std.stdio. |
|---|
| 73 |
*/ |
|---|
| 74 |
version(Tango) { public import tango .io .Stdout ; } |
|---|
| 75 |
else { public import std .stdio ; } |
|---|
| 76 |
|
|---|
| 77 |
/***************************************************************************************** |
|---|
| 78 |
* Current depth string. |
|---|
| 79 |
*/ |
|---|
| 80 |
private char[] depthString () { |
|---|
| 81 |
return STR_TABS[0 .. depth]; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
/***************************************************************************************** |
|---|
| 85 |
* Control code for moduleOutput(). |
|---|
| 86 |
*/ |
|---|
| 87 |
private enum : size_t { |
|---|
| 88 |
HEADER = 0_U , |
|---|
| 89 |
FOOTER = 1_U |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
/***************************************************************************************** |
|---|
| 93 |
* Output module header/footer. |
|---|
| 94 |
*/ |
|---|
| 95 |
private void moduleOutput (size_t which) { |
|---|
| 96 |
static char[][] HDFT = ["begin"c, "end"c] ; |
|---|
| 97 |
|
|---|
| 98 |
version(Tango) { Stdout.formatln("Unittest: {0}: {1}"c, moduleName, HDFT[which]).flush; } |
|---|
| 99 |
else { writefln("Unittest: %s: %s"c , moduleName, HDFT[which]) ; } |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
/***************************************************************************************** |
|---|
| 103 |
* Call near the beginning of a module to header all unittest output. |
|---|
| 104 |
*/ |
|---|
| 105 |
void beginModule (char[] name) { |
|---|
| 106 |
moduleName = name; |
|---|
| 107 |
depth = 0_U; |
|---|
| 108 |
moduleOutput(HEADER); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
/***************************************************************************************** |
|---|
| 112 |
* Call at the end of a module to footer output. |
|---|
| 113 |
*/ |
|---|
| 114 |
void endModule () { |
|---|
| 115 |
moduleOutput(FOOTER); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
/***************************************************************************************** |
|---|
| 119 |
* Advance the depth and output a section title. |
|---|
| 120 |
*/ |
|---|
| 121 |
void beginSection (char[] name) { |
|---|
| 122 |
++depth; |
|---|
| 123 |
|
|---|
| 124 |
version(Tango) { Stdout.formatln("{0}[{1}]"c, depthString(), name).flush; } |
|---|
| 125 |
else { writefln("%s[%s]"c , depthString(), name) ; } |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
/***************************************************************************************** |
|---|
| 129 |
* Retreat the depth. |
|---|
| 130 |
*/ |
|---|
| 131 |
void endSection () { |
|---|
| 132 |
--depth; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
/***************************************************************************************** |
|---|
| 136 |
* Call at the beginning of a test item to output a name and arrow. |
|---|
| 137 |
*/ |
|---|
| 138 |
void begin (char[] name) { |
|---|
| 139 |
itemArrow = ARROW.dup; |
|---|
| 140 |
itemArrow[1 .. name.length + 1] = name; |
|---|
| 141 |
|
|---|
| 142 |
version(Tango) { Stdout.format("{}{}"c , depthString(), itemArrow).flush; } |
|---|
| 143 |
else { writef("%s%s"c , depthString(), itemArrow) ; } |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
/***************************************************************************************** |
|---|
| 147 |
* Call at the _end of a test item to output the word 'Pass' since the test obviously did, |
|---|
| 148 |
* or else it wouldn't reach this point! |
|---|
| 149 |
*/ |
|---|
| 150 |
void end () { |
|---|
| 151 |
version(Tango) { Stdout ("Pass"c).newline.flush; } |
|---|
| 152 |
else { writefln("Pass"c) ; } |
|---|
| 153 |
} |
|---|