1 #ifndef MSP_TEST_TEST_H_
2 #define MSP_TEST_TEST_H_
6 #include <msp/strings/format.h>
12 #define EXPECT(c) expect(c, #c)
13 #define EXPECT_EQUAL(a, b) expect_equal(a, a==b, #a " == " #b)
15 class test_failed: public std::exception
21 test_failed(const std::string &w): what_(w) { }
22 virtual ~test_failed() throw() { }
24 const char *what() const throw() { return what_.c_str(); }
37 virtual const char *get_name() const = 0;
38 virtual Test *create() const = 0;
43 std::list<Function *> functions;
44 std::string detail_info;
45 std::string detail_debug;
46 std::string fail_reason;
50 Test(const std::string &);
54 static std::list<Factory *> &get_factories();
58 Function &add(void (T::*f)(), const std::string &d)
60 functions.push_back(new TypedFunction<T>(f, d));
61 return *functions.back();
65 static void run_single(const std::string &, bool);
66 static void run_all(bool);
70 void start_test(const std::string &);
72 void fail_test(const std::string &);
75 void expect(bool, const std::string &);
78 void expect_equal(const T &var, bool cond, const std::string &expr)
81 throw test_failed(format("!(%s)\nActual: %s", expr, var));
85 void info(const std::string &);
86 void debug(const std::string &);
87 void fail(const std::string &);
91 class RegisteredTest: public Test
94 class Factory: public Test::Factory
97 virtual const char *get_name() const { return T::get_name(); }
98 virtual Test *create() const { return new T; }
101 static Factory factory;
104 RegisteredTest(): Test(T::get_name()) { (void)factory; }
108 typename RegisteredTest<T>::Factory RegisteredTest<T>::factory;