From a1aace2658dbf512efa2e12d273f1527c923c3a1 Mon Sep 17 00:00:00 2001 From: rfprod Date: Thu, 5 Nov 2026 22:59:48 +0400 Subject: [PATCH] feat(program): add sentence-aware splitting and test coverage reporting - [x] rewrite `split_text` to prefer sentence boundaries and avoid abbreviation breaks; - [x] make `wrap_text` break long words at `max_width` and reject non-positive widths; - [x] validate `chunk_size` and `overlap`, throwing `std::invalid_argument` on bad input; - [x] declare private helpers for sentence and abbreviation detection in `TextUtils`; - [x] document `wrap_text` and `split_text` parameters and return values in the header; - [x] add `text_utils_test.cpp` covering `wrap_text`, `split_text` with and without overlap; - [x] glob test sources recursively and move `args_utils_test`.cpp into `tests/utils`; - [x] add `ENABLE_COVERAGE` `CMake` option; - [x] add `--coverage` and `--no-run` flags to the build script with matching env vars; - [x] validate `Debug` build, `gcovr`, and `GCOV_EXECUTABLE` before running coverage; - [x] clear stale `.gcda` files and generate `HTML` and `XML` coverage reports with `gcovr`; - [x] document `gcovr` installation in the root `README`; - [x] publish coverage page in docs and build reports in the `gh-pages` workflow; - [x] bump project version to `0.4.4`; --- .github/workflows/gh-pages.yml | 29 +++ CMakeLists.txt | 11 +- README.md | 2 +- docs/index.rst | 6 + docs/test_coverage.rst | 10 + src/utils/text_utils.cpp | 270 +++++++++++++++++++++----- src/utils/text_utils.hpp | 81 +++++++- tests/CMakeLists.txt | 16 +- tests/{ => utils}/args_utils_test.cpp | 22 +-- tests/utils/text_utils_test.cpp | 81 ++++++++ tools/shell/build.sh | 115 ++++++++++- 11 files changed, 570 insertions(+), 73 deletions(-) create mode 100644 docs/test_coverage.rst rename tests/{ => utils}/args_utils_test.cpp (86%) create mode 100644 tests/utils/text_utils_test.cpp diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index 29a136b..6502363 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -25,6 +25,10 @@ jobs: - name: Check out repository uses: actions/checkout@v7 + - name: Workspace cache + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + uses: ./.github/actions/workspace-cache + - name: Install Doxygen and Graphviz run: | sudo apt-get update @@ -68,6 +72,31 @@ jobs: deactivate + - name: Install C++ compiler and build tools + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + run: | + sudo apt-get update + sudo apt-get install -y build-essential cmake git python3-pip clang-format clang-tidy cppcheck + sudo apt-get install -y libcurl4-openssl-dev + + - name: Install Conan and gcovr + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + run: | + source .venv/bin/activate + pip install conan gcovr + conan --version + conan profile detect --force + deactivate + + - name: Build unit tests with coverage and generate the report + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + run: | + source .venv/bin/activate + bash ./tools/shell/build.sh --clean --tests --coverage --no-run + mkdir -p build/site/coverage + cp -r build/Debug/coverage/. build/site/coverage/ + deactivate + - name: Upload downloadable site artifact uses: actions/upload-artifact@v7 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 65614be..9590518 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) -project(cpp-workspace VERSION 0.4.3 LANGUAGES CXX) +project(cpp-workspace VERSION 0.4.4 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -12,6 +12,15 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) option(CPP_WORKSPACE_BUILD_TESTS "Build unit tests" ON) +option(ENABLE_COVERAGE "Build with gcov instrumentation" OFF) + +if(ENABLE_COVERAGE) + if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + message(FATAL_ERROR "ENABLE_COVERAGE requires GCC") + endif() + add_compile_options(--coverage -O0 -g) + add_link_options(--coverage) +endif() message(STATUS "Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}") file(GLOB_RECURSE CORE_SOURCES CONFIGURE_DEPENDS diff --git a/README.md b/README.md index 8bca87a..371f8ae 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,7 @@ source .env; \ ```bash source .venv/bin/activate; \ - pip install conan; \ + pip install conan gcovr; \ conan --version; \ conan profile detect --force ``` diff --git a/docs/index.rst b/docs/index.rst index f73d0e0..b777080 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,6 +1,12 @@ cpp-workspace documentation =========================== +.. toctree:: + :maxdepth: 2 + :caption: Contents + + test_coverage + C++ API reference ----------------- diff --git a/docs/test_coverage.rst b/docs/test_coverage.rst new file mode 100644 index 0000000..0947584 --- /dev/null +++ b/docs/test_coverage.rst @@ -0,0 +1,10 @@ +Test coverage +============= + +The coverage report is generated from the unit tests on each deployment. + +.. raw:: html + +

Open the full coverage report

+ diff --git a/src/utils/text_utils.cpp b/src/utils/text_utils.cpp index bcee996..a1667b4 100644 --- a/src/utils/text_utils.cpp +++ b/src/utils/text_utils.cpp @@ -1,53 +1,69 @@ #include "text_utils.hpp" +#include +#include +#include /** - * @brief Wraps text to fit within a specified width. + * Abbreviations that end with a period but do not end a sentence. + * Entries are lowercase and include the trailing period. + */ +const std::vector TextUtils::abbreviationExceptions = {"e.g.", "i.e.", "vs.", "approx.", "dr.", "mr.", + "mrs.", "ms.", "prof.", "jr.", "sr.", "st.", + "fig.", "no.", "vol.", "pp.", "ca."}; + +/** + * Wraps text to fit within a maximum line width. * - * Splits the input text into multiple lines, breaking at word boundaries - * where possible to avoid exceeding the maximum width. Respects natural - * spaces and removes leading spaces from wrapped lines. + * Splits the input text into lines, breaking at the last space that fits + * within max_width. Spaces at the start of each wrapped line and at the end + * of each line are removed. * * @param text The input text to wrap. - * @param max_width The maximum width (in characters) for each line. - * @return A vector of strings, each representing a wrapped line. - * The last line may be shorter than max_width. + * @param max_width The maximum number of characters per line. Must be greater than 0. + * @return A vector of wrapped lines. Each line is at most max_width characters. + * The last line may be shorter. * - * @note If a single word exceeds max_width, it will be placed on its own - * line without being broken. + * @throws std::invalid_argument if max_width is 0 or negative. + * + * @note If a single word is longer than max_width, it is broken at max_width + * characters. Only space characters are treated as break points. * * Algorithm: - * 1. Iteratively extract lines of up to max_width characters - * 2. Prefer breaking at word boundaries (spaces) - * 3. If no space exists within max_width, break at max_width - * 4. Remove leading whitespace from each new line - * 5. Continue until remaining text fits within max_width + * 1. Remove leading spaces from the text. + * 2. While the remaining text is longer than max_width, find the last space + * at or before max_width. + * 3. If no space exists, break at max_width. + * 4. Emit the line without trailing spaces, then remove leading spaces from the rest. + * 5. Emit any remaining text. */ -std::vector TextUtils::wrap_text(const std::string& text, int max_width) { +std::vector TextUtils::wrap_text(const std::string& text, const int max_width) { + if (max_width <= 0) { + throw std::invalid_argument("max_width must be greater than 0"); + } + + const size_t width = static_cast(max_width); std::vector result; - std::string remaining = text; - while (remaining.length() > max_width) { - // Find the last space within max_width. - int wrap_position = remaining.rfind(' ', max_width); + const size_t first = text.find_first_not_of(' '); + std::string remaining = (first == std::string::npos) ? std::string() : text.substr(first); + while (remaining.length() > width) { + // Find the last space within the width, or break at the width if none exists. + size_t wrap_position = remaining.rfind(' ', width); if (wrap_position == std::string::npos) { - wrap_position = max_width; + wrap_position = width; } - // Add the wrapped line without trailing space. + // Emit the line without trailing spaces. The first character is never a space + // here, so the line is never empty. std::string line = remaining.substr(0, wrap_position); + line.erase(line.find_last_not_of(' ') + 1); result.push_back(line); - // Skip the space and continue with remainder. + // Move past the break and remove leading spaces from the next line. remaining = remaining.substr(wrap_position); - - // Trim leading spaces from next line. - size_t first_non_space = remaining.find_first_not_of(' '); - if (first_non_space != std::string::npos) { - remaining = remaining.substr(first_non_space); - } else { - break; - } + const size_t next_start = remaining.find_first_not_of(' '); + remaining = (next_start == std::string::npos) ? std::string() : remaining.substr(next_start); } if (!remaining.empty()) { @@ -58,27 +74,191 @@ std::vector TextUtils::wrap_text(const std::string& text, int max_w } /** - * Splits text into fixed-size chunks with optional overlap. + * Checks whether a character is whitespace. + * + * @param c The character to test. + * @return true if c is a space, tab, newline, or other whitespace character. + */ +bool TextUtils::is_space(const char c) { + return std::isspace(static_cast(c)) != 0; +} + +/** + * Checks whether the token ending before a whitespace character is an abbreviation. + * + * Extracts the token that ends at space_idx, strips leading punctuation such as + * '(' or '"', converts it to lowercase, and compares it to abbreviationExceptions. + * + * @param text The full input text. + * @param space_idx The index of the whitespace character that follows the token. + * @return true if the token is in abbreviationExceptions. + */ +bool TextUtils::is_abbreviation(const std::string& text, const size_t space_idx) { + size_t word_start = space_idx; + while (word_start > 0 && !is_space(text[word_start - 1])) { + word_start -= 1; + } + + // Strip leading punctuation so "(e.g." still matches. + while (word_start < space_idx && !std::isalnum(static_cast(text[word_start]))) { + word_start += 1; + } + + std::string word = text.substr(word_start, space_idx - word_start); + std::transform(word.begin(), word.end(), word.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + + return std::find(abbreviationExceptions.begin(), abbreviationExceptions.end(), word) + != abbreviationExceptions.end(); +} + +/** + * Checks whether a whitespace character marks the end of a sentence. + * + * A sentence ends at whitespace preceded by '!' or '?', or by '.' when the + * preceding token is not an abbreviation. + * + * @param text The full input text. + * @param i The index to test. Must be less than text.length(). + * @return true if index i is a sentence boundary. + */ +bool TextUtils::is_sentence_boundary(const std::string& text, const size_t i) { + if (i == 0 || !is_space(text[i])) { + return false; + } + + const char prev = text[i - 1]; + if (prev == '!' || prev == '?') { + return true; + } + if (prev == '.') { + return !is_abbreviation(text, i); + } + return false; +} + +/** + * Finds the best position to end a chunk within a window. + * + * Searches backward from end for a sentence boundary first, then for any + * whitespace. Only positions at or after start + min_len are considered, + * so the resulting chunk is at least min_len characters long. + * + * @param text The full input text. + * @param start The index where the current chunk begins. + * @param end The maximum index for the chunk end. Must be less than text.length(). + * @param min_len The minimum chunk length in characters. + * @return The break index, or 0 if no suitable break exists. + */ +size_t TextUtils::find_break(const std::string& text, const size_t start, const size_t end, const size_t min_len) { + const size_t lo = start + std::max(1, min_len); // chunk must be at least min_len long + if (lo > end) { + return 0; + } + + for (size_t i = end; i >= lo; i -= 1) { + if (is_sentence_boundary(text, i)) { + return i; + } + } + + for (size_t i = end; i >= lo; i -= 1) { + if (is_space(text[i])) { + return i; + } + } + + return 0; +} + +/** + * Splits text into sentence-aware chunks with optional overlap. + * + * Each chunk is at most chunk_size characters. The method prefers to end a + * chunk at a sentence boundary, then at any whitespace, and only splits inside + * a word when the window contains no whitespace in its second half. Chunks are + * at least chunk_size / 2 characters long, except for the final chunk. + * + * Consecutive chunks share approximately overlap characters. The next chunk + * starts at the earliest sentence start inside the overlap region, or at the + * nearest word start if there is none. The overlap is therefore at most + * overlap characters and may be smaller. * - * Divides the input text into substrings of the specified chunk size. - * If overlap is specified, chunks will share overlapping content, allowing - * for smooth transitions between consecutive chunks. The final chunk may be - * shorter if the text length is not evenly divisible by chunk_size. + * Leading and trailing whitespace is removed from each chunk. * - * @param text The input string to split - * @param chunk_size The size of each chunk in characters - * @param overlap The number of overlapping characters between consecutive chunks - * @return A vector of string chunks + * @param text The input text to split. + * @param chunk_size The maximum size of each chunk in characters. Must be greater than 0. + * @param overlap The approximate number of characters shared between consecutive chunks. + * Must be less than chunk_size. + * @return A vector of chunks. Returns an empty vector if text contains only whitespace. + * + * @throws std::invalid_argument if chunk_size is 0 or overlap is greater than or equal to chunk_size. */ -std::vector TextUtils::split_text(const std::string& text, size_t chunk_size, size_t overlap) { +std::vector TextUtils::split_text(const std::string& text, const size_t chunk_size, const size_t overlap) { + if (chunk_size == 0) { + throw std::invalid_argument("chunk_size must be greater than 0"); + } + if (overlap >= chunk_size) { + throw std::invalid_argument("overlap must be smaller than chunk_size"); + } + + const size_t min_len = chunk_size / 2; std::vector chunks; + const size_t text_length = text.length(); size_t start = 0; - while (start < text.length()) { - size_t end = std::min(start + chunk_size, text.length()); - chunks.push_back(text.substr(start, end - start)); - start = end - overlap; - if (overlap == 0) start = end; + while (start < text_length && is_space(text[start])) { + start += 1; + } + + while (start < text_length) { + size_t end = std::min(start + chunk_size, text_length); + + if (end < text_length) { + const size_t text_break = find_break(text, start, end, min_len); + if (text_break != 0) { + end = text_break; // otherwise no acceptable break exists: hard split + } + } + + size_t chunk_end = end; + while (chunk_end > start && is_space(text[chunk_end - 1])) { + chunk_end -= 1; + } + chunks.push_back(text.substr(start, chunk_end - start)); + + if (end >= text_length) { + break; + } + + // Begin the next chunk `overlap` characters back. + size_t next = (end > overlap) ? end - overlap : 0; + if (next <= start) { + next = end; // guarantees forward progress + } + + // Prefer the earliest sentence start inside the overlap region. + size_t sentence_start = end; + for (size_t i = next; i < end; i += 1) { + if (is_sentence_boundary(text, i)) { + sentence_start = i; + break; + } + } + + if (sentence_start < end) { + next = sentence_start; + } else { + // No sentence start available: snap forward to the start of a word. + while (next < end && !is_space(text[next - 1])) { + next += 1; + } + } + + while (next < text_length && is_space(text[next])) { + next += 1; // skip the whitespace + } + start = next; } return chunks; diff --git a/src/utils/text_utils.hpp b/src/utils/text_utils.hpp index e445f8e..18bf308 100644 --- a/src/utils/text_utils.hpp +++ b/src/utils/text_utils.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -7,12 +8,84 @@ * @class TextUtils * @brief Utility class for text manipulation operations. * - * Provides static methods for common text processing tasks, including - * line wrapping based on a specified maximum width. + * Provides static helpers for wrapping text to a maximum line width and for + * splitting text into overlapping, sentence-aware chunks suitable for + * vector embeddings. */ class TextUtils { + private: + /** + * Abbreviations that end with a period but do not end a sentence. + * Entries are lowercase and include the trailing period. + */ + static const std::vector abbreviationExceptions; + + /** + * Checks whether a character is whitespace. + * + * @param c The character to test. + * @return true if c is a space, tab, newline, or other whitespace character. + */ + static bool is_space(const char c); + + /** + * Checks whether the token ending before a whitespace character is an abbreviation. + * + * @param text The full input text. + * @param space_idx The index of the whitespace character that follows the token. + * @return true if the token (case-insensitive) is in abbreviationExceptions. + */ + static bool is_abbreviation(const std::string& text, const size_t space_idx); + + /** + * Checks whether a whitespace character marks the end of a sentence. + * + * A sentence ends at whitespace preceded by '!' or '?', or by '.' when + * the preceding token is not an abbreviation. + * + * @param text The full input text. + * @param i The index to test. Must be less than text.length(). + * @return true if index i is a sentence boundary. + */ + static bool is_sentence_boundary(const std::string& text, const size_t i); + + /** + * Finds the best position to end a chunk within a window. + * + * Searches backward from end for a sentence boundary, then for any + * whitespace. Only positions at or after start + min_len are considered, + * so the resulting chunk is at least min_len characters long. + * + * @param text The full input text. + * @param start The index where the current chunk begins. + * @param end The maximum index for the chunk end. Must be less than text.length(). + * @param min_len The minimum chunk length in characters. + * @return The break index, or 0 if no suitable break exists. + */ + static size_t find_break(const std::string& text, const size_t start, const size_t end, const size_t min_len); + public: - static std::vector wrap_text(const std::string& text, int max_width); + /** + * Wraps text to fit within a maximum line width. + * + * Breaks lines at the last space within the width. If a word is longer than + * the width, the line is broken at max_width characters. + * + * @param text The input text to wrap. + * @param max_width The maximum number of characters per line. Must be greater than 0. + * @return A vector of wrapped lines. Each line is at most max_width characters. + */ + static std::vector wrap_text(const std::string& text, const int max_width); - static std::vector split_text(const std::string& text, size_t chunk_size, size_t overlap = 0); + /** + * Splits text into sentence-aware chunks with optional overlap. + * + * @param text The input text to split. + * @param chunk_size The maximum size of each chunk in characters. Must be greater than 0. + * @param overlap The approximate number of characters shared between consecutive chunks. + * Must be less than chunk_size. Defaults to 0. + * @return A vector of chunks with leading and trailing whitespace removed. + */ + static std::vector split_text(const std::string& text, const size_t chunk_size, + const size_t overlap = 0); }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 00710fa..fe563b9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,21 @@ find_package(GTest CONFIG REQUIRED) +message(STATUS "Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}") +file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/interface/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/interface/*.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/ollama/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ollama/*.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/storage/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/storage/*.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.hpp) +if(NOT TEST_SOURCES) + message(FATAL_ERROR "No test sources found. Check the paths in TEST_SOURCES.") +endif() + add_executable(cpp-workspace-tests - args_utils_test.cpp + ${TEST_SOURCES} ) target_link_libraries(cpp-workspace-tests diff --git a/tests/args_utils_test.cpp b/tests/utils/args_utils_test.cpp similarity index 86% rename from tests/args_utils_test.cpp rename to tests/utils/args_utils_test.cpp index 9834e5b..5912a2b 100644 --- a/tests/args_utils_test.cpp +++ b/tests/utils/args_utils_test.cpp @@ -39,7 +39,7 @@ class Argv { } // namespace -TEST(ArgsUtilsDefaults, ReturnsDefaultsWhenNoArgsGiven) { +TEST(ArgsUtils_defaults, returns_defaults_when_no_args_given) { ArgsUtils utils; Argv argv; auto p = utils.parse_args(argv.argc(), argv.argv()); @@ -55,7 +55,7 @@ TEST(ArgsUtilsDefaults, ReturnsDefaultsWhenNoArgsGiven) { EXPECT_FALSE(p.help); } -TEST(ArgsUtilsParse, AssignsStringValues) { +TEST(ArgsUtils_parse_args, assigns_string_values) { ArgsUtils utils; Argv argv{"--model=llama3", "--embedding_model=nomic", "--prompt=hi there"}; auto p = utils.parse_args(argv.argc(), argv.argv()); @@ -65,25 +65,25 @@ TEST(ArgsUtilsParse, AssignsStringValues) { EXPECT_EQ(p.prompt, "hi there"); } -TEST(ArgsUtilsParse, SplitsOnFirstEqualsOnly) { +TEST(ArgsUtils_parse_args, splits_on_first_equals_only) { ArgsUtils utils; Argv argv{"--prompt=a=b"}; EXPECT_EQ(utils.parse_args(argv.argc(), argv.argv()).prompt, "a=b"); } -TEST(ArgsUtilsParse, EmptyValueYieldsEmptyString) { +TEST(ArgsUtils_parse_args, empty_value_yields_empty_string) { ArgsUtils utils; Argv argv{"--model="}; EXPECT_EQ(utils.parse_args(argv.argc(), argv.argv()).model, ""); } -TEST(ArgsUtilsParse, LastOccurrenceWins) { +TEST(ArgsUtils_parse_args, last_occurrence_wins) { ArgsUtils utils; Argv argv{"--model=a", "--model=b"}; EXPECT_EQ(utils.parse_args(argv.argc(), argv.argv()).model, "b"); } -TEST(ArgsUtilsParse, IgnoresArgsWithoutEquals) { +TEST(ArgsUtils_parse_args, ignores_args_without_equals) { ArgsUtils utils; Argv argv{"--stream", "--model"}; auto p = utils.parse_args(argv.argc(), argv.argv()); @@ -92,7 +92,7 @@ TEST(ArgsUtilsParse, IgnoresArgsWithoutEquals) { EXPECT_EQ(p.model, "gemma3n:latest"); } -TEST(ArgsUtilsParse, IgnoresUnknownKeys) { +TEST(ArgsUtils_parse_args, ignores_unknown_keys) { ArgsUtils utils; Argv argv{"--unknown=x"}; auto p = utils.parse_args(argv.argc(), argv.argv()); @@ -106,14 +106,14 @@ using BoolCase = std::pair; class BoolFlagTest : public testing::TestWithParam {}; -TEST_P(BoolFlagTest, ExactTrueEnablesFlag) { +TEST_P(BoolFlagTest, exact_true_enables_flag) { ArgsUtils utils; Argv argv{GetParam().first + "=true"}; auto p = utils.parse_args(argv.argc(), argv.argv()); EXPECT_TRUE(p.*GetParam().second); } -TEST_P(BoolFlagTest, NonExactTrueValuesLeaveFlagDisabled) { +TEST_P(BoolFlagTest, non_exact_true_values_leave_flag_disabled) { ArgsUtils utils; for (const char* val : {"TRUE", "1", "yes", "false"}) { Argv argv{GetParam().first + "=" + val}; @@ -122,14 +122,14 @@ TEST_P(BoolFlagTest, NonExactTrueValuesLeaveFlagDisabled) { } } -INSTANTIATE_TEST_SUITE_P(AllBoolFlags, BoolFlagTest, +INSTANTIATE_TEST_SUITE_P(AllBool_flags, BoolFlagTest, testing::Values(BoolCase{"--stream", &ParsedArgs::stream}, BoolCase{"--rag", &ParsedArgs::rag}, BoolCase{"--interactive", &ParsedArgs::interactive}, BoolCase{"--dark_theme", &ParsedArgs::dark_theme}, BoolCase{"--skip_embedding", &ParsedArgs::skip_embedding}, BoolCase{"--help", &ParsedArgs::help})); -TEST(ArgsUtilsHelp, DocumentsEveryHandledFlag) { +TEST(ArgsUtils_help_text, documents_every_handled_flag) { ArgsUtils utils; const std::string text = utils.help_text(); for (const char* key : {"--model=", "--embedding_model=", "--prompt=", "--stream=", "--rag=", "--interactive=", diff --git a/tests/utils/text_utils_test.cpp b/tests/utils/text_utils_test.cpp new file mode 100644 index 0000000..e4a6eb6 --- /dev/null +++ b/tests/utils/text_utils_test.cpp @@ -0,0 +1,81 @@ +#include + +#include +#include +#include + +#include "utils/text_utils.hpp" + +TEST(TextUtils_wrap_text, no_wrap) { + std::string text = "This text will not be wrapped."; + int max_width = text.length() + 1; + + std::vector result = TextUtils::wrap_text(text, max_width); + + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result.at(0), text); +} + +TEST(TextUtils_wrap_text, wrap) { + std::string text = "This text value will be wrapped."; + int max_width = text.length() / 2; + + std::vector result = TextUtils::wrap_text(text, max_width); + + size_t last_non_space{}; + + std::string first = text.substr(0, max_width); + last_non_space = first.find_last_not_of(' '); + first.resize(last_non_space + 1); + + std::string second = text.substr(max_width); + last_non_space = second.find_last_not_of(' '); + second.resize(last_non_space + 1); + + EXPECT_EQ(result.size(), 2); + EXPECT_EQ(result.at(0), first); + EXPECT_EQ(result.at(1), second); +} + +TEST(TextUtils_split_text, no_split) { + std::string text = "This text will not be chunked."; + size_t chunk_size = text.length() + 1; + size_t overlap = 0; + + std::vector chunks = TextUtils::split_text(text, chunk_size, overlap); + + EXPECT_EQ(chunks.size(), 1); + EXPECT_EQ(chunks.at(0), text); +} + +TEST(TextUtils_split_text, split_no_overlap) { + std::string text = "This text value will be chunked."; + size_t chunk_size = text.length() / 2; + size_t overlap = 0; + + std::vector chunks = TextUtils::split_text(text, chunk_size, overlap); + + size_t last_non_space{}; + + std::string first = text.substr(0, chunk_size); + last_non_space = first.find_last_not_of(' '); + first.resize(last_non_space + 1); + + std::string second = text.substr(chunk_size - overlap); + last_non_space = second.find_last_not_of(' '); + second.resize(last_non_space + 1); + + EXPECT_EQ(chunks.size(), 2); + EXPECT_EQ(chunks.at(0), first); + EXPECT_EQ(chunks.at(1), second); +} + +TEST(TextUtils_split_text, split_with_overlap) { + std::string text = "This text value is chunked with overlap."; + size_t chunk_size = text.length() / 2; + size_t overlap = 4; + + std::vector chunks = TextUtils::split_text(text, chunk_size, overlap); + + EXPECT_EQ(chunks.size(), 3); +} diff --git a/tools/shell/build.sh b/tools/shell/build.sh index 726d870..7f76bc6 100644 --- a/tools/shell/build.sh +++ b/tools/shell/build.sh @@ -21,6 +21,8 @@ show_help() { -b DIR, --build-dir DIR Specify build directory (default: build) -t, --tests Build and run unit tests (default: off) --no-tests Build without unit tests (overrides ENABLE_TESTS) + --no-run Builds without running executable (overrides RUN_EXECUTABLE) + --coverage Enable GCC coverage and generate gcovr reports (Debug only, implies --tests) --cxx-flags FLAGS Set CMAKE_CXX_FLAGS (default: -DDEBUG) --no-debug-flags Build without -DDEBUG flag --exe-args ARGS Pass arguments to executable (e.g., '--skip_embedding=true')" @@ -29,6 +31,8 @@ show_help() { BUILD_TYPE Set build type: Debug or Release (default: Debug) CLEAN_BUILD Set to true to clean before building (default: false) ENABLE_TESTS Set to true to build and run tests (default: false) + ENABLE_COVERAGE Set to true to enable coverage and generate reports (default: false) + GCOV_EXECUTABLE gcov binary matching the GCC version (default: gcov-13) CMAKE_CXX_FLAGS Set C++ compiler flags (default: -DDEBUG) EXECUTABLE_ARGS Arguments to pass to the executable" @@ -40,8 +44,11 @@ show_help() { ./tools/shell/build.sh --exe-args '--skip_embedding=true --other-arg=true' # Pass args to exe ./tools/shell/build.sh --tests # Debug build with tests ./tools/shell/build.sh --release --tests # Release build with tests + ./tools/shell/build.sh --release --tests --no-run # Release build with tests without binary run + ./tools/shell/build.sh --coverage # Debug build, tests, and coverage report ENABLE_TESTS=true ./tools/shell/build.sh # Via env var + ENABLE_COVERAGE=true ./tools/shell/build.sh # Via env var CMAKE_CXX_FLAGS='-O3' ./tools/shell/build.sh # Via env var" } @@ -51,7 +58,10 @@ BUILD_DIR="build" BUILD_TYPE="${BUILD_TYPE:-Debug}" CLEAN_BUILD="${CLEAN_BUILD:-false}" ENABLE_TESTS="${ENABLE_TESTS:-false}" +ENABLE_COVERAGE="${ENABLE_COVERAGE:-false}" +GCOV_EXECUTABLE="${GCOV_EXECUTABLE:-gcov-13}" PACKAGE_RELEASE="${PACKAGE_RELEASE:-false}" +RUN_EXECUTABLE="${RUN_EXECUTABLE:-true}" # Additional configuration CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS:--DDEBUG}" EXECUTABLE_ARGS="${EXECUTABLE_ARGS:-}" @@ -87,6 +97,14 @@ while [[ $# -gt 0 ]]; do ENABLE_TESTS=false shift ;; + --no-run) + RUN_EXECUTABLE=false + shift + ;; + --coverage) + ENABLE_COVERAGE=true + shift + ;; --cxx-flags) CMAKE_CXX_FLAGS="$2" shift 2 @@ -212,6 +230,59 @@ check_test_link_for_ncurses_cxx() { log_success "Test link line is free of ncurses++" } +# Validates prerequisites for coverage and forces tests on, since coverage needs them. +validate_coverage_setup() { + if [ "$BUILD_TYPE" != "Debug" ]; then + log_error "Coverage requires a Debug build (-O0 -g). Remove --release or set BUILD_TYPE=Debug." + exit 1 + fi + + if [ "$ENABLE_TESTS" != true ]; then + log_warning "Coverage requires tests; enabling tests" + ENABLE_TESTS=true + fi + + if ! command -v gcovr >/dev/null 2>&1; then + log_error "gcovr not found. Install it with: pip install gcovr" + exit 1 + fi + + if ! command -v "$GCOV_EXECUTABLE" >/dev/null 2>&1; then + log_error "$GCOV_EXECUTABLE not found. It must match the GCC version used for the build." + exit 1 + fi + + log_success "Coverage tooling validated (gcovr $(gcovr --version | head -n1 | awk '{print $2}'), ${GCOV_EXECUTABLE})" +} + +# Deletes .gcda files so counters from earlier runs don't leak into the report. +clean_coverage_data() { + log_info "Removing stale coverage data (.gcda files)..." + find "$BUILD_DIR/$BUILD_TYPE" -name '*.gcda' -delete +} + +# Generates HTML and Cobertura XML coverage reports with gcovr. +generate_coverage_report() { + local COVERAGE_DIR="$BUILD_DIR/$BUILD_TYPE/coverage" + mkdir -p "$COVERAGE_DIR" + + log_info "Generating coverage reports..." + if gcovr --root . \ + --object-directory "$BUILD_DIR/$BUILD_TYPE" \ + --filter 'src/' \ + --exclude 'tests/' \ + --gcov-executable "$GCOV_EXECUTABLE" \ + --print-summary \ + --xml-pretty --output "$COVERAGE_DIR/coverage.xml" \ + --html-details "$COVERAGE_DIR/coverage.html"; then + log_success "Coverage HTML report: $COVERAGE_DIR/coverage.html" + log_success "Coverage Cobertura XML: $COVERAGE_DIR/coverage.xml" + else + log_error "Coverage report generation failed" + exit 1 + fi +} + # Main build script main() { @@ -230,13 +301,24 @@ main() { log_info "Version: ${VERSION}, Build: ${BUILD_NUMBER}" + if [ "$ENABLE_COVERAGE" = true ]; then + validate_coverage_setup + fi + local TESTS_CMAKE_FLAG TESTS_CMAKE_FLAG=OFF if [ "$ENABLE_TESTS" = true ]; then TESTS_CMAKE_FLAG=ON fi + local COVERAGE_CMAKE_FLAG + COVERAGE_CMAKE_FLAG=OFF + if [ "$ENABLE_COVERAGE" = true ]; then + COVERAGE_CMAKE_FLAG=ON + fi + log_info "Enable tests: ${ENABLE_TESTS}, Tests CMake Flag: ${TESTS_CMAKE_FLAG}" + log_info "Enable coverage: ${ENABLE_COVERAGE}, Coverage CMake Flag: ${COVERAGE_CMAKE_FLAG}" # Clean build directory if requested if [ "$CLEAN_BUILD" = true ]; then @@ -256,7 +338,7 @@ main() { fi log_info "Configuring CMake (${BUILD_TYPE})..." - if cmake -S . -B "$BUILD_DIR"/"$BUILD_TYPE" -DCMAKE_TOOLCHAIN_FILE="$PWD"/"$BUILD_DIR"/"$BUILD_TYPE"/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DCPP_WORKSPACE_BUILD_TESTS="$TESTS_CMAKE_FLAG" -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" -DCMAKE_C_COMPILER=gcc-13 -DCMAKE_CXX_COMPILER=g++-13; then + if cmake -S . -B "$BUILD_DIR"/"$BUILD_TYPE" -DCMAKE_TOOLCHAIN_FILE="$PWD"/"$BUILD_DIR"/"$BUILD_TYPE"/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DCPP_WORKSPACE_BUILD_TESTS="$TESTS_CMAKE_FLAG" -DENABLE_COVERAGE="$COVERAGE_CMAKE_FLAG" -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" -DCMAKE_C_COMPILER=gcc-13 -DCMAKE_CXX_COMPILER=g++-13; then log_success "CMake configured" else @@ -279,6 +361,10 @@ main() { exit 1 fi + if [ "$ENABLE_COVERAGE" = true ]; then + clean_coverage_data + fi + log_info "Running tests..." if ctest --test-dir "$BUILD_DIR/$BUILD_TYPE" --output-on-failure; then log_success "All tests passed" @@ -287,6 +373,10 @@ main() { fi check_test_link_for_ncurses_cxx + + if [ "$ENABLE_COVERAGE" = true ]; then + generate_coverage_report + fi else log_info "Skipping tests (use --tests to enable)" fi @@ -295,17 +385,21 @@ main() { BIN_NAME=cpp-workspace-app if [ -f "$BUILD_DIR/$BUILD_TYPE/bin/$BIN_NAME" ]; then - log_info "Running executable..." - load_env_file - if [ -n "$EXECUTABLE_ARGS" ]; then - # allow splitting of $EXECUTABLE_ARGS to support multiple executable arguments. - # shellcheck disable=SC2086 - "$BUILD_DIR/$BUILD_TYPE/bin/$BIN_NAME" $EXECUTABLE_ARGS + if [ "$RUN_EXECUTABLE" = true ]; then + log_info "Running executable..." + load_env_file + if [ -n "$EXECUTABLE_ARGS" ]; then + # allow splitting of $EXECUTABLE_ARGS to support multiple executable arguments. + # shellcheck disable=SC2086 + "$BUILD_DIR/$BUILD_TYPE/bin/$BIN_NAME" $EXECUTABLE_ARGS + else + "$BUILD_DIR/$BUILD_TYPE/bin/$BIN_NAME" + fi + + log_success "Executable ran successfully" else - "$BUILD_DIR/$BUILD_TYPE/bin/$BIN_NAME" + log_info "Executable exists, but RUN_EXECUTABLE=$RUN_EXECUTABLE (remove --no-run to enable)" fi - - log_success "Executable ran successfully" else log_warning "Executable not found at $BUILD_DIR/bin/$BIN_NAME" fi @@ -358,6 +452,7 @@ EOF echo " Build Number: ${BUILD_NUMBER}" echo " Build Type: ${BUILD_TYPE}" echo " Build Directory: ${BUILD_DIR}" + echo " Coverage: ${ENABLE_COVERAGE}" [ "$PACKAGE_RELEASE" = true ] && echo " Artifacts: release-${ARTIFACT_VERSION}.{tar.gz,zip}" echo "" }