tunit - Reference Guide  1.0.0
Modern c++17 unit testing framework on Windows, macOS, Linux, iOS and android.
ostream_unit_test.h
Go to the documentation of this file.
1 #pragma once
5 #include "unit_test.h"
6 #include <ostream>
7 
9 namespace tunit {
13  public:
16  explicit ostream_unit_test(std::ostream& os) noexcept : tunit::unit_test(std::make_unique<tunit::ostream_event_listener>(os)), os_(os) {}
17 
22  ostream_unit_test(std::ostream& os, char* argv[], int argc) : tunit::unit_test(std::make_unique<tunit::ostream_event_listener>(os), argv, argc), os_(os) {}
23 
27  std::ostream& ostream() {return os_;}
28 
29  int list_tests(const std::vector<std::string>& tests) override {
30  for (auto name : tests)
31  this->os_ << name << std::endl;
32  return this->unit_test::list_tests(tests);
33  }
34 
35  bool parse_arguments(const std::vector<std::string>& args) override {
36  for (auto arg : args)
37  if (arg == "--help") {
38  write_help();
39  return true;
40  }
41  return this->unit_test::parse_arguments(args);
42  }
43 
44  void write_help() {
45  this->os_ << "This program contains tests written using tunit. You can use the" << std::endl;
46  this->os_ << "following command line flags to control its behavior:" << std::endl;
47  this->os_ << std::endl;
48  this->os_ << "Test selection:" << std::endl;
49  this->os_ << __foreground_color(__console_color::green);
50  this->os_ << " --list_tests" << std::endl;
51  this->os_ << __reset_color();
52  this->os_ << " List the names of all tests instead of running them." << std::endl;
53  this->os_ << __foreground_color(__console_color::green);
54  this->os_ << " --filter_tests=";
55  this->os_ << __reset_color();
56  this->os_ << __foreground_color(__console_color::yellow);
57  this->os_ << "[PATTERN]" << std::endl;
58  this->os_ << __reset_color();
59  this->os_ << " Run only the tests whose name matches one of the pattern." << std::endl;
60  this->os_ << " '?' matches any single character; '*' matches any substring." << std::endl;
61  this->os_ << __foreground_color(__console_color::green);
62  this->os_ << " --also_run_ignored_tests" << std::endl;
63  this->os_ << __reset_color();
64  this->os_ << " Run all ignored tests too." << std::endl;
65  this->os_ << std::endl;
66  this->os_ << "Test execution:" << std::endl;
67  this->os_ << __foreground_color(__console_color::green);
68  this->os_ << " --repeat_tests=";
69  this->os_ << __reset_color();
70  this->os_ << __foreground_color(__console_color::yellow);
71  this->os_ << "[COUNT]" << std::endl;
72  this->os_ << __reset_color();
73  this->os_ << " Run the tests repeatedly; use a negative count to repeat forever." << std::endl;
74  this->os_ << __foreground_color(__console_color::green);
75  this->os_ << " --shuffle_tests" << std::endl;
76  this->os_ << __reset_color();
77  this->os_ << " Randomize tests' orders on every iteration." << std::endl;
78  this->os_ << __foreground_color(__console_color::green);
79  this->os_ << " --random_seed=";
80  this->os_ << __reset_color();
81  this->os_ << __foreground_color(__console_color::yellow);
82  this->os_ << "[NUMBER]" << std::endl;
83  this->os_ << __reset_color();
84  this->os_ << " Random number seed to use for shuffling test order. (any number" << std::endl;
85  this->os_ << " or 0 to use a seed based on the current time)." << std::endl;
86  this->os_ << std::endl;
87  this->os_ << "Test output:" << std::endl;
88  this->os_ << __foreground_color(__console_color::green);
89  this->os_ << " --output_color=";
90  this->os_ << __reset_color();
91  this->os_ << __foreground_color(__console_color::yellow);
92  this->os_ << "(true|false)" << std::endl;
93  this->os_ << __reset_color();
94  this->os_ << " Enable/disable colored output." << std::endl;
95  this->os_ << __foreground_color(__console_color::green);
96  this->os_ << " --show_duration=";
97  this->os_ << __reset_color();
98  this->os_ << __foreground_color(__console_color::yellow);
99  this->os_ << "(true|false)" << std::endl;
100  this->os_ << __reset_color();
101  this->os_ << " Enable/disable the elapsed time of each test display." << std::endl;
102  this->os_ << __foreground_color(__console_color::green);
103  this->os_ << " --output_xml=xml";
104  this->os_ << __reset_color();
105  this->os_ << __foreground_color(__console_color::yellow);
106  this->os_ << "[=PATH]" << std::endl;
107  this->os_ << __reset_color();
108  this->os_ << " Generate an xml report in the given directory or with the given file" << std::endl;
109  this->os_ << " name. PATH defaults to tests.xml." << std::endl;
110  this->os_ << std::endl;
111  }
112 
113  private:
114  std::ostream& os_;
115  };
116 }
ostream_unit_test(std::ostream &os) noexcept
Create a new console unit test with ostream specified.
Definition: ostream_unit_test.h:16
std::ostream & ostream()
Gets the ostream used by this instance.
Definition: ostream_unit_test.h:27
The template class.
Definition: unit_test.h:23
The ostream_unit_test class is a specialisation of event_listener class for writing events in std::os...
Definition: ostream_event_listener.h:16
ostream_unit_test(std::ostream &os, char *argv[], int argc)
Create a new console unit test with ostream specified, argv specified and argc specified.
Definition: ostream_unit_test.h:22
The ostream_unit_test class is ostream unit test interface.
Definition: ostream_unit_test.h:12
Contains tunit::unit_test class.
The tunit namespace contains a unit test library.
Definition: abort_error.h:8
Contains tunit::ostream_event_listener class.