BLT/src/blt/parse/argparse_v2.cpp

146 lines
6.7 KiB
C++
Raw Normal View History

2025-02-12 02:54:22 -05:00
/*
* <Short Description>
* Copyright (C) 2025 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <blt/parse/argparse_v2.h>
#include <blt/std/assert.h>
namespace blt::argparse
{
namespace detail
{
2025-02-12 19:42:50 -05:00
2025-02-12 02:54:22 -05:00
}
namespace detail
{
// Unit Tests for class argument_string_t
// Test Case 1: Ensure the constructor handles flags correctly
2025-02-13 01:53:21 -05:00
void test_argument_string_t_flag_basic(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("-f", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag.");
2025-02-12 15:43:54 -05:00
BLT_ASSERT(arg.value() == "f" && "Flag value should match the input string.");
2025-02-12 02:54:22 -05:00
}
// Test Case 2: Ensure the constructor handles long flags correctly
2025-02-13 01:53:21 -05:00
void test_argument_string_t_long_flag(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("--file", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag.");
2025-02-12 15:43:54 -05:00
BLT_ASSERT(arg.value() == "file" && "Long flag value should match the input string.");
2025-02-12 02:54:22 -05:00
}
// Test Case 3: Ensure positional arguments are correctly identified
2025-02-13 01:53:21 -05:00
void test_argument_string_t_positional_argument(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("filename.txt", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(!arg.is_flag() && "Expected argument to be identified as positional.");
BLT_ASSERT(arg.value() == "filename.txt" && "Positional argument value should match the input string.");
}
// Test Case 5: Handle an empty string
2025-02-13 01:53:21 -05:00
void test_argument_string_t_empty_input(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(!arg.is_flag() && "Expected an empty input to be treated as positional, not a flag.");
BLT_ASSERT(arg.value().empty() && "Empty input should have an empty value.");
}
// Test Case 6: Handle edge case of a single hyphen (`-`) which might be ambiguous
2025-02-13 01:53:21 -05:00
void test_argument_string_t_single_hyphen(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("-", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(arg.is_flag() && "Expected single hyphen (`-`) to be treated as a flag.");
2025-02-12 15:43:54 -05:00
BLT_ASSERT(arg.value().empty() && "Single hyphen flag should have empty value.");
BLT_ASSERT(arg.get_flag() == "-" && "Single hyphen flag should match the input string.");
2025-02-12 02:54:22 -05:00
}
// Test Case 8: Handle arguments with prefix only (like "--")
2025-02-13 01:53:21 -05:00
void test_argument_string_t_double_hyphen(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("--", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(arg.is_flag() && "Double hyphen ('--') should be treated as a flag.");
2025-02-12 15:43:54 -05:00
BLT_ASSERT(arg.value().empty() && "Double hyphen flag should have empty value.");
BLT_ASSERT(arg.get_flag() == "--" && "Double hyphen value should match the input string.");
2025-02-12 02:54:22 -05:00
}
// Test Case 9: Validate edge case of an argument with spaces
2025-02-13 01:53:21 -05:00
void test_argument_string_t_with_spaces(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg(" ", prefixes);
2025-02-12 19:42:50 -05:00
BLT_ASSERT(!arg.is_flag() && "Arguments with spaces should not be treated as flags.");
BLT_ASSERT(arg.value() == " " && "Arguments with spaces should match the input string.");
2025-02-12 02:54:22 -05:00
}
// Test Case 10: Validate arguments with numeric characters
2025-02-13 01:53:21 -05:00
void test_argument_string_t_numeric_flag(const hashset_t<char>& prefixes)
2025-02-12 02:54:22 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("-123", prefixes);
2025-02-12 02:54:22 -05:00
BLT_ASSERT(arg.is_flag() && "Numeric flags should still be treated as flags.");
2025-02-12 15:43:54 -05:00
BLT_ASSERT(arg.value() == "123" && "Numeric flag value should match the input string.");
2025-02-12 02:54:22 -05:00
}
2025-02-12 19:42:50 -05:00
// Test Case 11: Ensure the constructor handles '+' flag correctly
2025-02-13 01:53:21 -05:00
void test_argument_string_t_plus_flag_basic(const hashset_t<char>& prefixes)
2025-02-12 19:42:50 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("+f", prefixes);
2025-02-12 19:42:50 -05:00
BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag.");
BLT_ASSERT(arg.value() == "f" && "Plus flag value should match the input string.");
}
// Test Case 13: Handle edge case of a single plus (`+`) which might be ambiguous
2025-02-13 01:53:21 -05:00
void test_argument_string_t_single_plus(const hashset_t<char>& prefixes)
2025-02-12 19:42:50 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("+", prefixes);
2025-02-12 19:42:50 -05:00
BLT_ASSERT(arg.is_flag() && "Expected single plus (`+`) to be treated as a flag.");
BLT_ASSERT(arg.value().empty() && "Single plus flag should have empty value.");
BLT_ASSERT(arg.get_flag() == "+" && "Single plus flag should match the input string.");
}
// Test Case 14: Handle arguments with prefix only (like '++')
2025-02-13 01:53:21 -05:00
void test_argument_string_t_double_plus(const hashset_t<char>& prefixes)
2025-02-12 19:42:50 -05:00
{
2025-02-13 01:53:21 -05:00
const argument_string_t arg("++", prefixes);
2025-02-12 19:42:50 -05:00
BLT_ASSERT(arg.is_flag() && "Double plus ('++') should be treated as a flag.");
BLT_ASSERT(arg.value().empty() && "Double plus flag should have empty value.");
BLT_ASSERT(arg.get_flag() == "++" && "Double plus value should match the input string.");
}
2025-02-12 02:54:22 -05:00
void run_all_tests_argument_string_t()
{
2025-02-13 01:53:21 -05:00
const hashset_t<char> prefixes = {'-', '+'};
test_argument_string_t_flag_basic(prefixes);
test_argument_string_t_long_flag(prefixes);
test_argument_string_t_positional_argument(prefixes);
test_argument_string_t_empty_input(prefixes);
test_argument_string_t_single_hyphen(prefixes);
test_argument_string_t_double_hyphen(prefixes);
test_argument_string_t_with_spaces(prefixes);
test_argument_string_t_numeric_flag(prefixes);
test_argument_string_t_plus_flag_basic(prefixes);
test_argument_string_t_single_plus(prefixes);
test_argument_string_t_double_plus(prefixes);
2025-02-12 02:54:22 -05:00
}
2025-02-12 19:42:50 -05:00
2025-02-12 02:54:22 -05:00
void test()
{
run_all_tests_argument_string_t();
}
}
}