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;
49 Test(const std::string &);
53 static std::list<Factory *> &get_factories();
57 Function &add(void (T::*f)(), const std::string &d)
59 functions.push_back(new TypedFunction<T>(f, d));
60 return *functions.back();
64 static void run_single(const std::string &, bool);
65 static void run_all(bool);
69 void start_test(const std::string &);
71 void fail_test(const std::string &);
74 void expect(bool, const std::string &);
77 void expect_equal(const T &var, bool cond, const std::string &expr)
80 throw test_failed(format("!(%s)\nActual: %s", expr, var));
84 void info(const std::string &);
85 void debug(const std::string &);
89 class RegisteredTest: public Test
92 class Factory: public Test::Factory
95 virtual const char *get_name() const { return T::get_name(); }
96 virtual Test *create() const { return new T; }
99 static Factory factory;
102 RegisteredTest(): Test(T::get_name()) { (void)factory; }
106 typename RegisteredTest<T>::Factory RegisteredTest<T>::factory;