Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for the argument parsing. #270

Merged
merged 5 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For example:
3. Plot the resulting...
4. See error by checking...

**Expected behavior**
**Expected behaviour**
What you expected to happen (if it's not already obvious from the context).

**Plots**
Expand Down
115 changes: 115 additions & 0 deletions tdms/tests/unit/test_argument_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* @file test_argument_parser.cpp
* @brief Tests of the argument parsing and file I/O.
*/
#include "argument_parser.h"

#include <algorithm>
#include <stdexcept>
#include <string>
#include <vector>

#include <catch2/catch_test_macros.hpp>

using std::string;
using std::vector;

/**
* @brief Test that the argument parser recovers the input arguments provided.
*/
TEST_CASE("Test parsing help") {
const char *input_args[] = {"tdms", "-h"};
auto args = ArgumentNamespace(2, const_cast<char **>(input_args));
REQUIRE(args.have_flag("-h"));
}

/** Drop the first and last component of an std::vector (needs size > 2!!). */
template<typename T>
vector<T> drop_first_and_last(const vector<T> &v) {
return vector<T>(v.begin() + 1, v.end() - 1);
}

/** Convert a vector of std::strings to an array of chars */
const char **vector_to_array(const vector<string> &vec) {
const char **arr = new const char *[vec.size()] {};
std::transform(vec.begin(), vec.end(), arr,
[](const std::string &s) { return s.c_str(); });
return arr;
}
// TODO: check with Will that this doesn't shadow the implementation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not super in-the-lingo; but if you're worried that the executable is meant to do this conversion itself, it doesn't.


/**
* @brief Test that the argument parser returns the correct filenames.
*/
TEST_CASE("Test parsing two filenames") {

const vector<string> input_args = {"tdms", "input_file.mat",
"output_file.mat"};
auto args = ArgumentNamespace(
input_args.size(), const_cast<char **>(vector_to_array(input_args)));

SECTION("Inputs") {
vector<string> expected_filenames = drop_first_and_last(input_args);
vector<string> parsed_filenames = args.input_filenames();
REQUIRE(expected_filenames == parsed_filenames);
}
SECTION("Output") {
string parsed = args.output_filename();
string expected = input_args[input_args.size() - 1];
REQUIRE(expected == parsed);
}
SECTION("Grid") {
REQUIRE(!args.has_grid_filename());
REQUIRE_THROWS_AS(args.grid_filename(), std::runtime_error);
}
}

/**
* @brief Test that the argument parser returns the correct filenames.
*/
TEST_CASE("Test parsing three filenames") {
vector<string> input_args = {"tdms", "input_file.mat", "grid_file.mat",
"output_file.mat"};
auto args = ArgumentNamespace(
input_args.size(), const_cast<char **>(vector_to_array(input_args)));

SECTION("Input") {
vector<string> expected_filenames = drop_first_and_last(input_args);
vector<string> parsed_filenames = args.input_filenames();
REQUIRE(expected_filenames == parsed_filenames);
}
SECTION("Output") {
string parsed = args.output_filename();
string expected = input_args[input_args.size() - 1];
REQUIRE(expected == parsed);
}
SECTION("Grid") {
string parsed = args.grid_filename();
string expected = input_args[input_args.size() - 2];
REQUIRE(args.has_grid_filename());
REQUIRE(expected == parsed);
}
}

/**
* @brief Test that the argument correctly errors with the wrong number of file
* names.
*/
TEST_CASE("Test wrong number of file names") {
SECTION("Zero") {
const char *input_args[] = {"tdms"};
auto args = ArgumentNamespace(1, const_cast<char **>(input_args));
REQUIRE(!args.have_correct_number_of_filenames());
}
SECTION("One") {
const char *input_args[] = {"tdms", "input_file.mat"};
auto args = ArgumentNamespace(2, const_cast<char **>(input_args));
REQUIRE(!args.have_correct_number_of_filenames());
}
SECTION("Four") {
const char *input_args[] = {"tdms", "input_file.mat", "grid_file.mat",
"output_file.mat", "fourth_file.mat"};
auto args = ArgumentNamespace(5, const_cast<char **>(input_args));
REQUIRE(!args.have_correct_number_of_filenames());
}
}
19 changes: 0 additions & 19 deletions tdms/tests/unit/test_openandorder.cpp

This file was deleted.