From 57ec762adf7121283cd1ce964b190ebfe7adb0ff Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 17 Oct 2020 01:33:34 +0300 Subject: CommandLine: add MSDN test cases --- test/unit_tests/cmd_line.cpp | 81 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/unit_tests/cmd_line.cpp b/test/unit_tests/cmd_line.cpp index 99646ff..42f4a93 100644 --- a/test/unit_tests/cmd_line.cpp +++ b/test/unit_tests/cmd_line.cpp @@ -5,30 +5,89 @@ #include +// clang-format off +// The order matters for older Boost versions. #include +#include +#include +// clang-format on +#include #include +#include + +using namespace winapi; + +namespace std { + +ostream& operator<<(ostream& os, const vector& xs) { + os << "[\n"; + for (const auto& x : xs) { + os << x << '\n'; + } + os << "]"; + return os; +} + +} // namespace std + +BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector); + +namespace { + +// MSDN examples +// https://docs.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments?view=vs-2019 + +const std::vector msdn_string{ + R"(test.exe "abc" d e)", + R"(test.exe a\\\b d"e f"g h)", + R"(test.exe a\\\"b c d)", + R"(test.exe a\\\\"b c" d e)", +}; + +const std::vector> msdn_argv{ + {"test.exe", "abc", "d", "e"}, + {"test.exe", R"(a\\\b)", "de fg", "h"}, + {"test.exe", R"(a\"b)", "c", "d"}, + {"test.exe", R"(a\\b c)", "d", "e"}, +}; + +} // namespace BOOST_AUTO_TEST_SUITE(cmd_line_tests) BOOST_AUTO_TEST_CASE(query) { - const auto cmd_line = winapi::CommandLine::query(); - BOOST_TEST(!cmd_line.get_argv0().empty()); - BOOST_TEST_MESSAGE(cmd_line.get_argv0()); + const auto cmd_line = CommandLine::query(); + BOOST_TEST(!cmd_line.get_argv0().empty(), "argv[0]: " << cmd_line.get_argv0()); } -BOOST_AUTO_TEST_CASE(escape) { - wchar_t* argv[] = { - L"test.exe", - L"arg1 arg2", - LR"(path\to\file)", - LR"(path\to\dir\)", - LR"(weird\\argument)", +BOOST_AUTO_TEST_CASE(to_string) { + const std::vector argv{ + "test.exe", + "arg1 arg2", + R"(path\to\file)", + R"(path\to\dir\)", + R"(weird\\argument)", }; - const auto cmd_line = winapi::CommandLine::from_main(5, argv); + const CommandLine cmd_line{argv}; const auto expected = R"("test.exe" "arg1 arg2" "path\to\file" "path\to\dir\\" "weird\\argument")"; BOOST_TEST(cmd_line.to_string() == expected); } +BOOST_DATA_TEST_CASE(msdn_parse, + boost::unit_test::data::make(msdn_string) ^ msdn_argv, + input, + expected) { + const auto cmd_line = CommandLine::parse(input); + const auto actual = cmd_line.get_argv(); + BOOST_TEST(actual == expected, "actual: " << actual); +} + +BOOST_DATA_TEST_CASE(msdn_to_string, msdn_argv, argv) { + const CommandLine cmd_line{argv}; + const auto actual = CommandLine::parse(cmd_line.to_string()).get_argv(); + BOOST_TEST(actual == argv, "actual: " << actual); +} + BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3