-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathkoangtestprinter.cpp
More file actions
135 lines (117 loc) · 4.64 KB
/
Copy pathkoangtestprinter.cpp
File metadata and controls
135 lines (117 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "koangtestprinter.h"
using namespace testing;
using namespace testing::internal;
// kludge to get access to gtest internal stuff which cppkoans finds usefull but
// which gtest did not intend to put on their public interface.
namespace testing {
namespace internal {
enum GTestColor {
COLOR_DEFAULT,
COLOR_RED,
COLOR_GREEN,
COLOR_YELLOW
};
void ColoredPrintf(GTestColor color, const char* fmt, ...);
}
}
namespace {
void printFileAndLineNo(const TestPartResult& part_result) {
#ifdef _MSC_VER
printf("%s(%d): error:\n", part_result.file_name(), part_result.line_number());
#else
printf("%s:%d:\n", part_result.file_name(), part_result.line_number());
#endif
}
}
KoanGTestPrinter::KoanGTestPrinter() :
m_PassedKoansCount(0)
{
}
void KoanGTestPrinter::OnTestProgramStart(const UnitTest&)
{
printf("The master is examining your answers:\n");
}
void KoanGTestPrinter::OnTestEnd(const TestInfo& test_info)
{
const TestResult& test_result = *test_info.result();
if (test_result.Passed()) {
ColoredPrintf(COLOR_GREEN,
"Koan %s in area %s has expanded your awareness.\n",
test_info.name(), test_info.test_case_name() );
m_PassedKoansCount++;
} else {
bool all_unanswered = true;
for (int i=0; i<test_result.total_part_count(); i++) {
const TestPartResult& part_result = test_result.GetTestPartResult(i);
if ( part_result.failed() ) {
bool answered = strcmp(part_result.summary(),"_")!=0;
all_unanswered &= !answered;
} else {
all_unanswered = false;
break;
}
}
if (all_unanswered) {
ColoredPrintf(COLOR_RED,
"\nKoan %s in area %s shall be your next step on your path to enlightenment.\n",
test_info.name(), test_info.test_case_name());
const TestPartResult& part_result = test_result.GetTestPartResult(0);
printf("Meditate on the following code:\n");
printFileAndLineNo(part_result);
}
else {
ColoredPrintf(COLOR_RED,
"\nYour answer to koan %s in area %s has damaged your karma.\n",
test_info.name(), test_info.test_case_name());
for (int i=0; i<test_result.total_part_count(); i++) {
const TestPartResult& part_result =
test_result.GetTestPartResult(i);
if ( part_result.failed() ) {
const char* summary = part_result.summary();
if (strcmp(summary,"__schelte__")==0) {
printf("\nDon't try to cheat me - you will succeed eventually.\n");
printf("However mind that you will not reach enlightenment by going this path.\n");
printf("Continue to faithfully meditate on the following code:\n");
printFileAndLineNo(part_result);
}
else if (strcmp(summary,"_")==0) {
printf("\nYou did not yet answer the following question, continue to meditate on it:\n");
printFileAndLineNo(part_result);
}
else {
printf("\nYour expectation did not match actual reality:\n%s\n",
summary);
// the file_name(line_number) format is currently only optimized for
// msvc, so one can easily jump to the file/line in the IDE by double
// clicking on that line in the output
printf("You must continue to meditate on the following code:\n");
printFileAndLineNo(part_result);
}
}
}
}
// Hat's tip to Ara T. Howard for the zen statements from his metakoans
// Ruby Quiz (http://rubyquiz.com/quiz67.html)
const char* statement = NULL;
switch ((m_PassedKoansCount/3) % 6) {
case 0: statement = "Mountains are merely mountains"; break;
case 1: statement = "Learn the rules so you know how to break them properly"; break;
case 2: statement = "Remember that silence is sometimes the best answer"; break;
case 3: statement = "Sleep is the best meditation"; break;
case 4: statement = "When you lose, don't lose the lesson"; break;
default: statement = "Things are not what they appear to be: nor are they otherwise";
}
printf("\n%s\n",statement);
printf("Your path to enlightenment thus far: %d of %d koans\n",
m_PassedKoansCount, UnitTest::GetInstance()->test_to_run_count());
}
}
void KoanGTestPrinter::OnTestProgramEnd(const UnitTest& unit_test) {
if ( unit_test.Passed() ) {
printf("\nYou reached enlightenment! You are now a master yourself and shall guide\n"
"others on their paths.\n");
}
// in case of failure, OnTestEnd already printed all the info and
// StopOnFirstFailureListener aborted running the koans, i.e. in case of
// failure we never reach this point.
}