| 25 | | |
|---|
| 26 | | //------------------------------------------------------------------------------ |
|---|
| 27 | | |
|---|
| 28 | | struct TestCase { |
|---|
| 29 | | string name; |
|---|
| 30 | | } |
|---|
| 31 | | |
|---|
| 32 | | struct Trace { |
|---|
| | 25 | import std2.conv; |
|---|
| | 26 | |
|---|
| | 27 | import doost.core.Traits; |
|---|
| | 28 | |
|---|
| | 29 | //------------------------------------------------------------------------------ |
|---|
| | 30 | |
|---|
| | 31 | enum Align {Left, Center, Right} |
|---|
| | 32 | |
|---|
| | 33 | //------------------------------------------------------------------------------ |
|---|
| | 34 | |
|---|
| | 35 | interface Element { |
|---|
| | 36 | string pattern(); |
|---|
| | 37 | string render(string val); |
|---|
| | 38 | } |
|---|
| | 39 | |
|---|
| | 40 | //------------------------------------------------------------------------------ |
|---|
| | 41 | |
|---|
| | 42 | class CellElem : Element { |
|---|
| | 43 | public: |
|---|
| | 44 | this(uint rwidth, Align aligning, string form="%s", uint border = 1) { |
|---|
| | 45 | m_cwidth = rwidth; |
|---|
| | 46 | m_aligning = aligning; |
|---|
| | 47 | m_border = border; |
|---|
| | 48 | m_form = form; |
|---|
| | 49 | } |
|---|
| | 50 | |
|---|
| | 51 | override string pattern() { |
|---|
| | 52 | return m_form; |
|---|
| | 53 | } |
|---|
| | 54 | |
|---|
| | 55 | override string render(string content) { |
|---|
| | 56 | string result; |
|---|
| | 57 | |
|---|
| | 58 | result~=repeat(" ", m_border); |
|---|
| | 59 | if (content.length + 2*m_border > m_cwidth) { |
|---|
| | 60 | content = content[0.. m_cwidth-2*m_border -1] ~ "."; |
|---|
| | 61 | } |
|---|
| | 62 | |
|---|
| | 63 | uint clen = content.length + 2*m_border; |
|---|
| | 64 | |
|---|
| | 65 | if (m_aligning == Align.Left) { |
|---|
| | 66 | result~=content; |
|---|
| | 67 | result~=repeat(" ", m_cwidth - clen); |
|---|
| | 68 | } else |
|---|
| | 69 | if (m_aligning == Align.Right) { |
|---|
| | 70 | result~=repeat(" ", m_cwidth - clen); |
|---|
| | 71 | result~=content; |
|---|
| | 72 | } else |
|---|
| | 73 | if (m_aligning == Align.Center) { |
|---|
| | 74 | uint empty = m_cwidth - clen; |
|---|
| | 75 | result~=repeat(" ", empty / 2); |
|---|
| | 76 | result~=content; |
|---|
| | 77 | result~=repeat(" ", empty - empty / 2); |
|---|
| | 78 | } |
|---|
| | 79 | |
|---|
| | 80 | result~=repeat(" ", m_border); |
|---|
| | 81 | return result; |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | private: |
|---|
| | 85 | uint m_cwidth; |
|---|
| | 86 | Align m_aligning; |
|---|
| | 87 | uint m_border; |
|---|
| | 88 | string m_form; |
|---|
| | 89 | } |
|---|
| | 90 | |
|---|
| | 91 | //------------------------------------------------------------------------------ |
|---|
| | 92 | |
|---|
| | 93 | class SepElem : Element { |
|---|
| | 94 | public: |
|---|
| | 95 | this(string form="|") { |
|---|
| | 96 | m_form = form; |
|---|
| | 97 | } |
|---|
| | 98 | override string pattern() { |
|---|
| | 99 | return m_form; |
|---|
| | 100 | } |
|---|
| | 101 | override string render(string str) { |
|---|
| | 102 | return str; |
|---|
| | 103 | } |
|---|
| | 104 | private: |
|---|
| | 105 | string m_form; |
|---|
| | 106 | } |
|---|
| | 107 | |
|---|
| | 108 | //------------------------------------------------------------------------------ |
|---|
| | 109 | |
|---|
| | 110 | class HlElem : CellElem { |
|---|
| | 111 | public: |
|---|
| | 112 | this(string form="-") { |
|---|
| | 113 | super(-1, Align.Left, form, 1); |
|---|
| | 114 | } |
|---|
| | 115 | override string pattern() { |
|---|
| | 116 | return m_form; |
|---|
| | 117 | } |
|---|
| | 118 | override string render(string str) { |
|---|
| | 119 | return repeat(" ", m_border) ~ repeat(m_form, m_cwidth-2*m_border) ~ repeat(" ", m_border); |
|---|
| | 120 | } |
|---|
| | 121 | } |
|---|
| | 122 | |
|---|
| | 123 | //------------------------------------------------------------------------------ |
|---|
| | 124 | |
|---|
| | 125 | alias Element[] Row; |
|---|
| | 126 | |
|---|
| | 127 | //TODO: struktura Row, i opAssign(Element[] val); w czasie przypisania sÄ |
|---|
| | 128 | |
|---|
| | 129 | //obliczane rozmiary kolumn |
|---|
| | 130 | |
|---|
| | 131 | //------------------------------------------------------------------------------ |
|---|
| | 132 | Element Sep(string form = "|") { |
|---|
| | 133 | return new SepElem(form); |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | //------------------------------------------------------------------------------ |
|---|
| | 137 | |
|---|
| | 138 | Element Cell(uint rwidth, Align aligning, string form="%s", uint border = 1) { |
|---|
| | 139 | return new CellElem(rwidth, aligning, form, border); |
|---|
| | 140 | } |
|---|
| | 141 | |
|---|
| | 142 | //------------------------------------------------------------------------------ |
|---|
| | 143 | |
|---|
| | 144 | Element Hl(string form="-") { |
|---|
| | 145 | return new HlElem(form); |
|---|
| | 146 | } |
|---|
| | 147 | |
|---|
| | 148 | //------------------------------------------------------------------------------ |
|---|
| | 149 | |
|---|
| | 150 | class Table { |
|---|
| | 151 | public: |
|---|
| | 152 | this(uint width = 75) { |
|---|
| | 153 | m_width = width; |
|---|
| | 154 | } |
|---|
| | 155 | |
|---|
| | 156 | void render(T...)(Row r, T param) { |
|---|
| | 157 | uint scount; // number of separators |
|---|
| | 158 | uint ccount; // number of cells |
|---|
| | 159 | string onerow; //one row |
|---|
| | 160 | uint clen; //content length + borders |
|---|
| | 161 | uint defwidth; |
|---|
| | 162 | CellElem dyncol; |
|---|
| | 163 | uint pos; |
|---|
| | 164 | |
|---|
| | 165 | |
|---|
| | 166 | foreach(e; r) { |
|---|
| | 167 | if (cast(SepElem)e) scount++; |
|---|
| | 168 | if (auto elem = cast(CellElem)e) { |
|---|
| | 169 | ccount++; |
|---|
| | 170 | if (elem.m_cwidth == -1) { |
|---|
| | 171 | if (dyncol !is null) throw new Exception("Only one column can have dynamic width"); |
|---|
| | 172 | dyncol = elem; |
|---|
| | 173 | } else defwidth+=elem.m_cwidth; |
|---|
| | 174 | } |
|---|
| | 175 | } |
|---|
| | 176 | |
|---|
| | 177 | if (defwidth + scount>m_width) throw new Exception("Columns have more than 100% width"); |
|---|
| | 178 | if (dyncol !is null) { |
|---|
| | 179 | dyncol.m_cwidth = m_width - defwidth - scount; |
|---|
| | 180 | if (dyncol.m_cwidth<dyncol.m_border*2+1) throw new Exception("Dynamic column is too narrow!"); |
|---|
| | 181 | } |
|---|
| | 182 | |
|---|
| | 183 | string content; |
|---|
| | 184 | foreach(i, e; r) { |
|---|
| | 185 | if (argNumberToMunch(e.pattern) > 0) { |
|---|
| | 186 | content = ""; |
|---|
| | 187 | foreach(j, v; param) { |
|---|
| | 188 | if (j == pos) { |
|---|
| | 189 | content = format(e.pattern, v); |
|---|
| | 190 | pos++; |
|---|
| | 191 | break; |
|---|
| | 192 | } |
|---|
| | 193 | } |
|---|
| | 194 | } else content = e.pattern; |
|---|
| | 195 | |
|---|
| | 196 | onerow ~= e.render(content); |
|---|
| | 197 | } |
|---|
| | 198 | |
|---|
| | 199 | if (pos < param.length) throw new Exception("Not all tokens consumed!"); |
|---|
| | 200 | |
|---|
| | 201 | writefln("%s", onerow); |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | private: |
|---|
| | 205 | uint argNumberToMunch(string p) { |
|---|
| | 206 | return count(p, "%") - 2*count(p, "%%"); |
|---|
| | 207 | } |
|---|
| | 208 | |
|---|
| | 209 | uint m_width; |
|---|
| | 210 | } |
|---|
| | 211 | |
|---|
| | 212 | unittest { |
|---|
| | 213 | auto p = new Table; |
|---|
| | 214 | assert(p.argNumberToMunch("%% %% %% %% %") == 1); |
|---|
| | 215 | assert(p.argNumberToMunch("% %% % %% %") == 3); |
|---|
| | 216 | |
|---|
| | 217 | } |
|---|
| | 218 | |
|---|
| | 219 | //------------------------------------------------------------------------------ |
|---|
| | 220 | |
|---|
| | 221 | struct TraceInfo { |
|---|
| 65 | | |
|---|
| 66 | | //TODO: traces are not connected with failures; should be stored separately |
|---|
| 67 | | //posibility to show traces only on failure |
|---|
| 68 | | |
|---|
| 69 | | TestCase[uint] testcases; |
|---|
| 70 | | Trace[][uint] traces; |
|---|
| 71 | | Failure[uint] failed; |
|---|
| 72 | | |
|---|
| 73 | | uint tc_counter, tc_fcounter; |
|---|
| 74 | | void delegate() setUp, tearDown; |
|---|
| 75 | | d_time starttime, elapsedtime; |
|---|
| 76 | | |
|---|
| 77 | | |
|---|
| 78 | | const table_width = 70; |
|---|
| 79 | | const table_col1_width = 3; |
|---|
| 80 | | const table_col3_width = 6; |
|---|
| 81 | | const table_col2_width = table_width - table_col1_width - table_col3_width - 5; |
|---|
| 82 | | |
|---|
| 83 | | const table_hr = " " ~ ctfe_repeat("-", table_width-2) ~ " "; |
|---|
| 84 | | |
|---|
| 85 | | const table_simple = "|%-"~ ctfe_ulongTostring(table_width-2) ~"s|"; |
|---|
| 86 | | const table_tc_col1 = "|%"~ ctfe_ulongTostring(table_col1_width) ~"d."; |
|---|
| 87 | | const table_tc_col2 = " %-"~ ctfe_ulongTostring(table_col2_width) ~"s"; |
|---|
| 88 | | const table_tc_col3 = "%"~ ctfe_ulongTostring(table_col3_width) ~"s |"; |
|---|
| 89 | | |
|---|
| 90 | | const table_stat_all = "| - all : %-3d" ~ ctfe_repeat(" ", table_width-18) ~"|"; |
|---|
| 91 | | const table_stat_pass = "| - passed : %-3d (%6.2f%%)" ~ ctfe_repeat(" ", table_width-28) ~"|"; |
|---|
| 92 | | const table_stat_fail = "| - failed : %-3d (%6.2f%%)" ~ ctfe_repeat(" ", table_width-28) ~"|"; |
|---|
| 93 | | |
|---|
| 94 | | const table_fail_row1 = "|%3d. %-"~ ctfe_ulongTostring(table_width-7) ~"s|"; |
|---|
| 95 | | const table_fail_row2 = "| %-"~ ctfe_ulongTostring(table_width-7) ~"s|"; |
|---|
| 96 | | |
|---|
| 97 | | //TODO: lepsze funkcje do wydruku tabeli (bardziej uniwersalne) |
|---|
| 98 | | //moÅŒe wystarczy tylko usuwaÄ z ciÄ |
|---|
| 99 | | gu formatujÄ |
|---|
| 100 | | cego wszystkie spacje? np. |
|---|
| 101 | | //wydruk nie tylko na ekran, ale takŌe do pliku + do kilku źródeŠ|
|---|
| 102 | | |
|---|
| 103 | | |
|---|
| 104 | | // ----------------------------------------------- |
|---|
| 105 | | // | * test suite... | Status | Count | Time | |
|---|
| 106 | | // ----------------------------------------------- |
|---|
| 107 | | // |* |* | *| * | * | |
|---|
| 108 | | // ----------------------------------------------- |
|---|
| 109 | | // | Traces: | |
|---|
| 110 | | // | | |
|---|
| 111 | | // ----------------------------------------------- |
|---|
| 112 | | |
|---|
| 113 | | //------------------------------------------------------------------------------ |
|---|
| 114 | | |
|---|
| 115 | | //TODO: ewentualnie dodawanie setup i teardown w osobnych funkcjach, ale |
|---|
| | 234 | |
|---|
| | 235 | //------------------------------------------------------------------------------ |
|---|
| | 236 | |
|---|
| | 237 | class TestCase { |
|---|
| | 238 | public: |
|---|
| | 239 | void execute(string testname, void delegate() dg) { |
|---|
| | 240 | m_testname = testname; |
|---|
| | 241 | m_passed = false; |
|---|
| | 242 | m_elapsedtime = real.nan; |
|---|
| | 243 | |
|---|
| | 244 | int no; |
|---|
| | 245 | string result; |
|---|
| | 246 | |
|---|
| | 247 | if (m_repeat != 0) { |
|---|
| | 248 | no = m_repeat; |
|---|
| | 249 | m_owner.m_tccounter++; |
|---|
| | 250 | m_number = m_owner.m_tccounter; |
|---|
| | 251 | try { |
|---|
| | 252 | d_time starttime = getUTCtime(); |
|---|
| | 253 | for(uint i = 0; i<m_repeat; i++) { |
|---|
| | 254 | if (m_setup !is null) m_setup(); |
|---|
| | 255 | dg(); |
|---|
| | 256 | if (m_teardown !is null) m_teardown(); |
|---|
| | 257 | } |
|---|
| | 258 | m_elapsedtime = (cast(real)(getUTCtime() - starttime))/1000; |
|---|
| | 259 | result = "OK"; |
|---|
| | 260 | m_passed = true; |
|---|
| | 261 | } |
|---|
| | 262 | catch(Exception e) { |
|---|
| | 263 | result = "FAIL"; |
|---|
| | 264 | m_error = e.toString; |
|---|
| | 265 | m_owner.m_tcfcounter++; |
|---|
| | 266 | m_owner.m_tcfailed~=this; |
|---|
| | 267 | no = 0; |
|---|
| | 268 | } |
|---|
| | 269 | } else result = "DISABLED"; |
|---|
| | 270 | |
|---|
| | 271 | string tc_no; |
|---|
| | 272 | if (m_repeat == 0) {tc_no = "-- "; no = 0; } |
|---|
| | 273 | else tc_no = to!(string)(m_owner.m_tccounter) ~ "."; |
|---|
| | 274 | |
|---|
| | 275 | if (m_dotiming) m_owner.table.render(m_owner.tc_tim, tc_no, m_testname ~ " test...", result, no, m_elapsedtime); |
|---|
| | 276 | else m_owner.table.render(m_owner.tc, tc_no, m_testname ~ " test...", result); |
|---|
| | 277 | |
|---|
| | 278 | if (m_elapsedtime !is real.nan) m_owner.m_elapsedtime+=m_elapsedtime; |
|---|
| | 279 | } |
|---|
| | 280 | |
|---|
| | 281 | TestCase setUp(void delegate() setup) { |
|---|
| | 282 | m_setup = setup; |
|---|
| | 283 | return this; |
|---|
| | 284 | } |
|---|
| | 285 | |
|---|
| | 286 | TestCase tearDown(void delegate() teardown) { |
|---|
| | 287 | m_teardown = teardown; |
|---|
| | 288 | return this; |
|---|
| | 289 | } |
|---|
| | 290 | |
|---|
| | 291 | TestCase repeat(uint count) { |
|---|
| | 292 | m_repeat = count; |
|---|
| | 293 | return this; |
|---|
| | 294 | } |
|---|
| | 295 | |
|---|
| | 296 | TestCase disable() { |
|---|
| | 297 | m_repeat = 0; |
|---|
| | 298 | return this; |
|---|
| | 299 | } |
|---|
| | 300 | |
|---|
| | 301 | TestCase traces() { |
|---|
| | 302 | m_dotraces = true; |
|---|
| | 303 | m_owner.m_reqtraces = true; |
|---|
| | 304 | return this; |
|---|
| | 305 | } |
|---|
| | 306 | |
|---|
| | 307 | private: |
|---|
| | 308 | this(TestSuite owner) { |
|---|
| | 309 | m_owner = owner; |
|---|
| | 310 | } |
|---|
| | 311 | |
|---|
| | 312 | void printTraces() { |
|---|
| | 313 | string lastfile = ""; |
|---|
| | 314 | foreach(t; m_traces) { |
|---|
| | 315 | if (t.file != lastfile) { |
|---|
| | 316 | m_owner.table.render(m_owner.simple); |
|---|
| | 317 | if (t.file != "") m_owner.table.render(m_owner.simple, " " ~ getBaseName(t.file) ~ ":"); |
|---|
| | 318 | else m_owner.table.render(m_owner.simple); |
|---|
| | 319 | lastfile = t.file; |
|---|
| | 320 | } |
|---|
| | 321 | |
|---|
| | 322 | if (t.line !=0) m_owner.table.render(m_owner.simple, " " ~ format("%4d: %s", t.line, t.trace)); |
|---|
| | 323 | else m_owner.table.render(m_owner.simple, " " ~ t.trace); |
|---|
| | 324 | } |
|---|
| | 325 | } |
|---|
| | 326 | |
|---|
| | 327 | |
|---|
| | 328 | string m_testname; |
|---|
| | 329 | uint m_repeat = 1; |
|---|
| | 330 | void delegate() m_setup; |
|---|
| | 331 | void delegate() m_teardown; |
|---|
| | 332 | |
|---|
| | 333 | TraceInfo[] m_traces; |
|---|
| | 334 | string m_error; |
|---|
| | 335 | bool m_passed; |
|---|
| | 336 | bool m_dotiming; |
|---|
| | 337 | bool m_dotraces; |
|---|
| | 338 | |
|---|
| | 339 | real m_elapsedtime; |
|---|
| | 340 | uint m_number; |
|---|
| | 341 | TestSuite m_owner; |
|---|
| | 342 | |
|---|
| | 343 | } |
|---|
| | 344 | |
|---|
| | 345 | //------------------------------------------------------------------------------ |
|---|
| | 346 | |
|---|
| | 347 | class TestGroup { |
|---|
| | 348 | public: |
|---|
| | 349 | void begin(string groupname="") { |
|---|
| | 350 | m_groupname = groupname; |
|---|
| | 351 | if (groupname != "") { |
|---|
| | 352 | //m_owner.table.render(m_owner.simple); |
|---|
| | 353 | /*if (!first)*/ m_owner.table.render(m_owner.hl); |
|---|
| | 354 | m_owner.table.render(m_owner.simple, "Group: " ~ groupname); |
|---|
| | 355 | /*if (!last)*/ //m_owner.table.render(m_owner.hl); |
|---|
| | 356 | } |
|---|
| | 357 | } |
|---|
| | 358 | |
|---|
| | 359 | void finish() { |
|---|
| | 360 | |
|---|
| | 361 | } |
|---|
| | 362 | |
|---|
| | 363 | TestGroup setUp(void delegate() setup) { |
|---|
| | 364 | m_setup = setup; |
|---|
| | 365 | return this; |
|---|
| | 366 | } |
|---|
| | 367 | |
|---|
| | 368 | TestGroup tearDown(void delegate() teardown) { |
|---|
| | 369 | m_teardown = teardown; |
|---|
| | 370 | return this; |
|---|
| | 371 | } |
|---|
| | 372 | |
|---|
| | 373 | TestGroup repeat(uint count) { |
|---|
| | 374 | m_repeat = count; |
|---|
| | 375 | return this; |
|---|
| | 376 | } |
|---|
| | 377 | |
|---|
| | 378 | TestGroup disable() { |
|---|
| | 379 | m_repeat = 0; |
|---|
| | 380 | return this; |
|---|
| | 381 | } |
|---|
| | 382 | |
|---|
| | 383 | private: |
|---|
| | 384 | this(TestSuite owner) { |
|---|
| | 385 | m_owner = owner; |
|---|
| | 386 | } |
|---|
| | 387 | |
|---|
| | 388 | string m_groupname; |
|---|
| | 389 | uint m_repeat = 1; |
|---|
| | 390 | void delegate() m_setup; |
|---|
| | 391 | void delegate() m_teardown; |
|---|
| | 392 | bool m_dotiming; |
|---|
| | 393 | bool m_dotraces; |
|---|
| | 394 | |
|---|
| | 395 | TestSuite m_owner; |
|---|
| | 396 | } |
|---|
| | 397 | |
|---|
| | 398 | //------------------------------------------------------------------------------ |
|---|
| | 399 | |
|---|
| | 400 | class TestSuite { |
|---|
| | 401 | public: |
|---|
| | 402 | //TODO: function disable, which sets repeat to 0 |
|---|
| | 403 | //TODO: tylko jeden otwarty test suite jednoczeÅnie. |
|---|
| | 404 | //jeÅŒeli ktoÅ próbuje otworzyÄ drugi to musi czekaÄ na skoÅczenie pierwszego?? |
|---|
| | 405 | //jeÅŒeli na tym samym threadzie, to po prostu bÅÄ |
|---|
| | 406 | d |
|---|
| | 407 | |
|---|
| | 408 | void begin(string suitename) { |
|---|
| | 409 | m_suitename = suitename; |
|---|
| | 410 | m_tccounter = 0; |
|---|
| | 411 | m_tcfcounter = 0; |
|---|
| | 412 | |
|---|
| | 413 | table.render(hl); |
|---|
| | 414 | table.render(title, m_suitename ~ " test suite."); |
|---|
| | 415 | //table.render(hl); |
|---|
| | 416 | } |
|---|
| | 417 | |
|---|
| | 418 | void finish() { |
|---|
| | 419 | uint tcpcounter = m_tccounter - m_tcfcounter; |
|---|
| | 420 | |
|---|
| | 421 | if (m_tcfailed.length>0) { |
|---|
| | 422 | table.render(hl); |
|---|
| | 423 | table.render(simple, "Failed test cases / errors / traces:"); |
|---|
| | 424 | table.render(simple); |
|---|
| | 425 | foreach(tc; m_tcfailed) { |
|---|
| | 426 | table.render(fail, tc.m_number, tc.m_testname); |
|---|
| | 427 | table.render(simple, " " ~ tc.m_error); |
|---|
| | 428 | table.render(simple); |
|---|
| | 429 | tc.printTraces; |
|---|
| | 430 | } |
|---|
| | 431 | |
|---|
| | 432 | } |
|---|
| | 433 | |
|---|
| | 434 | if (m_dotraces || m_reqtraces) { |
|---|
| | 435 | table.render(hl); |
|---|
| | 436 | table.render(simple, "Traces:"); |
|---|
| | 437 | table.render(simple); |
|---|
| | 438 | foreach(tc; m_testcases) { |
|---|
| | 439 | if (tc.m_repeat == 0 || tc.m_dotraces == false) continue; |
|---|
| | 440 | table.render(fail, tc.m_number, tc.m_testname); |
|---|
| | 441 | tc.printTraces; |
|---|
| | 442 | table.render(simple); |
|---|
| | 443 | } |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | table.render(hl); |
|---|
| | 447 | table.render(simple); |
|---|
| | 448 | table.render(etime, "Statistics :", "Total time :", m_elapsedtime); |
|---|
| | 449 | table.render(stat, " - all :", m_tccounter); |
|---|
| | 450 | table.render(stat, " - passed :", tcpcounter, cast(double)(tcpcounter*100)/m_tccounter); |
|---|
| | 451 | table.render(stat, " - failed :", m_tcfcounter, cast(double)(m_tcfcounter*100)/m_tccounter); |
|---|
| | 452 | table.render(hl); |
|---|
| | 453 | |
|---|
| | 454 | } |
|---|
| | 455 | |
|---|
| | 456 | TestSuite setUp(void delegate() setup) { |
|---|
| | 457 | m_setup = setup; |
|---|
| | 458 | return this; |
|---|
| | 459 | } |
|---|
| | 460 | |
|---|
| | 461 | TestSuite tearDown(void delegate() teardown) { |
|---|
| | 462 | m_teardown = teardown; |
|---|
| | 463 | return this; |
|---|
| | 464 | } |
|---|
| | 465 | |
|---|
| | 466 | TestSuite timing() { |
|---|
| | 467 | m_dotiming = true; |
|---|
| | 468 | return this; |
|---|
| | 469 | } |
|---|
| | 470 | |
|---|
| | 471 | TestSuite traces() { |
|---|
| | 472 | m_dotraces = true; |
|---|
| | 473 | return this; |
|---|
| | 474 | } |
|---|
| | 475 | |
|---|
| | 476 | TestSuite repeat(uint count) { |
|---|
| | 477 | m_repeat = count; |
|---|
| | 478 | return this; |
|---|
| | 479 | } |
|---|
| | 480 | |
|---|
| | 481 | TestSuite disable() { |
|---|
| | 482 | m_repeat = 0; |
|---|
| | 483 | return this; |
|---|
| | 484 | } |
|---|
| | 485 | |
|---|
| | 486 | TestSuite width(uint w) { |
|---|
| | 487 | table.m_width = w; |
|---|
| | 488 | return this; |
|---|
| | 489 | } |
|---|
| | 490 | |
|---|
| | 491 | private: |
|---|
| | 492 | /*************************************************************************** |
|---|
| | 493 | Private constructor to avoid creating suites from outside |
|---|
| | 494 | **************************************************************************/ |
|---|
| | 495 | this() { |
|---|
| | 496 | table = new Table; |
|---|
| | 497 | |
|---|
| | 498 | title = [Sep, Cell(-1, Align.Center), Sep]; |
|---|
| | 499 | tc = [Sep, Cell(5, Align.Right, "%-s", 0), Cell(-1, Align.Left), Cell(7, Align.Right), Sep]; |
|---|
| | 500 | tc_tim = [Sep, Cell(5, Align.Right, "%-s", 0), Cell(-1, Align.Left), Cell(7, Align.Right), Sep, Cell(8, Align.Right), Sep, Cell(8, Align.Right, "%6.2f"), Sep]; |
|---|
| | 501 | simple = [Sep, Cell(-1, Align.Left), Sep]; |
|---|
| | 502 | etime = [Sep, Cell(15, Align.Right), Cell(-1, Align.Right), Cell(10, Align.Left, "%6.4f s"), Sep]; |
|---|
| | 503 | stat = [Sep, Cell(15, Align.Right), Cell(6, Align.Left), Cell(-1, Align.Left, "(%6.2f %%)"), Sep]; |
|---|
| | 504 | fail = [Sep, Cell(5, Align.Right, "%d."), Cell(-1, Align.Left), Sep]; |
|---|
| | 505 | hl = [Hl]; |
|---|
| | 506 | |
|---|
| | 507 | } |
|---|
| | 508 | |
|---|
| | 509 | /*************************************************************************** |
|---|
| | 510 | **************************************************************************/ |
|---|
| | 511 | TestGroup createGroup() { |
|---|
| | 512 | auto tg = new TestGroup(this); |
|---|
| | 513 | tg.repeat(m_repeat); |
|---|
| | 514 | tg.setUp(m_setup); |
|---|
| | 515 | tg.tearDown(m_teardown); |
|---|
| | 516 | tg.m_dotiming = m_dotiming; |
|---|
| | 517 | tg.m_dotraces = m_dotraces; |
|---|
| | 518 | m_testgroups~=tg; |
|---|
| | 519 | m_curgroup = tg; |
|---|
| | 520 | m_firstingroup = true; |
|---|
| | 521 | return tg; |
|---|
| | 522 | } |
|---|
| | 523 | |
|---|
| | 524 | TestCase createCase() { |
|---|
| | 525 | auto tc = new TestCase(this); |
|---|
| | 526 | if (m_curgroup !is null) { |
|---|
| | 527 | tc.repeat(m_curgroup.m_repeat); |
|---|
| | 528 | tc.setUp(m_curgroup.m_setup); |
|---|
| | 529 | tc.tearDown(m_curgroup.m_teardown); |
|---|
| | 530 | tc.m_dotiming = m_curgroup.m_dotiming; |
|---|
| | 531 | tc.m_dotraces = m_dotraces; |
|---|
| | 532 | } else { |
|---|
| | 533 | tc.repeat(m_repeat); |
|---|
| | 534 | tc.setUp(m_setup); |
|---|
| | 535 | tc.tearDown(m_teardown); |
|---|
| | 536 | tc.m_dotiming = m_dotiming; |
|---|
| | 537 | tc.m_dotraces = m_dotraces; |
|---|
| | 538 | } |
|---|
| | 539 | |
|---|
| | 540 | if (m_firstingroup) {table.render(hl); m_firstingroup=false;} |
|---|
| | 541 | m_testcases~=tc; |
|---|
| | 542 | m_curcase = tc; |
|---|
| | 543 | return m_testcases[$-1]; |
|---|
| | 544 | } |
|---|
| | 545 | |
|---|
| | 546 | Table table; |
|---|
| | 547 | Row title, tc, tc_tim, simple, etime, stat, fail, hl; |
|---|
| | 548 | |
|---|
| | 549 | void delegate() m_setup; |
|---|
| | 550 | void delegate() m_teardown; |
|---|
| | 551 | bool m_dotiming; |
|---|
| | 552 | bool m_dotraces; |
|---|
| | 553 | bool m_reqtraces; |
|---|
| | 554 | string m_suitename; |
|---|
| | 555 | uint m_repeat = 1; |
|---|
| | 556 | bool m_firstingroup=true; |
|---|
| | 557 | |
|---|
| | 558 | TestCase m_curcase; |
|---|
| | 559 | TestCase[] m_testcases; |
|---|
| | 560 | TestCase[] m_tcfailed; |
|---|
| | 561 | TestGroup m_curgroup; |
|---|
| | 562 | TestGroup[] m_testgroups; |
|---|
| | 563 | real m_elapsedtime=0; |
|---|
| | 564 | |
|---|
| | 565 | uint m_tccounter, m_tcfcounter; |
|---|
| | 566 | } |
|---|
| 173 | | if (v is null || second[i] is null) { |
|---|
| 174 | | if (v !is second[i]) return false; |
|---|
| 175 | | continue; |
|---|
| 176 | | } |
|---|
| 177 | | if (v != second[i]) return false; |
|---|
| 178 | | } |
|---|
| 179 | | |
|---|
| 180 | | return true; |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | //------------------------------------------------------------------------------ |
|---|
| 184 | | |
|---|
| 185 | | void testcase(string name, void delegate() dg, uint number = 1) { |
|---|
| 186 | | tc_counter++; |
|---|
| 187 | | testcases[tc_counter] = TestCase(name); |
|---|
| 188 | | writef(table_tc_col1, tc_counter); |
|---|
| 189 | | writef(table_tc_col2, name ~ " test..."); |
|---|
| 190 | | try { |
|---|
| 191 | | //TODO: add timing of every test case |
|---|
| 192 | | d_time starttime = getUTCtime(); |
|---|
| 193 | | for(uint i = 0; i<number; i++) { |
|---|
| 194 | | if (setUp !is null) setUp(); |
|---|
| 195 | | dg(); |
|---|
| 196 | | if (tearDown !is null) tearDown(); |
|---|
| 197 | | } |
|---|
| 198 | | d_time elapsedtime = getUTCtime() - starttime; |
|---|
| 199 | | |
|---|
| 200 | | writefln(table_tc_col3, "OK"); |
|---|
| 201 | | } |
|---|
| 202 | | catch(Exception e) { |
|---|
| 203 | | writefln(table_tc_col3, "FAIL"); |
|---|
| 204 | | tc_fcounter++; |
|---|
| 205 | | failed[tc_counter] = Failure(e.toString); |
|---|
| 206 | | } |
|---|
| | 633 | static if (is(ElemType TYPE == TYPE*) || is(ElemType == class)) { |
|---|
| | 634 | res = compareRefs(v, second[i]); |
|---|
| | 635 | } else |
|---|
| | 636 | static if (is(ElemType == float) || is(ElemType == double) || is(ElemType == real)) { |
|---|
| | 637 | res = compareFloats(v, second[i]); |
|---|
| | 638 | } else |
|---|
| | 639 | static assert("Don't know how to compare arrays."); |
|---|
| | 640 | |
|---|
| | 641 | if (res == false) break; |
|---|
| | 642 | } |
|---|
| | 643 | |
|---|
| | 644 | return res; |
|---|
| | 645 | } |
|---|
| | 646 | |
|---|
| | 647 | //------------------------------------------------------------------------------ |
|---|
| | 648 | |
|---|
| | 649 | TestSuite suite; |
|---|
| | 650 | |
|---|
| | 651 | TestSuite testSuite() { |
|---|
| | 652 | if (suite is null) suite = new TestSuite; |
|---|
| | 653 | return suite; |
|---|
| | 654 | } |
|---|
| | 655 | |
|---|
| | 656 | TestGroup testGroup() { |
|---|
| | 657 | assert(suite !is null, "You have to start suite before creating test group!"); |
|---|
| | 658 | return suite.createGroup; |
|---|
| | 659 | } |
|---|
| | 660 | |
|---|
| | 661 | TestCase testCase() { |
|---|
| | 662 | assert(suite !is null, "You have to start suite before creating test case!"); |
|---|
| | 663 | return suite.createCase; |
|---|
| 243 | | |
|---|
| 244 | | //------------------------------------------------------------------------------ |
|---|
| 245 | | enum {TIMING=1, TRACES=2} |
|---|
| 246 | | |
|---|
| 247 | | void finishSuite(int params=0) { |
|---|
| 248 | | uint tc_pcounter = tc_counter - tc_fcounter; |
|---|
| 249 | | |
|---|
| 250 | | if (failed.length>0) { |
|---|
| 251 | | writefln(table_hr); |
|---|
| 252 | | writefln(table_simple, " Failed test cases / errors / traces:"); |
|---|
| 253 | | writefln(table_simple, " "); |
|---|
| 254 | | foreach(k, v; failed) { |
|---|
| 255 | | writefln(table_fail_row1, k, testcases[k].name); |
|---|
| 256 | | writefln(table_fail_row2, failed[k].error); |
|---|
| 257 | | writefln(table_simple, " "); |
|---|
| 258 | | if (k in traces) { |
|---|
| 259 | | printTraces(k); |
|---|
| 260 | | writefln(table_simple, " "); |
|---|
| 261 | | } |
|---|
| 262 | | } |
|---|
| 263 | | } |
|---|
| 264 | | |
|---|
| 265 | | if (params && TRACES && traces.length>0) { |
|---|
| 266 | | writefln(table_hr); |
|---|
| 267 | | writefln(table_simple, " Traces:"); |
|---|
| 268 | | writefln(table_simple, " "); |
|---|
| 269 | | foreach(no, val; traces) { |
|---|
| 270 | | writefln(table_fail_row1, no, testcases[no].name); |
|---|
| 271 | | printTraces(no); |
|---|
| 272 | | writefln(table_simple, " "); |
|---|
| 273 | | } |
|---|
| 274 | | } |
|---|
| 275 | | |
|---|
| 276 | | writefln(table_hr); |
|---|
| 277 | | writefln(table_simple, " Statistics:"); |
|---|
| 278 | | writefln(table_stat_all, tc_counter); |
|---|
| 279 | | writefln(table_stat_pass, tc_pcounter, cast(double)(tc_pcounter*100)/tc_counter); |
|---|
| 280 | | writefln(table_stat_fail, tc_fcounter, cast(double)(tc_fcounter*100)/tc_counter); |
|---|
| 281 | | writefln(table_hr); |
|---|
| 282 | | writefln; |
|---|
| 283 | | writefln; |
|---|
| 284 | | } |
|---|
| | 671 | //------------------------------------------------------------------------------ |
|---|
| | 672 | |
|---|
| | 673 | void tracefl(T...)(string file, long line, T params) { |
|---|
| | 674 | debug { |
|---|
| | 675 | if (suite is null) return; |
|---|
| | 676 | if (suite.m_curcase is null) return; |
|---|
| | 677 | suite.m_curcase.m_traces ~= TraceInfo(format(params), line, file); |
|---|
| | 678 | } |
|---|
| | 679 | } |
|---|
| | 680 | |
|---|
| | 681 | //------------------------------------------------------------------------------ |
|---|
| | 682 | |
|---|
| | 683 | void trace(T...)(T params) { |
|---|
| | 684 | debug { |
|---|
| | 685 | if (suite is null) return; |
|---|
| | 686 | if (suite.m_curcase is null) return; |
|---|
| | 687 | suite.m_curcase.m_traces ~= TraceInfo(format(params)); |
|---|
| | 688 | } |
|---|
| | 689 | } |
|---|