48 void printHelpMessage(
49 const char* binary_name_) {
50 std::cout <<
"Usage: " << binary_name_ <<
" [options]" << std::endl;
51 std::cout << std::endl;
52 std::cout <<
"A collection of automatic tests run by the OTest2 framework" << std::endl;
53 std::cout << std::endl;
54 std::cout <<
"Options (all options are optional):" << std::endl;
55 std::cout <<
" -h --help Print this message." << std::endl;
56 std::cout <<
" --disable-console Disable reporting into the console." << std::endl;
57 std::cout <<
" -v --verbose Make the console reporter verbose. This option" << std::endl;
58 std::cout <<
" turns on printing of results of passed tests." << std::endl;
59 std::cout <<
" -j file --junit=file Print report into a file in the JUnit XML format." << std::endl;
60 std::cout <<
" This option may be used several times. Several" << std::endl;
61 std::cout <<
" files will then be written." << std::endl;
62 std::cout <<
" -r glob --restrictive=glob Run just test object which match the tag glob." << std::endl;
63 std::cout <<
" The default value runs all untagged objects." << std::endl;
64 std::cout <<
" -m file --regression=file Path of the regression file. The default value" << std::endl;
65 std::cout <<
" is 'regression.ot2tm' (stored in the working" << std::endl;
66 std::cout <<
" directory)." << std::endl;
67 std::cout <<
" -t name --test=name Name of the test how it's reported. The default" << std::endl;
68 std::cout <<
" value is the name of the test's binary." << std::endl;
69 std::cout << std::endl;
72 std::string createDefaultTestName(
73 const char* binname_) {
76 std::string test_name_(binname_);
77 auto last_slash_(test_name_.find_last_of(
'/'));
78 if(last_slash_ != std::string::npos)
79 test_name_.erase(0, last_slash_ + 1);
85 struct DfltEnvironment::Impl {
87 TimeSourceSys time_source;
88 ExcCatcherOrdinary exc_catcher_ordinary;
89 ExcCatcher* exc_catcher;
90 std::vector<std::unique_ptr<Reporter>> reporters;
91 ReporterTee reporter_root;
92 std::unique_ptr<RunnerFilter> filter;
93 TestMarkFactory test_mark_factory;
94 std::unique_ptr<TestMarkStorage> test_mark_storage;
96 std::unique_ptr<Runner> runner;
99 bool console_reporter;
100 bool console_verbose;
101 std::string regression_file;
102 std::string test_name;
105 const std::string& test_name_);
108 DfltEnvironment::Impl::Impl(
109 const std::string& test_name_) :
111 exc_catcher_ordinary(),
112 exc_catcher(nullptr),
120 console_reporter(true),
121 console_verbose(false),
122 regression_file(
"regression.ot2tm"),
123 test_name(test_name_) {
128 const std::string& testname_) :
129 pimpl(new Impl(testname_)) {
136 pimpl(new Impl(createDefaultTestName(argv_[0]))) {
139 DISABLE_CONSOLE_REPORTER = 1000,
147 struct option long_options_[] = {
148 {
"disable-console", 0,
nullptr, DISABLE_CONSOLE_REPORTER},
149 {
"verbose", 0,
nullptr, VERBOSE_CONSOLE},
150 {
"junit", 1,
nullptr, JUNIT_REPORTER},
151 {
"restrictive", 1,
nullptr, RESTRICTIVE_RUN},
152 {
"regression", 1,
nullptr, REGRESSION_FILE},
153 {
"test", 1,
nullptr, TEST_NAME},
154 {
"help", 0,
nullptr, HELP},
155 {
nullptr, 0,
nullptr, 0},
158 while((opt_ = getopt_long(argc_, argv_,
"vj:r:m:t:h", long_options_,
nullptr)) >= 0) {
160 case DISABLE_CONSOLE_REPORTER:
161 pimpl->console_reporter =
false;
164 case VERBOSE_CONSOLE:
165 pimpl->console_verbose =
true;
169 pimpl->reporters.emplace_back(
new ReporterJUnit(optarg,
false));
170 pimpl->reporter_root.appendReporter(pimpl->reporters.back().get());
173 case RESTRICTIVE_RUN:
175 pimpl->filter = ::OTest2::make_unique<RunnerFilterTags>(optarg);
178 std::cout <<
"invalid tag expression: " << exc_.
reason() << std::endl;
183 case REGRESSION_FILE:
184 pimpl->regression_file = optarg;
188 pimpl->test_name = optarg;
192 printHelpMessage(argv_[0]);
196 printHelpMessage(argv_[0]);
210 assert(reporter_ !=
nullptr);
212 pimpl->reporter_root.appendReporter(reporter_);
213 pimpl->console_reporter =
false;
218 assert(catcher_ !=
nullptr);
220 pimpl->exc_catcher = catcher_;
224 return pimpl->user_data;
228 if(pimpl->runner ==
nullptr) {
230 if(pimpl->exc_catcher ==
nullptr)
231 pimpl->exc_catcher = &pimpl->exc_catcher_ordinary;
234 if(pimpl->console_reporter) {
236 &std::cout, pimpl->console_verbose,
false));
237 pimpl->reporter_root.appendReporter(pimpl->reporters.back().get());
241 if(pimpl->filter ==
nullptr)
242 pimpl->filter = ::OTest2::make_unique<RunnerFilterUntagged>();
245 pimpl->test_mark_storage.reset(
246 new TestMarkStorage(&pimpl->test_mark_factory, pimpl->regression_file));
256 &pimpl->reporter_root,
257 &pimpl->test_mark_factory,
258 pimpl->test_mark_storage.get(),
262 return *pimpl->runner;