summaryrefslogtreecommitdiffstats
path: root/tests/test_main.cpp
blob: d74c473b49c55713932967f98f1b02ecf37402f5 (plain)
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
#include "framework.hpp"

#include <exception>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

namespace cepis::testing {
namespace {

std::vector<std::pair<std::string, std::function<void()>>>& registry() {
  static std::vector<std::pair<std::string, std::function<void()>>> tests;
  return tests;
}

}  // namespace

void register_test(const char* name, std::function<void()> body) {
  registry().emplace_back(name, std::move(body));
}

int run_all() {
  int failures = 0;
  for (const auto& [name, body] : registry()) {
    try {
      body();
      std::cout << "ok   " << name << '\n';
    } catch (const std::exception& error) {
      ++failures;
      std::cout << "FAIL " << name << "\n       " << error.what() << '\n';
    } catch (...) {
      ++failures;
      std::cout << "FAIL " << name << "\n       unknown exception\n";
    }
  }
  std::cout << '\n' << registry().size() << " test(s), " << failures << " failure(s)" << std::endl;
  return failures == 0 ? 0 : 1;
}

}  // namespace cepis::testing

int main() { return cepis::testing::run_all(); }