2 #include <msp/debug/demangle.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/utils.h>
12 /* This makes the linker pull in the Runner class when linking msptest as a
15 { return new Msp::Test::Runner(0, 0); }
23 Test::Test(const string &n):
29 for(list<Function *>::iterator i=functions.begin(); i!=functions.end(); ++i)
33 list<Test::Factory *> &Test::get_factories()
35 static list<Factory *> factories;
39 void Test::run_single(const string &name, bool verbose)
41 const list<Factory *> &factories = get_factories();
42 for(list<Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
43 if((*i)->get_name()==name)
45 Test *test = (*i)->create();
51 void Test::run_all(bool verbose)
53 const list<Factory *> &factories = get_factories();
54 IO::print("Running %d test suites\n", factories.size());
55 for(list<Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
57 Test *test = (*i)->create();
63 void Test::run(bool verbose)
65 IO::print("Running tests for %s\n", name);
67 unsigned n_passed = 0;
69 for(list<Function *>::const_iterator i=functions.begin(); i!=functions.end(); ++i)
71 detail_info = string();
72 detail_debug = string();
74 start_test((*i)->get_description());
75 const ExceptionCheck *exc_check = (*i)->get_exception_check();
80 fail_test("Exception expected but none thrown");
84 catch(const test_failed &e)
88 catch(const exception &e)
90 if(exc_check && exc_check->check(e))
93 debug(Debug::demangle(typeid(e).name()));
101 fail_test("Unknown object thrown");
105 detail_info += detail_debug;
106 if(!detail_info.empty())
108 vector<string> lines = split(detail_info, '\n');
109 for(vector<string>::const_iterator j=lines.begin(); j!=lines.end(); ++j)
110 IO::print(" %s\n", *j);
118 IO::print(" %d/%d passed\n", n_passed, total);
121 void Test::start_test(const string &descr)
123 IO::print(" %s: ", descr);
126 void Test::pass_test()
128 IO::print("\033[32mok\033[0m\n");
132 void Test::fail_test(const string &why)
134 IO::print("\033[31mfailed\033[0m\n");
135 IO::print(" %s\n", why);
138 void Test::expect(bool cond, const string &expr)
141 throw test_failed(format("!(%s)", expr));
145 void Test::info(const string &str)
147 detail_info += format("%s\n", str);
150 void Test::debug(const string &str)
152 detail_debug += format("%s\n", str);
155 void Test::fail(const string &why)
157 throw test_failed(why);
161 Test::Factory::Factory()
163 get_factories().push_back(this);