41 static inline std::string getUnitName(
const char *n_);
42 static inline std::string getTestName(
const Test *t);
43 typedef void (*test_func)();
55 Test(
const char *file,
const char *name_, test_func func_)
59 unit = getUnitName(file);
60 Test::getTests().push_back(
this);
63 static std::vector<Test*> &getTests() {
64 static std::vector<Test*> tests;
80 TestSetup::getSetups().push_back(
this);
83 static std::vector<TestSetup*> &getSetups() {
84 static std::vector<TestSetup*> setups;
91 main(
int argc,
const char **argv)
93 for(
auto setup : TestSetup::getSetups()) {
97 auto filtered(Test::getTests());
99 std::remove_if(filtered.begin(), filtered.end(),
101 return argc > 1 && !std::any_of(argv + 1, argv + argc,
102 [&](
const char *substr) {
103 return getTestName(x).find(substr) != std::string::npos;
108 std::sort(filtered.begin(), filtered.end(),
110 return getTestName(x) < getTestName(y);
114 if(getenv(
"RUN_COUNT")) {
115 run_count = std::stoi(getenv(
"RUN_COUNT"));
118 std::cout <<
"Selected " << filtered.size() <<
" tests." << std::endl;
119 for(
int i = 0; i < run_count; i++) {
120 for(
auto test : filtered) {
121 std::cout <<
"Running " << getTestName(test) <<
" ..." << std::endl;
134 static inline std::string
135 getUnitName(
const char *n_)
138 n = n.erase(n.find_last_of(
"."));
140 size_t slash_p = n.find_last_of(
"/");
141 if(slash_p != std::string::npos) {
142 n = n.erase(0, 1+slash_p);
149 static inline std::string
150 getTestName(
const Test *t)
170 template<
typename Exception,
179 s +=
typeid(Exception).name();
180 s +=
" was not raised";
181 fprintf(stderr,
"%s\n", s.c_str());
182 throw std::runtime_error(s);
183 }
catch(Exception &e) {
192 #define MU_DEBUG(x, ...) \ 193 fprintf(stderr, __FILE__ ": " x "\n", __VA_ARGS__); 196 #define MU_SETUP(name) \ 197 static void setup_##name(); \ 199 static TestSetup _##name##_testsetup (&setup_##name); \ 201 static void setup_##name() 214 #define MU_TEST(name) \ 215 static void test_##name(); \ 217 static Test _##name##_testinfo (__FILE__, #name, &test_##name); \ 219 static void test_##name() 228 int main(int argc, const char **argv) { \ 229 return myunit::main(argc, argv); \ myunit: Ultra tiny test framework.
Definition: myunit.hpp:37
Internal descriptor for a function to be executed once during unit initialization.
Definition: myunit.hpp:74
Internal descriptor for a test case, tracks the name of the unit where the test was defined...
Definition: myunit.hpp:50
Exception raises(Expr expr)
Crash if an exception was not thrown.
Definition: myunit.hpp:173