1 module specd.reporter;
2 
3 import specd.specd;
4 
5 import std.stdio;
6 
7 interface Reporter {
8 
9 	void reportSpecificationGroup(SpecificationGroup group);
10 
11 	void reportSpecification(Specification specification);
12 
13 	void reportSummary(int totalNumberOfSpecs, int numberOfFailedSpecs);
14 
15 	final bool report() {
16 		int total = 0;
17 		int failures = 0;
18 		foreach(specGroup; SpecificationGroup.allSpecs) {
19 			reportSpecificationGroup(specGroup);
20 			foreach(spec; specGroup.specifications) {
21 				++total;
22 				if (!spec.isSuccess) {
23 					++failures;
24 				}
25 				reportSpecification(spec);
26 			}
27 		}
28 		reportSummary(total, failures);
29 		return failures == 0;
30 	}
31 }
32 
33 class ConsoleReporter : Reporter {
34 	override void reportSpecificationGroup(SpecificationGroup group) {
35 		writeln(mark(group.isSuccess), group.title, " should", markOff());
36 	}
37 
38 	override void reportSpecification(Specification spec) {
39 		writeln(mark(spec.isSuccess), "  ", spec.test, markOff());
40 		if (!spec.isSuccess)
41 			writeln(spec.exception);
42 	}
43 
44 	override void reportSummary(int totalNumberOfSpecs, int numberOfFailedSpecs) {
45 		auto success = numberOfFailedSpecs == 0;
46 		writeln(mark(success), success ? "SUCCESS" : "FAILURE",
47 			" Failed: ", numberOfFailedSpecs,
48 			" out of ", totalNumberOfSpecs,
49 			markOff());
50 	}
51 
52 version(Posix) {
53 	string mark(bool success) {
54 		if (success)
55 			return "\x1b[32m";
56 		else
57 			return "\x1b[31m";
58 	}
59 
60 	string markOff() {
61 		return "\x1b[39m";
62 	}		
63 } else {
64 	string mark(bool success) { return success ? "[ OK ] " : "[FAIL] "; }
65 	string markOff() { return ""; }
66 }
67 
68 }