|
micro-test-plus 6.0.0
µTest++ Testing Framework
|
The configuration of a test session, and its command-line parser. More...
#include "micro-os-plus/micro-test-plus.h"
Public Member Functions | |
| runner_options (const runner_options &other)=default | |
| Copy constructor. | |
| runner_options (int argc, char *argv[]) | |
| Constructs the options by parsing the command line. | |
| runner_options (runner_options &&other)=default | |
| Move constructor. | |
| runner_options (void)=default | |
| Constructs the default options. | |
| ~runner_options ()=default | |
| Destructor. | |
| std::span< const std::string > | application_arguments (void) const noexcept |
| Returns the arguments left to the application. | |
| const std::vector< std::string > & | arguments (void) const noexcept |
| Returns the complete command line, including argv[0], if any. | |
| const std::vector< std::string > & | errors (void) const noexcept |
| Returns the errors found while parsing the command line. | |
| bool | has_errors (void) const noexcept |
| Checks whether errors were found while parsing the command line. | |
| bool | has_unknown_option (void) const noexcept |
| Checks whether an unknown option was found while parsing the command line. | |
| runner_options & | operator= (const runner_options &other) |
| Copy assignment operator. | |
| runner_options & | operator= (runner_options &&other)=default |
| Move assignment operator. | |
Public Attributes | |
| colour_mode | colour { colour_mode::automatic } |
| Whether the console output uses colours. | |
| std::vector< std::string > | filter_patterns {} |
| The patterns that select the suites and the test cases to run, one per nesting level. | |
| bool | help { false } |
| Whether to display the usage and the list of the options, instead of running the tests. | |
| bool | list_tests { false } |
| Whether the test cases are listed instead of run. | |
| std::string | output_file_path {} |
| Path of the optional output file. | |
| reporter_kind | selected_reporter { reporter_kind::tap } |
| The reporter that displays the results. | |
| micro_test_plus::verbosity | verbosity_level |
| The level of detail of the console output. | |
Private Member Functions | |
| void | add_error (std::string_view before, std::string_view value={}, std::string_view after={}) |
| Records a command-line error. | |
Private Attributes | |
| std::size_t | application_begin_ { 0 } |
| The index in arguments_ of the first argument left to the application. | |
| std::vector< std::string > | arguments_ {} |
| The complete command line, including argv[0], if any. | |
| std::vector< std::string > | errors_ {} |
| The errors found while parsing the command line. | |
| bool | unknown_option_ { false } |
| Whether an unknown option was found. | |
The options are usually constructed from the command line, by runner::initialise (argc, argv), which creates the selected reporter and starts the session:
The applications that do not have a command line, such as the embedded ones without semihosting arguments, can define the options directly, starting from the defaults:
The applications can also parse the command line themselves, and adjust the result before passing it to the runner:
The configuration members are public, since any combination of their values is valid; the command line and the results of the parsing (whether errors were found, whether an unknown option was found, and where the application arguments start) are private, since they depend on one another, and are read with the accessors.
The options own their strings (the command line, the path of the output file, and the filter patterns), so they do not depend on the lifetime of the caller's strings; the runner keeps its own copy of them.
Definition at line 217 of file runner-options.h.
|
default |
The TAP reporter, the normal verbosity, no output file, automatic colours, no filter, and no command line; the test cases are run, not listed.
Referenced by runner_options(), runner_options(), operator=(), and operator=().
|
explicit |
Starts from the defaults, and parses the µTest++ options (--reporter, --silent, --quiet, --verbose, --output-file, --colour, --color, --use-colour, --use-color, --filter, --list-tests, --list, --help, and -h). The parsing ends at the first argument that does not start with a dash (an application argument), or at the first -- alone; that argument, and all the following ones, are left to the application, and are available from application_arguments(). An option argument given separately (--reporter human) belongs to its option and does not end the parsing. Before that point, the arguments starting with a dash that are not µTest++ options are reported as unknown options; an application that receives such arguments first must precede them with --.
A missing option argument, an unknown reporter name, an unknown colour mode, a colour mode given separately after --colour or --color, or an unknown option, is recorded in errors(), and makes has_errors() true; the constructor writes nothing. The parsing continues, to record all the errors, but runner::initialise() then runs no test: it writes the errors on stderr, on the console, and in the output file, if any, followed, for an unknown option, by a line suggesting --help, and run_suites() returns 2.
The argv[] strings are copied, so they need not outlive the options.
| argc | The argument count from main(). |
| argv | The argument vector from main(). |
Scans the arguments after argv[0], in order, for:
When an option is repeated, the last one takes effect, except --filter, whose patterns are all kept, in order, one per nesting level; --colour, --color, --use-colour, and --use-color set the same option. An option argument given separately is the next argument, whatever its content; --colour and --color accept their value only after =.
The parsing ends at the first argument that does not start with a dash, or at the first -- alone; the arguments from there on are left to the application, and are not interpreted, so they can be options of the application. The -- itself is not an application argument. An argument that starts with a dash, before that point, and is not a µTest++ option, is reported as an unknown option. The whole command line is copied into arguments_, to be listed by the reporter; the option values are copied from there.
A missing option argument, an unknown reporter name, an unknown colour mode, a colour mode given separately after --colour or --color, or an unknown option (any other argument that starts with a dash, including a single one), is recorded by add_error(), and the option is ignored; the parsing continues with the next argument, to record all the errors. Nothing is written by the constructor; the runner then writes the errors and refuses to run the tests.
Definition at line 119 of file runner-options.cpp.
References add_error(), micro_os_plus::micro_test_plus::always, application_begin_, arguments_, micro_os_plus::micro_test_plus::automatic, colour, filter_patterns, help, micro_os_plus::micro_test_plus::human, list_tests, micro_os_plus::micro_test_plus::never, output_file_path, micro_os_plus::micro_test_plus::quiet, selected_reporter, micro_os_plus::micro_test_plus::silent, micro_os_plus::micro_test_plus::tap, unknown_option_, micro_os_plus::micro_test_plus::verbose, and verbosity_level.
|
default |
Copies all the members, including the strings and the results of the parsing, so the copy initialises a runner exactly as the original does; the copy is independent of the original.
| other | The instance to copy. |
References runner_options().
|
default |
Moves the strings and the vectors, and copies the other members; other is left valid, with unspecified strings.
| other | The instance to move from. |
References runner_options().
|
default |
Releases the owned strings.
|
private |
Appends the text to errors_; nothing is written, so that parsing the command line has no output.
| before | The start of the message, without the error: prefix. |
| value | The variable part, usually an option or a value. |
| after | The end of the message, without the newline. |
The message is only kept in errors_, so that the constructor has no output; the runner writes it on stderr and on the console, since the reporter does not exist yet.
Definition at line 376 of file runner-options.cpp.
References errors_.
Referenced by runner_options().
|
inlinenodiscardnoexcept |
The arguments that follow the µTest++ options on the command line: the first argument that does not start with a dash, if the parsing ended there, and all the following ones, unchanged, or, if the parsing ended at a -- alone, the arguments after it, which may start with --. The -- itself and argv[0] are not included. The span refers to the command line, so it is valid as long as the options are not modified or destroyed.
Returns the part of arguments_ that starts at application_begin_, or an empty span if there are none, including when the options were not parsed from a command line.
Definition at line 117 of file runner-options-inlines.h.
References application_begin_, and arguments_.
|
inlinenodiscardnoexcept |
A copy of the argv[] strings of main(), set when the options are parsed from the command line; empty for the default options. When not empty, the reporters list it at the beginning of the report, and runner::add_top_suite() derives the default name of the top suite from its first element. The arguments left to the application are returned by application_arguments().
The command line is read-only, since application_arguments() depends on the position where the parsing ended, which would be stale if the strings were changed afterwards.
Returns the member arguments_, which is filled only by the parsing constructor.
Definition at line 105 of file runner-options-inlines.h.
References arguments_.
Referenced by micro_os_plus::micro_test_plus::runner::add_top_suite(), micro_os_plus::micro_test_plus::detail::reporter_human::begin_session(), micro_os_plus::micro_test_plus::detail::reporter_tap::begin_session(), and micro_os_plus::micro_test_plus::runner::initialise().
|
inlinenodiscardnoexcept |
Each message is the text of the error, without the error: prefix and the newline, in the order of the arguments. The runner writes them on stderr and on the console when it refuses to run the tests.
Returns the errors_ member.
Definition at line 94 of file runner-options-inlines.h.
References errors_.
Referenced by micro_os_plus::micro_test_plus::runner::initialise().
|
inlinenodiscardnoexcept |
The constructor only records the errors; when set, the runner runs no test and runner::run_suites() returns 2. Never set for the default options.
Returns whether the errors_ member is not empty.
Definition at line 74 of file runner-options-inlines.h.
References errors_.
Referenced by micro_os_plus::micro_test_plus::runner::initialise().
|
inlinenodiscardnoexcept |
Set together with the corresponding error; the runner then adds a line suggesting --help to the errors it writes on the console.
Returns the unknown_option_ member.
Definition at line 84 of file runner-options-inlines.h.
References unknown_option_.
Referenced by micro_os_plus::micro_test_plus::runner::initialise().
|
inline |
Replaces all the members with copies of those of other.
Not defaulted: it copies other with the copy constructor, and moves the copy into this instance, since the copy assignment of the vectors of strings uses std::copy, which triggers -Waggregate-return in the standard library headers of GCC on some targets. If the copy fails, this instance is not changed.
| other | The instance to copy. |
Copies other with the copy constructor, and moves the copy into this instance; self-assignment is safe.
Definition at line 62 of file runner-options-inlines.h.
References runner_options().
|
default |
Moves the strings and the vectors, and copies the other members.
| other | The instance to move from. |
References runner_options().
|
private |
Set by the parsing constructor, where the parsing of the µTest++ options ends; used by application_arguments().
Definition at line 629 of file runner-options.h.
Referenced by runner_options(), and application_arguments().
|
private |
Filled by the parsing constructor with a copy of the argv[] strings; returned by arguments(), and used by application_arguments().
Definition at line 619 of file runner-options.h.
Referenced by runner_options(), application_arguments(), and arguments().
| colour_mode micro_os_plus::micro_test_plus::runner_options::colour { colour_mode::automatic } |
Set by --colour[=<when>], or by its American spelling --color[=<when>], as in the GNU tools, where <when> is always, never, or auto, or one of their GNU synonyms (yes and force, no and none, tty and if-tty), and --colour alone means always; the value is accepted only after =, and a mode word in the next argument, such as --colour never, is reported as an error, since it was probably meant as the value (an application argument of that kind can follow --). The alternative --use-colour=<mode> or --use-colour <mode>, also spelled --use-color, as in Catch2 and Boost UT, takes yes, no, or auto, and also accepts the other values of --colour. Used only by the human-readable reporter.
Definition at line 389 of file runner-options.h.
Referenced by micro_os_plus::micro_test_plus::detail::reporter_human::reporter_human(), and runner_options().
|
private |
Appended by add_error(); returned by errors(), and tested by has_errors().
Definition at line 600 of file runner-options.h.
Referenced by add_error(), errors(), and has_errors().
| std::vector<std::string> micro_os_plus::micro_test_plus::runner_options::filter_patterns {} |
Each --filter=<pattern> or --filter <pattern> option adds one pattern; unlike the other options, --filter may be repeated. As with --grep in node-tap, the first pattern applies to the suite names, the second to the names of the test cases, and each further pattern to the names of the test cases nested one level deeper; the levels without a pattern are not filtered.
Within a pattern, | separates alternatives; a name matches when it matches any of them, as defined by utility::is_match(): * matches any sequence of characters, ? matches any single character, the match is case-sensitive, and the alternative must cover the whole name.
The suites and the test cases that do not match are reported as skipped, and their bodies are not called. The top suite is never skipped as a whole, since its body is main(), but when its name does not match the first pattern, all its test cases are skipped.
When the list is empty (the default), everything runs.
Definition at line 418 of file runner-options.h.
Referenced by runner_options(), micro_os_plus::micro_test_plus::runner::initialise(), micro_os_plus::micro_test_plus::runner::is_selected(), and micro_os_plus::micro_test_plus::runner::run_suites().
| bool micro_os_plus::micro_test_plus::runner_options::help { false } |
Set by the --help option, or by its short form -h, which is the only short option. The runner then writes, on the console, the usage and the list of the options, runs no test, creates no output file, and run_suites() returns 0, or 2 after an option error. The text is written also with the silent verbosity, since it was requested.
Definition at line 471 of file runner-options.h.
Referenced by runner_options(), micro_os_plus::micro_test_plus::detail::runnable_base::accepts_test_case(), micro_os_plus::micro_test_plus::runner::add_top_suite(), micro_os_plus::micro_test_plus::detail::runnable_base::after_test_case_create(), and micro_os_plus::micro_test_plus::runner::initialise().
| bool micro_os_plus::micro_test_plus::runner_options::list_tests { false } |
Set by the --list-tests option, or by its short form --list. When set, the suite bodies run, as usual, to create their test cases, but the bodies of the test cases do not, so no check is evaluated. The selected test cases (see filter_patterns) are listed under the names of their suites, as Catch2 lists them:
The header is Matching test cases: when there are filter patterns, and the suites without selected test cases are not listed. The nested test cases are not listed, since they are created by the bodies of their parents, which do not run. The list is made of plain lines, the same with both reporters and with all the verbosity levels, and is written to the console and to the output file, if any; no report is written, and run_suites() returns 0, 1 if a call was rejected or an exception escaped a suite body, which makes the list incomplete (the error is written in the list, as an error: line, or a # error: comment with the TAP reporter), or 2 after an option error, or if the filters selected no test case.
Definition at line 455 of file runner-options.h.
Referenced by runner_options(), micro_os_plus::micro_test_plus::detail::runnable_base::accepts_test_case(), micro_os_plus::micro_test_plus::runner::add_top_suite(), micro_os_plus::micro_test_plus::detail::runnable_base::after_test_case_create(), micro_os_plus::micro_test_plus::runner::initialise(), micro_os_plus::micro_test_plus::runner::reject_call(), micro_os_plus::micro_test_plus::runner::run_child_suites(), and micro_os_plus::micro_test_plus::runner::run_suites().
| std::string micro_os_plus::micro_test_plus::runner_options::output_file_path {} |
Set by --output-file=<path> or --output-file <path>. When it is empty (the default), no file is written. The reporter opens the file when it is created by initialise(); if the file cannot be opened, the error is reported on stderr and on the console, no test is run, and runner::run_suites() returns 2.
Definition at line 368 of file runner-options.h.
Referenced by micro_os_plus::micro_test_plus::detail::reporter::reporter(), runner_options(), and micro_os_plus::micro_test_plus::runner::initialise().
| reporter_kind micro_os_plus::micro_test_plus::runner_options::selected_reporter { reporter_kind::tap } |
Set by --reporter=<name> or --reporter <name>, where the name is tap or human.
Definition at line 345 of file runner-options.h.
Referenced by runner_options(), and micro_os_plus::micro_test_plus::runner::initialise().
|
private |
Set by the parsing constructor, with the error; tested by has_unknown_option().
Definition at line 609 of file runner-options.h.
Referenced by runner_options(), and has_unknown_option().
| micro_test_plus::verbosity micro_os_plus::micro_test_plus::runner_options::verbosity_level |
Set by --silent, --quiet, or --verbose; when several are given, the last one takes effect.
Definition at line 354 of file runner-options.h.
Referenced by runner_options(), micro_os_plus::micro_test_plus::runner::initialise(), micro_os_plus::micro_test_plus::runner::reject_call(), and micro_os_plus::micro_test_plus::runner::run_suites().