Skip to content

Commit

Permalink
Add tests for optional<T> support
Browse files Browse the repository at this point in the history
  • Loading branch information
Miro Palmu committed Jul 29, 2023
1 parent 6015294 commit e209949
Showing 1 changed file with 120 additions and 6 deletions.
126 changes: 120 additions & 6 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ int main(int argc, char* argv[])
int number = 123;
bool boolean = false;

std::optional<std::string> input_opt{};
std::optional<std::string> study_opt{};
std::optional<int> number_opt{};

argz::options opts{
{ { "input", 'i' }, input, "the input file"},
{ { "study", 's' }, study, "a study file"},
{ { "number" }, number, "input an int"},
{ { "boolean" }, boolean, "a boolean" },
{ { "input_opt" }, input_opt, "the input file"},
{ { "study_opt" }, study_opt, "a study file"},
{ { "number_opt" }, number_opt, "input an int"},
};

try {
Expand Down Expand Up @@ -127,7 +134,7 @@ int main(int argc, char* argv[])
};

test("test0") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
parse_string(R"(program.exe -i some_file --study study_file --boolean --number 12345)");

expect(input == "some_file");
Expand All @@ -139,7 +146,7 @@ int main(int argc, char* argv[])
};

test("test1") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
parse_string(R"(program.exe -i some/path --study s --boolean --number 12)");

expect(input == "some/path");
Expand All @@ -153,7 +160,7 @@ int main(int argc, char* argv[])
};

test("test3") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
parse_string(R"(program.exe -i "some/path" --study "s" --boolean --number 22 )");
expect(input == "some/path");
expect(study == "s");
Expand All @@ -162,7 +169,7 @@ int main(int argc, char* argv[])
};

test("test4") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
parse_string(R"(program.exe -i some/path --study s --boolean -- )");
expect(input == "some/path");
expect(study == "s");
Expand All @@ -175,7 +182,7 @@ int main(int argc, char* argv[])
};

test("test6") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
expect(nothrow([&] {parse_string(R"(program.exe -i some/path --study s --boolean -- )"); }));
expect(input == "some/path");
expect(study == "s");
Expand All @@ -184,7 +191,7 @@ int main(int argc, char* argv[])
};

test("test7") = [&] {
boolean = false; // Reset not to be right on default
boolean = false; // Reset not to be right on default
expect(throws([&] {parse_string(R"(program.exe -i some/path --study s --boolean 27 --number true)"); }));
};

Expand All @@ -202,5 +209,112 @@ int main(int argc, char* argv[])
expect(input == "./../some quoted path.txt") << "actual: " << input;
};

test("opt_test0") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

parse_string(R"(program.exe --input_opt some_file --study_opt study_file --boolean --number_opt 12345)");

expect(input_opt.value() == "some_file");
expect(study_opt.value() == "study_file");
expect(number_opt.value() == 12345);
expect(boolean == true);


};

test("opt_test1") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

parse_string(R"(program.exe --input_opt some/path --study_opt s --boolean --number_opt 12)");

expect(input_opt.value() == "some/path");
expect(study_opt.value() == "s");
expect(number_opt.value() == 12);
expect(boolean == true);
};

test("opt_test2") = [&] {
parse_string(R"(program.exe --input_opt)");
};

test("opt_test3") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

parse_string(R"(program.exe --input_opt "some/path" --study_opt "s" --boolean --number_opt 22 )");
expect(input_opt.value() == "some/path");
expect(study_opt.value() == "s");
expect(number_opt.value() == 22);
expect(boolean == true);
};

test("opt_test4") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

parse_string(R"(program.exe --input_opt some/path --study_opt s --boolean -- )");
expect(input_opt.value() == "some/path");
expect(study_opt == "s");
expect(!number_opt.has_value());
expect(boolean == true);
};

test("opt_test5") = [&] {
expect(nothrow([&] {parse_string(R"(program.exe -h)"); }));
};

test("opt_test6") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

expect(nothrow([&] {parse_string(R"(program.exe --input_opt some/path --study_opt s --boolean -- )"); }));

expect(input_opt.value() == "some/path");
expect(study_opt.value() == "s");
expect(!number_opt.has_value());
expect(boolean == true);

};

test("opt_test7") = [&] {
expect(throws([&] {parse_string(R"(program.exe --input_opt some/path --study_opt s --boolean 27 --number_opt true)"); }));
};

test("opt_test8") = [&] {
boolean = false; // Reset not to be right on default
input_opt = {};
study_opt = {};
number_opt = {};

expect(nothrow([&] {parse_string(R"(program.exe - )"); }));

expect(!input_opt.has_value());
expect(!study_opt.has_value());
expect(!number_opt.has_value());
expect(boolean == false);
};

test("opt_test-dashes") = [&] {
parse_string(R"(program.exe --input_opt ./some-path-with-dashes.txt )");
expect(input_opt.value() == std::string("./some-path-with-dashes.txt")) << "actual: " << input_opt.value_or("Empty");
};

test("opt_quoted_path") = [&] {
parse_string(R"(program.exe --input_opt "./../some quoted path.txt" )");
expect(input_opt.value() == "./../some quoted path.txt") << "actual: " << input_opt.value_or("Empty");
};

return 0;
}

0 comments on commit e209949

Please sign in to comment.