BinaryTree  HW 06
Spell Checking Using Binary Trees
 All Classes Files Functions Variables Pages
testing-logger.hpp
Go to the documentation of this file.
1 
8 #ifndef TESTING_LOGGER_HPP_INCLUDED
9 #define TESTING_LOGGER_HPP_INCLUDED 1
10 
11 #include <map>
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15 
16 /*****
17  * Affirm Logger
18  *
19  * Affirm is like assert, but it doesn't halt the
20  * program. Instead, it counts how many times the affirmation fails.
21  *****/
22 
24 public:
25  TestingLogger(std::string name);
26  ~TestingLogger();
27  static void check(bool assertion, std::string description);
28 
29  // This version is like check, but takes a lambda function that
30  // returns a bool. It'll call the function in an environment where any
31  // exceptions are caught. This code is unusual in a number of ways.
32  template <typename Function>
33  static void checkSafely(Function assertionFn, std::string description)
34  {
35  try {
36  check(assertionFn(), description);
37  }
38  catch (std::exception& exn) {
39  exn_fail(&exn, exn.what(), description);
40  }
41  catch (const char* cstring) {
42  exn_fail(nullptr, cstring, description);
43  }
44  catch (...) {
45  exn_fail(nullptr, "", description);
46  }
47  }
48 
49  bool summarize(bool verbose = false);
50  void clear();
51  void abortOnFail();
52 
53  static TestingLogger* currentLogger;
54 
55 private:
56  struct AssertInfo {
57  int asserts_ = 0;
58  int failures_ = 0;
59  };
60 
61  static void exn_fail(std::exception* exn, std::string what,
62  std::string description);
63 
64  std::map<std::string, AssertInfo> assertions_;
65  using const_iter = std::map<std::string, AssertInfo>::const_iterator;
66  using iter = std::map<std::string, AssertInfo>::iterator;
67  std::string testName_;
68  bool failedSome_;
69  bool abortOnFail_;
70  TestingLogger* previousLogger_;
71 };
72 
73 // C has a global macro called assert, here we create our own called affirm.
74 
75 #define affirm(isTrue) \
76  TestingLogger::checkSafely([&]{ return isTrue; }, \
77  std::string(__FILE__) + ":" + std::to_string(__LINE__) \
78  + ":\t" + #isTrue)
79 
80 #endif // TESTING_LOGGER_HPP_INCLUDED
Definition: testing-logger.hpp:56
Definition: testing-logger.hpp:23