pianod2
multisource multiuser scriptable networked music player
failurecounter.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <iostream>
12 #include <sstream>
13 
14 #ifdef HAVE_NCURSES
15 #include <curses.h>
16 #include <term.h>
17 #endif
18 
19 namespace Test {
20 
22  private:
23  static int grand_total_tests;
24  static int grand_total_passes;
25  static int grand_total_errors;
27  static int grand_total_skips;
28  static std::string green;
29  static std::string red;
30  static std::string yellow;
31  static std::string bold;
32  static std::string normal;
33  std::string name;
34  std::string subtest_name;
35  int tests{0};
36  int passes{0};
37  int errors{0};
38  int warnings{0};
39  int skips{0};
40  int check_number{0};
41 
42  public:
47  static void initialize_colors() {
48 #ifdef HAVE_NCURSES
49  setupterm (NULL, 1, NULL);
50  const char *setcolor = tigetstr ("setaf");
51  green = setcolor ? tiparm (setcolor, 2) : "";
52  red = setcolor ? tiparm (setcolor, 1) : "";
53  yellow = setcolor ? tiparm (setcolor, 3) : "";
54  const char *temp = tigetstr ("sgr0");
55  normal = temp ? temp : "";
56  temp = tigetstr ("bold");
57  bold = temp ? temp : "";
58 #else
59  std::cerr << "Not compiled with ncurses; colors not available\n";
60 #endif
61  }
62 
65  FailureCounter (const std::string &n) : name (n) {
66  std::cerr << "==============================================\n"
67  "Performing test "
68  << bold << name << normal << std::endl
69  << "----------------------------------------------\n\n";
70  }
71 
74  std::cerr << std::endl << "Test group " << name << std::endl << tests << " tests run: ";
80  assert (tests == passes + errors + warnings + skips);
81  if (errors) {
82  std::cerr << errors << red << " FAILURES, " << normal;
83  }
84  std::cerr << passes << green << " passed" << normal;
85  if (warnings) {
86  std::cerr << ", " << warnings << yellow << " warnings" << normal;
87  }
88  if (skips) {
89  std::cerr << ", " << skips << yellow << " tests skipped" << normal;
90  }
91  std::cerr << std::endl << std::endl;
92  }
93 
96  void subtest (const std::string &n) {
97  subtest_name = n;
98  check_number = 0;
99  std::cerr << std::endl << "=== Testing " << name << " / " << subtest_name << " ===" << std::endl;
100  }
101 
104  void skip (const std::string &reason) {
105  check_number++;
106  tests++;
107  skips++;
108  std::cout << yellow << "skip: " << normal << reason << std::endl;
109  }
110 
113  void warning (const std::string &reason) {
114  check_number++;
115  tests++;
116  warnings++;
117  std::cout << yellow << "warning: " << normal << reason << std::endl;
118  }
119 
122  bool succeed (const std::string &success) {
123  check_number++;
124  tests++;
125  passes++;
126  std::cout << green << "ok: " << normal << success << std::endl;
127  return true;
128  }
129 
133  bool fail (const std::string &failure, const std::string &detail = "") {
134  check_number++;
135  tests++;
136  errors++;
137  std::cout << red << name << (subtest_name.empty() ? "" : " / ") << subtest_name << " check #"
138  << check_number << " error:" << normal << std::endl;
139  std::cout << failure << (detail.empty() ? "" : ": ") << detail << std::endl;
140  return false;
141  }
142 
143  bool result (bool result, const std::string &test, const std::string &detail = "") {
144  if (result) {
145  return succeed (test);
146  } else {
147  return fail (test, detail);
148  }
149  }
150 
151  template <typename T>
152  bool equal (const std::string &test_name, const T &expect, const T &actual) {
153  if (result (expect == actual, test_name, "mismatch:")) {
154  return true;
155  }
156  std::cout << "mismatch- expect: '" << expect << '\'' << std::endl;
157  std::cout << " actual: '" << actual << '\'' << std::endl;
158  return false;
159  }
160 
161  bool equal (const std::string &test_name, const char *expect, const char *actual) {
162  return equal (test_name, std::string{expect}, std::string{actual});
163  }
164 
165  bool equal (const std::string &test_name, const char *expect, const std::string &actual) {
166  return equal (test_name, std::string{expect}, actual);
167  }
168 
169  bool equal (const std::string &test_name, long expect, const long actual) {
170  return equal<long> (test_name, expect, actual);
171  }
172 
173  bool isTrue (const std::string &test_name, bool actual) {
174  return equal (test_name, "true", actual ? "true" : "false");
175  }
176 
177  bool isFalse (const std::string &test_name, bool actual) {
178  return equal (test_name, "false", actual ? "true" : "false");
179  }
180 
181  bool signEqual (const std::string &test_name, long expect, long actual) {
182  return equal (test_name, expect < 0 ? -1 : expect > 0 ? 1 : 0, actual < 0 ? -1 : actual > 0 ? 1 : 0);
183  }
184 
185  /*
186  * 2-parameter checks
187  */
188  template <typename T>
189  bool equal (const T &expect, const T &actual) {
190  if (expect == actual) {
191  std::stringstream name;
192  name << expect;
193  return succeed (name.str());
194  }
195  fail ("mismatch:");
196  std::cout << "mismatch- expect: '" << expect << '\'' << std::endl;
197  std::cout << " actual: '" << actual << '\'' << std::endl;
198  return false;
199  }
200 
201  bool equal (const char *expect, const char *actual) {
202  return equal (std::string{expect}, std::string{actual});
203  }
204 
205  bool equal (const char *expect, const std::string &actual) {
206  return equal (std::string{expect}, actual);
207  }
208 
209  bool equal (long expect, const long actual) {
210  return equal<long> (expect, actual);
211  }
212 
213  bool isTrue (bool actual) {
214  return equal ("true", actual ? "true" : "false");
215  }
216 
217  bool isFalse (bool actual) {
218  return equal ("false", actual ? "true" : "false");
219  }
220 
221  /*
222  * Specialized checks
223  */
224  bool sameException (const std::string &trigger,
225  const std::exception &expect,
226  const std::exception &actual,
227  bool compare_text = false) {
228  if (typeid (expect) == typeid (actual)) {
229  if (compare_text && (strcmp (expect.what(), actual.what()) != 0)) {
230  fail ("Wrong exception text", trigger);
231  std::cerr << "mismatch- expect: " << expect.what() << std::endl;
232  std::cerr << " actual: " << actual.what() << std::endl;
233  return false;
234  }
235  succeed (trigger + ": " + expect.what());
236  return true;
237  }
238  fail ("Wrong exception", trigger);
239  std::cerr << "mismatch- expect: " << typeid (expect).name() << std::endl;
240  std::cerr << " actual: " << typeid (actual).name() << std::endl;
241  return false;
242  }
243 
244  bool sameExceptionAndText (const std::string &trigger,
245  const std::exception &expect,
246  const std::exception &actual) {
247  return sameException (trigger, expect, actual, true);
248  }
249 
250  inline int errorCount() {
251  return errors;
252  }
253 
254  static inline int grandTotalErrors() {
255  return grand_total_errors;
256  }
257 
258  static inline void summary() {
259  std::ostringstream sum;
260  if (grand_total_errors == 0) {
261  std::cerr << "All tests passed!" << std::endl << std::endl;
262  ;
263  } else {
264  std::cerr << "Some tests FAILED!" << normal << std::endl << std::endl;
265  }
266  std::cerr << "Tests performed: " << grand_total_tests << std::endl;
267  std::cerr << "Tests passed: " << green << grand_total_passes << normal << std::endl;
268  if (grand_total_errors) {
269  std::cerr << "Tests failed: " << red << grand_total_errors << normal << std::endl;
270  }
271  if (grand_total_warnings) {
272  std::cerr << "Warnings generated: " << yellow << grand_total_warnings << normal << std::endl;
273  }
274  if (grand_total_skips) {
275  std::cerr << "Tests skipped " << yellow << grand_total_skips << normal << std::endl;
276  }
277  }
278  };
279  std::string FailureCounter::green;
280  std::string FailureCounter::red;
281  std::string FailureCounter::yellow;
282  std::string FailureCounter::bold;
283  std::string FailureCounter::normal;
289 
290 } // namespace Test
Definition: failurecounter.h:21
static std::string normal
Definition: failurecounter.h:32
void warning(const std::string &reason)
Record that a test generated a warning.
Definition: failurecounter.h:113
static std::string yellow
Definition: failurecounter.h:30
FailureCounter(const std::string &n)
Start a new group of tests.
Definition: failurecounter.h:65
bool equal(const std::string &test_name, const T &expect, const T &actual)
Definition: failurecounter.h:152
std::string subtest_name
Definition: failurecounter.h:34
bool isFalse(const std::string &test_name, bool actual)
Definition: failurecounter.h:177
int check_number
Definition: failurecounter.h:40
bool sameExceptionAndText(const std::string &trigger, const std::exception &expect, const std::exception &actual)
Definition: failurecounter.h:244
void subtest(const std::string &n)
Provide a name for a group of subtests.
Definition: failurecounter.h:96
int errorCount()
Definition: failurecounter.h:250
static int grand_total_skips
Definition: failurecounter.h:27
static int grand_total_passes
Definition: failurecounter.h:24
bool fail(const std::string &failure, const std::string &detail="")
Record and report a failed test.
Definition: failurecounter.h:133
int passes
Definition: failurecounter.h:36
bool equal(const T &expect, const T &actual)
Definition: failurecounter.h:189
bool equal(long expect, const long actual)
Definition: failurecounter.h:209
bool succeed(const std::string &success)
Record and report a successful test.
Definition: failurecounter.h:122
~FailureCounter()
On destruction, report summary of test results.
Definition: failurecounter.h:73
int warnings
Definition: failurecounter.h:38
bool isTrue(const std::string &test_name, bool actual)
Definition: failurecounter.h:173
static std::string red
Definition: failurecounter.h:29
static std::string green
Definition: failurecounter.h:28
bool equal(const std::string &test_name, const char *expect, const std::string &actual)
Definition: failurecounter.h:165
static std::string bold
Definition: failurecounter.h:31
bool result(bool result, const std::string &test, const std::string &detail="")
Definition: failurecounter.h:143
std::string name
Definition: failurecounter.h:33
bool sameException(const std::string &trigger, const std::exception &expect, const std::exception &actual, bool compare_text=false)
Definition: failurecounter.h:224
bool equal(const std::string &test_name, long expect, const long actual)
Definition: failurecounter.h:169
static int grandTotalErrors()
Definition: failurecounter.h:254
bool signEqual(const std::string &test_name, long expect, long actual)
Definition: failurecounter.h:181
static void initialize_colors()
If ncurses is available, initializes terminal color escape sequences.
Definition: failurecounter.h:47
bool equal(const std::string &test_name, const char *expect, const char *actual)
Definition: failurecounter.h:161
bool equal(const char *expect, const char *actual)
Definition: failurecounter.h:201
int skips
Definition: failurecounter.h:39
bool isFalse(bool actual)
Definition: failurecounter.h:217
static int grand_total_errors
Definition: failurecounter.h:25
static void summary()
Definition: failurecounter.h:258
int errors
Definition: failurecounter.h:37
int tests
Definition: failurecounter.h:35
bool equal(const char *expect, const std::string &actual)
Definition: failurecounter.h:205
static int grand_total_warnings
Definition: failurecounter.h:26
void skip(const std::string &reason)
Record that a test was skipped.
Definition: failurecounter.h:104
static int grand_total_tests
Definition: failurecounter.h:23
bool isTrue(bool actual)
Definition: failurecounter.h:213
Definition: failurecounter.h:19