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();
64 template<typename T, typename A>
65 Function &add(void (T::*f)(A), const A &a, const std::string &d)
67 functions.push_back(new TypedFunction1<T, A>(f, a, d));
68 return *functions.back();
72 static void run_single(const std::string &, bool);
73 static void run_all(bool);
77 void start_test(const std::string &);
79 void fail_test(const std::string &);
82 void expect(bool, const std::string &);
85 void expect_equal(const T &var, bool cond, const std::string &expr)
88 throw test_failed(format("!(%s)\nActual: %s", expr, var));
92 void info(const std::string &);
93 void debug(const std::string &);
94 void fail(const std::string &);
98 class RegisteredTest: public Test
101 class Factory: public Test::Factory
104 virtual const char *get_name() const { return T::get_name(); }
105 virtual Test *create() const { return new T; }
108 static Factory factory;
111 RegisteredTest(): Test(T::get_name()) { (void)factory; }
115 typename RegisteredTest<T>::Factory RegisteredTest<T>::factory;