Skip to content

cli_test.cpp

file tests/cli/cli_test.cpp ยท Test suite

Tests of nmeasim, the command-line tool (src/cli/main.cpp), run as a separate process.

Source tests/cli/cli_test.cpp

Covers the exit status of --help and --version, the status 2 of every command-line error, whether CLI11 or the tool itself finds it, the strict reading of --udp, --serial, --destination, --duration and --speed, that valid values are still accepted, that --encoding, --tag-block and --tag-source apply to a profile's outputs, that --track and --replay keep the profile's loop and timestamp settings unless a flag is given, and that a subnet broadcast address selects UDP broadcast. Every run is short: it either fails while parsing or streams for a fraction of a second to standard output, a temporary log file or a UDP port (on the loopback interface, or broadcast on a local subnet). The file reads the fixtures tracks/timestamped.gpx and logs/plain.nmea from tests/fixtures and writes profiles into temporary directories.

Test cases

7 test cases, in source order.

Test case Tags
help and version exit with status 0 [cli]
every command-line error exits with status 2 [cli]
malformed and non-finite values exit with status 2 [cli]
well-formed values are still accepted [cli]
encoding and TAG block options apply to the outputs of a profile [cli]
track and replay options keep the profile's loop and timestamp settings unless given [cli]
broadcast addresses select UDP broadcast [cli]

Types

Type Description
CliRun What one run of the command-line tool left behind.

Summary

File-local functions

Name Description
run_cli() Runs the nmeasim that this build produced and waits for it to end.
fixture() Returns the absolute path of a file in the test fixtures directory.
quick_run() Returns run with the options of a short, quiet run that writes to standard output.
write_profile() Writes the built-in default profile, changed by a function, to a file.
set_simulation_key() Sets one key of a nested object of a profile.
source_length() Reads the length of the source from the status line of a run that was not quiet.
subnet_broadcast_address() Returns the subnet broadcast address of a local interface that can broadcast.

File-local functions

run_cli()

CliRun run_cli(const QStringList& arguments, int timeout_ms = 30000)

Runs the nmeasim that this build produced and waits for it to end.

The executable is the one named by the NMEASIM_CLI_PATH compile definition, which tests/CMakeLists.txt sets to the location of the nmeasim_cli target. Standard input is closed at once. A run that does not end within the timeout is killed.

Parameters

Name Type Description
arguments const QStringList& The command-line arguments, without the program name.
timeout_ms int How long to wait for the process to end, in milliseconds.

Returns CliRun: The exit status and the captured output.

Declared in tests/cli/cli_test.cpp:58

fixture()

QString fixture(const char* relative)

Returns the absolute path of a file in the test fixtures directory.

Parameters

Name Type Description
relative const char* Path below tests/fixtures, for example "logs/plain.nmea".

Returns QString: The path, built from the NMEASIM_FIXTURES_DIR compile definition.

Declared in tests/cli/cli_test.cpp:81

quick_run()

QStringList quick_run(const QStringList& extra)

Returns run with the options of a short, quiet run that writes to standard output.

Parameters

Name Type Description
extra const QStringList& Options appended after the common ones; they must not repeat --duration, which CLI11 refuses to take twice.

Returns QStringList: run --stdout --quiet --duration 0.3 followed by extra.

Declared in tests/cli/cli_test.cpp:90

write_profile()

QString write_profile(
    const QTemporaryDir& directory,
    const std::function<void(QJsonObject& )>& change)

Writes the built-in default profile, changed by a function, to a file.

The profile is taken from nmeasim profile show, so it is the one the tool itself uses. Fails the running test case when the tool does not print it.

Parameters

Name Type Description
directory const QTemporaryDir& Directory the file is written to, as profile.json.
change const std::function<void(QJsonObject& )>& Function that edits the profile's JSON object before it is written.

Returns QString: The path of the written file.

Declared in tests/cli/cli_test.cpp:104

set_simulation_key()

void set_simulation_key(
    QJsonObject& profile,
    const QString& section,
    const QString& key,
    const QJsonValue& value)

Sets one key of a nested object of a profile.

Parameters

Name Type Description
profile QJsonObject& The profile's JSON object.
section const QString& Key of the object below simulation, such as track.
key const QString& Key inside that object, such as loop.
value const QJsonValue& The new value.

Declared in tests/cli/cli_test.cpp:124

source_length()

std::string source_length(const QString& err)

Reads the length of the source from the status line of a run that was not quiet.

Parameters

Name Type Description
err const QString& Standard error of the run, which holds a line such as running profile 'Default' following track.gpx (12.3 s) with a 100 ms tick.

Returns std::string: The length in seconds as printed, such as 12.3, as a standard string so that Catch2 can print it; empty when there is none.

Declared in tests/cli/cli_test.cpp:139

subnet_broadcast_address()

QHostAddress subnet_broadcast_address()

Returns the subnet broadcast address of a local interface that can broadcast.

Returns QHostAddress: The first IPv4 subnet broadcast address, other than 255.255.255.255, of an interface that is up, running, able to broadcast and not the loopback; null when there is none.

Declared in tests/cli/cli_test.cpp:149