Skip to content

Add comprehensive unit tests for argparse library#3

Merged
helium729 merged 3 commits intomainfrom
copilot/fix-2
Jun 13, 2025
Merged

Add comprehensive unit tests for argparse library#3
helium729 merged 3 commits intomainfrom
copilot/fix-2

Conversation

Copy link
Contributor

Copilot AI commented Jun 13, 2025

This PR adds a complete unit test suite for the argparse library, providing comprehensive coverage of all functionality and use conditions as requested in the issue.

Test Coverage Added

Test Framework

  • Created a lightweight test framework with assertion macros (ASSERT_TRUE, ASSERT_FALSE, ASSERT_EQ, ASSERT_STREQ)
  • Integrated with CMake's CTest for automated testing
  • Added custom run_tests target for easy test execution

Test Suites (44 total test cases)

Parser Tests (17 tests)

  • Constructor/destructor functionality
  • Parameter addition for all types (NONE, INTEGER, STRING, FLOAT)
  • Short flag parsing (-h)
  • Long flag parsing (--help)
  • Parameter value parsing and retrieval
  • Multiple parameter combinations
  • argc/argv interface compatibility
  • Error handling (unknown parameters, missing values)
  • Help message generation
  • Program name extraction from paths

Parameter Tests (12 tests)

  • parameter_none: Boolean flag behavior
  • parameter_integer: Integer parsing with different bases (decimal, hex, octal)
  • parameter_string: String parameter handling
  • parameter_float: Floating-point value parsing
  • Required parameter functionality
  • Parameter construction with empty names

Utility Tests (8 tests)

  • Parameter factory creation for all types
  • Default parameter type handling
  • Memory management verification
  • Parameter functionality after creation

Integration Tests (7 tests)

  • Complex real-world parsing scenarios
  • Mixed short/long parameter usage
  • Default value handling
  • Help message formatting
  • Comprehensive error scenarios
  • Edge cases and boundary conditions

Bug Fixes

During test development, discovered and fixed two minor issues:

  1. Uninitialized required field: Added proper initialization in parameter constructor
  2. Parameter lookup improvement: Enhanced get_parameter_value_to() to handle both short and long names when passed without dashes

Usage

# Build with tests
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
make

# Run all tests
ctest --verbose
# or
make run_tests

# Run individual test suites
./test_parser
./test_parameters  
./test_util
./test_integration

Test Results

✅ All 44 test cases pass (100% success rate)
✅ Original functionality verified with example program
✅ Memory management tested (no leaks)
✅ Error conditions properly handled

The test suite ensures the argparse library works correctly across all supported use cases and provides a solid foundation for future development.

Fixes #2.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: helium729 <30749877+helium729@users.noreply.github.com>
Copilot AI changed the title [WIP] Unit tests required. Add comprehensive unit tests for argparse library Jun 13, 2025
Copilot AI requested a review from helium729 June 13, 2025 09:33
@helium729
Copy link
Owner

helium729 commented Jun 13, 2025

Please update Readme and workflow corresponding @copilot

…ation

Co-authored-by: helium729 <30749877+helium729@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 13, 2025

Please update Readme and workflow corresponding @copilot

Updated README.md and GitHub workflow (.github/workflows/cmake.yml) to include comprehensive test suite information. The README now includes detailed testing instructions, compilation notes for building with tests, test coverage overview, and better usage examples. The workflow now runs tests in both Release and Debug modes with proper error reporting. Commit: 154bc0e

@helium729 helium729 marked this pull request as ready for review June 13, 2025 10:45
@helium729 helium729 merged commit c257783 into main Jun 13, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit tests required.

2 participants