yams/tests/unit/common/string_utils_catch2_test.cpp
trvon 3b1c86f490
refactor: deduplicate trim/escapeRegex/hasWildcard helpers
The same string helpers were copy-pasted across CLI and service code:
trim/trimCopy in 5 files, escapeRegex in 3, hasWildcard in 2, and the
regex meta-char table in 2. Move canonical versions to
yams::common::string_utils.h (trimCopy, hasWildcard, escapeRegex,
isRegexMetaChar) and wire all call sites to them; daemon_helpers'
trim_copy now delegates. Add a Catch2 test for the shared helpers.

Kept isTransientMetadataError and annotateResultRelations untouched:
they have semantically different implementations and need a separate
review, not a mechanical merge.
2026-08-01 00:12:31 -04:00

48 lines
1.9 KiB
C++

// Copyright (c) 2025 YAMS Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include <catch2/catch_test_macros.hpp>
#include <yams/common/string_utils.h>
namespace {
TEST_CASE("trimCopy removes leading and trailing whitespace", "[common][string][trim]") {
CHECK(yams::common::trimCopy(" hello ") == "hello");
CHECK(yams::common::trimCopy("\thello\n") == "hello");
CHECK(yams::common::trimCopy("hello") == "hello");
CHECK(yams::common::trimCopy(" ") == "");
CHECK(yams::common::trimCopy("") == "");
CHECK(yams::common::trimCopy(" \t a b \t ") == "a b");
}
TEST_CASE("hasWildcard detects glob metacharacters", "[common][string][wildcard]") {
CHECK_FALSE(yams::common::hasWildcard("plain"));
CHECK_FALSE(yams::common::hasWildcard(""));
CHECK(yams::common::hasWildcard("*.cpp"));
CHECK(yams::common::hasWildcard("foo?"));
CHECK(yams::common::hasWildcard("*"));
CHECK(yams::common::hasWildcard("a*b?c"));
}
TEST_CASE("escapeRegex escapes regex metacharacters", "[common][string][regex]") {
CHECK(yams::common::escapeRegex("plain") == "plain");
CHECK(yams::common::escapeRegex("a.b") == "a\\.b");
CHECK(yams::common::escapeRegex("") == "");
CHECK(yams::common::escapeRegex("\\^$.|?*+()[]{}") ==
"\\\\\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}");
CHECK(yams::common::escapeRegex("a+b*c?d") == "a\\+b\\*c\\?d");
CHECK(yams::common::escapeRegex("no-meta-here_1") == "no-meta-here_1");
}
TEST_CASE("isRegexMetaChar classifies characters", "[common][string][regex]") {
CHECK(yams::common::isRegexMetaChar('.'));
CHECK(yams::common::isRegexMetaChar('*'));
CHECK(yams::common::isRegexMetaChar('['));
CHECK(yams::common::isRegexMetaChar('\\'));
CHECK_FALSE(yams::common::isRegexMetaChar('a'));
CHECK_FALSE(yams::common::isRegexMetaChar('1'));
CHECK_FALSE(yams::common::isRegexMetaChar('_'));
}
} // namespace