1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
| #ifndef MTEST_H_ #define MTEST_H_
#include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <string> #include <vector>
class MTest { public: using FuncType = void(int *);
struct TestItem { bool match_filter{false}; bool success{true}; FuncType *f; const char *itemname; const char *fullname;
TestItem(FuncType *arg_f, const char *arg_itemname, const char *arg_fullname) : f(arg_f), itemname(arg_itemname), fullname(arg_fullname) {} };
using TestSet = std::vector<TestItem>;
class MTestMessage { public: MTestMessage(bool always_output, bool important_output) : m_always_output(always_output), m_important_output(important_output) {}
template <typename T> MTestMessage &operator<<(T info) { if (m_important_output || !brief_output()) { if (m_always_output) { std::cout << info; return *this; }
if (!expect_result()) { std::cout << info << '\n'; return *this; } } return *this; }
template <typename T> static void evaluate_if_required(const char *str, T value) { if (std::to_string(value) != str) { std::cout << " Which is: " << value << '\n'; } }
static bool &expect_result() noexcept { static bool bool_expect_result{true}; return bool_expect_result; }
static bool &brief_output() noexcept { static bool bool_expect_result{false}; return bool_expect_result; }
private: const bool m_always_output; const bool m_important_output; };
static MTestMessage &msg() { static MTestMessage msg(true, false); return msg; }
static MTestMessage &msg_even_brief() { static MTestMessage msg(true, true); return msg; }
static MTestMessage &msg_when_fail_even_brief() { static MTestMessage msg(false, true); return msg; }
static void init_mtest(int argc, char *argv[], const char *main_file) { get_instance().set_main_file(main_file); get_instance().set_from_argv(argc, argv); }
static void init_mtest(int argc, char *argv[]) { get_instance().set_from_argv(argc, argv); }
static void init_mtest(const char *main_file) { get_instance().set_main_file(main_file); }
static void init_mtest() {}
static int add_test_item(const char *test_set_name, const char *test_itemname, FuncType *f, const char *test_fullname) noexcept { get_instance().set_test_item(test_set_name, test_itemname, f, test_fullname); return 0; }
static void set_filter(std::string filter, bool force_modify) { if (force_modify || get_instance().m_filter.empty()) get_instance().set_filter_force(filter); }
static int run_all_tests() { return get_instance().run(); }
private: static MTest &get_instance() noexcept { static MTest entity; return entity; }
int run() { set_filter("*", false); set_matched_count();
show_note_main(); show_note_filter();
show_start();
clock_t start_time_all{0}; clock_t start_time_test_set{0}; clock_t start_time_test_item{0}; clock_t end_time_all{0}; clock_t end_time_test_set{0}; clock_t end_time_test_item{0};
start_time_all = clock(); for (auto it = m_test_sets.begin(); it != m_test_sets.end(); it++) { std::string test_set_name = it->first; TestSet &test_set = it->second;
int count = 0; for (size_t i = 0; i < test_set.size(); i++) { if (test_set[i].match_filter) count++; } if (count == 0) continue;
info_one() << make_proper_str(count, "test", false) << " from " << test_set_name << '\n';
start_time_test_set = clock(); for (size_t i = 0; i < test_set.size(); i++) { if (!test_set[i].match_filter) continue;
const char *fullname = test_set[i].fullname; info_run() << fullname << '\n';
int tmp_fail_count = 0; start_time_test_item = clock(); test_set[i].f(&tmp_fail_count); end_time_test_item = clock(); test_set[i].success = (tmp_fail_count == 0);
if (test_set[i].success) { info_ok() << fullname << " (" << end_time_test_item - start_time_test_item << " ms)\n"; } else { info_failed() << fullname << " (" << end_time_test_item - start_time_test_item << " ms)\n";
++m_test_fail_count; } } end_time_test_set = clock();
info_one() << make_proper_str(count, "test", false) << " from " << test_set_name << " (" << end_time_test_set - start_time_test_set << " ms total)\n\n"; } end_time_all = clock(); m_cost_time_all = end_time_all - start_time_all;
show_result(); return m_test_fail_count; }
void list_and_exit() { set_filter("*", false); set_matched_count();
show_note_main();
for (auto it = m_test_sets.begin(); it != m_test_sets.end(); it++) { msg_even_brief() << it->first << ".\n";
const TestSet &test_set = it->second; for (size_t i = 0; i < test_set.size(); i++) { if (test_set[i].match_filter) msg_even_brief() << " " << test_set[i].itemname << '\n'; } }
exit(0); }
void help_and_exit() const { show_note_main(); show_logo();
msg_even_brief() << "[USAGE]\n"; msg_even_brief() << "This program contains tests written using MTest.\n" << "MTest is a gtest-style simple test framework by " "fenglielie@qq.com.\n" << "You can use the following command line flags to control " "its behavior:\n\n"; msg_even_brief() << "1. --mtest_filter=POSITIVE_PATTERN\n" << " Run only the tests whose name matches the positive " "pattern.\n" << " '?' matches any single character; '*' matches any " "substring(or empty).\n"; msg_even_brief() << "2. --mtest_list_tests\n" << " List the names of all tests without running them.\n" << "The name of TEST(Foo, Bar) is \"Foo.Bar\".\n"; msg_even_brief() << "3. --mtest_disable_color\n" << " Disable colorful output.\n"; msg_even_brief() << "4. --mtest_brief\n" << " Enable brief output.\n";
msg_even_brief() << '\n'; exit(0); }
void show_note_main() const { if (!m_main_file.empty() && m_main_file != "main.cpp") { msg_even_brief() << "Note: Running main() from " << m_main_file << '\n'; } }
void show_note_filter() const { if (m_filter != "*") msg_even_brief() << "Note: MTest filter = " << m_filter << '\n'; }
void show_start() const { show_logo();
info_two() << "Running " << make_proper_str(m_matched_count_items, "test", false) << " from " << make_proper_str(m_matched_count_sets, "test suite", false) << ".\n"; info_one() << "Global test environment set-up.\n"; }
void show_result() const { info_one() << "Global test environment tear-down\n"; info_two() << make_proper_str(m_matched_count_items, "test", false) << " from " << make_proper_str(m_matched_count_sets, "test suite", false) << " ran. (" << m_cost_time_all << " ms total)\n";
info_passed() << make_proper_str( m_matched_count_items - m_test_fail_count, "test", false) << ".\n";
if (m_test_fail_count != 0 && !MTestMessage::brief_output()) { info_failed() << make_proper_str(m_test_fail_count, "test", false) << ", listed below:\n";
for (auto it = m_test_sets.begin(); it != m_test_sets.end(); it++) { const TestSet &test_set = it->second; for (size_t i = 0; i < test_set.size(); i++) { if (!test_set[i].success) { info_failed() << test_set[i].fullname << '\n'; } } } if (m_use_color) { msg_even_brief() << "\n " << m_color_red << make_proper_str(m_test_fail_count, "FAILED TEST", true) << m_color_end << '\n'; } else { msg_even_brief() << "\n " << make_proper_str(m_test_fail_count, "FAILED TEST", true) << '\n'; } } }
void show_logo() const { if (m_use_color) { msg() << m_color_green << "\ __ __ _____ _____ ____ _____ \n\ | \\/ |_ _| ____/ ___|_ _|\n\ | |\\/| | | | | _| \\___ \\ | |\n\ | | | | | | | |___ ___) || |\n\ |_| |_| |_| |_____|____/ |_|\n" << m_color_end << '\n'; } else { msg() << "\ __ __ _____ _____ ____ _____ \n\ | \\/ |_ _| ____/ ___|_ _|\n\ | |\\/| | | | | _| \\___ \\ | |\n\ | | | | | | | |___ ___) || |\n\ |_| |_| |_| |_____|____/ |_|\n" << '\n'; } }
MTestMessage &info_passed() const { if (m_use_color) { msg_even_brief() << m_color_green << "[ PASSED ] " << m_color_end; } else { msg_even_brief() << "[ PASSED ] "; } return msg_even_brief(); }
MTestMessage &info_failed() const { if (m_use_color) { msg_even_brief() << m_color_red << "[ FAILED ] " << m_color_end; } else { msg_even_brief() << "[ FAILED ] "; } return msg_even_brief(); }
MTestMessage &info_ok() const { if (m_use_color) { msg() << m_color_green << "[ OK ] " << m_color_end; } else { msg() << "[ OK ] "; } return msg(); }
MTestMessage &info_two() const { if (m_use_color) { msg_even_brief() << m_color_green << "[==========] " << m_color_end; } else { msg_even_brief() << "[==========] "; } return msg_even_brief(); }
MTestMessage &info_one() const { if (m_use_color) { msg() << m_color_green << "[----------] " << m_color_end; } else { msg() << "[----------] "; } return msg(); }
MTestMessage &info_run() const { if (m_use_color) { msg() << m_color_green << "[ RUN ] " << m_color_end; } else { msg() << "[ RUN ] "; } return msg(); }
void set_test_item(const char *test_set_name, const char *test_itemname, FuncType *f, const char *test_fullname) noexcept { try { TestItem item(f, test_itemname, test_fullname); m_test_sets[test_set_name].emplace_back(item); } catch (...) { std::cerr << "Failed to add test item: " << test_fullname << '\n'; exit(1); } }
void set_filter_force(std::string filter) { m_filter = filter; }
void set_main_file(const char *main_file) { m_main_file = main_file; }
void set_from_argv(int argc, char *argv[]) { if (argc == 1) return;
const std::string filter_prefix = "--mtest_filter="; const std::string list_tests_option = "--mtest_list_tests"; const std::string disable_color_option = "--mtest_disable_color"; const std::string help_option = "--help"; const std::string brief_option = "--mtest_brief";
std::string arg_str; bool is_filter_override = false;
for (int i = 1; i < argc; i++) { arg_str = argv[i];
if (starts_with(arg_str, filter_prefix)) { if (!m_filter.empty()) is_filter_override = true; arg_str.erase(0, filter_prefix.length());
set_filter(arg_str, true); set_matched_count(); } else if (arg_str == disable_color_option) m_use_color = false; else if (arg_str == brief_option) MTestMessage::brief_output() = true; else if (arg_str == list_tests_option) list_and_exit(); else if (arg_str == help_option) help_and_exit(); else { msg_even_brief() << "Note: Unknown flag(" << arg_str << ") will be ignored.\n"; } }
if (is_filter_override) msg_even_brief() << "Note: MTest filter will be override by the last one."; }
void set_matched_count() { m_matched_count_items = 0; m_matched_count_sets = 0;
for (auto it = m_test_sets.begin(); it != m_test_sets.end(); it++) { TestSet &test_set = it->second;
int count = 0; for (size_t i = 0; i < test_set.size(); i++) { std::string fullname = test_set[i].fullname; if (str_match(fullname, m_filter)) { ++count; test_set[i].match_filter = true; ++m_matched_count_items; } else test_set[i].match_filter = false; }
if (count > 0) ++m_matched_count_sets; } }
static bool starts_with(const std::string &str, const std::string &prefix) { return prefix.size() <= str.size() && std::equal(prefix.cbegin(), prefix.cend(), str.cbegin()); }
static std::string make_proper_str(int num, const std::string &str, bool uppercase) { std::string res; if (num > 1) { if (uppercase) res = std::to_string(num) + " " + str + "S"; else res = std::to_string(num) + " " + str + "s"; } else { res = std::to_string(num) + " " + str; } return res; }
static bool str_match(std::string str, std::string pattern) { if (pattern == "*") return true;
const size_t m = str.size(); const size_t n = pattern.size(); std::vector<bool> prev(m + 1, false); std::vector<bool> curr(m + 1, false);
prev[0] = true;
for (size_t i = 1; i <= n; i++) { bool flag = true; for (size_t ii = 0; ii < i; ii++) { if (pattern[ii] != '*') { flag = false; break; } } curr[0] = flag;
for (size_t j = 1; j <= m; j++) { if (pattern[i - 1] == '*') { curr[j] = (curr[j - 1] || prev[j]); } else if ((pattern[i - 1] == '?') || (str[j - 1] == pattern[i - 1])) { curr[j] = prev[j - 1]; } else { curr[j] = false; } } prev = curr; }
return prev[m]; }
const char *m_color_red = "\x1b[91m"; const char *m_color_green = "\x1b[92m"; const char *m_color_end = "\x1b[0m";
std::map<const char *, TestSet> m_test_sets; int m_test_fail_count{0}; clock_t m_cost_time_all{0};
std::string m_filter; std::string m_main_file; bool m_use_color{true};
int m_matched_count_items{0}; int m_matched_count_sets{0}; };
#endif
|