Echo Writes Code

cli.cpp

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
#include "crucible/testing/cli.hpp"

#include "crucible/testing/reporter.hpp"
#include "crucible/testing/suite.inl"

#include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include <vector>

namespace crucible::testing::cli {
  auto execute(int argc, char *argv[]) -> int {
    auto &suite { suite::Suite::get_reference() };

    std::vector<std::string> const arguments(argv + 1, argv + argc);
    std::set<std::string> group_filter {};
    bool show_fixtures { false };

    auto current = arguments.begin();
    auto const end = arguments.end();

    while (current != end) {
      auto &argument = *current++;

      if (argument == "--show-fixtures") {
        show_fixtures = true;
        continue;
      }

      if (argument == "-g" || argument == "--group") {
        if (current == end) {
          std::cerr
            << "Missing group name after '" << argument << "'\n"
            << "Usage: " << argv[0] << " [--show-fixtures] [-g|--group GROUP]...\n";

          return EXIT_FAILURE;
        }

        group_filter.emplace(*current++);
        continue;
      }

      std::cerr
        << "Unrecognized option or argument '" << argument << "'\n"
        << "Usage: " << argv[0] << " [--show-fixtures] [-g|--group GROUP]...\n";

      return EXIT_FAILURE;
    }

    suite.set_group_filter(group_filter);

    auto reporter { reporter::make_console_reporter() };
    reporter.set_show_fixtures(show_fixtures);

    bool const all_passed { suite.execute(reporter) };

    if (all_passed) {
      return EXIT_SUCCESS;
    } else {
      return EXIT_FAILURE;
    }
  }
}