From cc42b34a786f95092e6c880db10a65823cc369ef Mon Sep 17 00:00:00 2001 From: Stephen Brawner Date: Tue, 26 Jan 2021 11:33:21 -0800 Subject: [PATCH] Add tests and robustify logic Signed-off-by: Stephen Brawner --- include/sdf/Types.hh | 1 + src/Types.cc | 29 ++++++++++++++-- src/Types_TEST.cc | 79 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/include/sdf/Types.hh b/include/sdf/Types.hh index 0ce7580de..b8ab9a2d4 100644 --- a/include/sdf/Types.hh +++ b/include/sdf/Types.hh @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/src/Types.cc b/src/Types.cc index f5cf3e3db..6245d40e4 100644 --- a/src/Types.cc +++ b/src/Types.cc @@ -106,16 +106,39 @@ std::pair SplitName( return {"", _absoluteName}; } +static bool EndsWithDelimiter(const std::string &_s) +{ + if (_s.size() < kSdfScopeDelimiter.size()) + return false; + + const size_t findStartPosition = _s.size() - kSdfScopeDelimiter.size(); + return _s.substr(findStartPosition) == kSdfScopeDelimiter; +} + +static bool StartsWithDelimiter(const std::string &_s) +{ + if (_s.size() < kSdfScopeDelimiter.size()) + return false; + + return _s.substr(0, kSdfScopeDelimiter.size()) == kSdfScopeDelimiter; +} + // Join an scope name prefix with a local name using the scope delimeter std::string JoinName( const std::string &_scopeName, const std::string &_localName) { if (_scopeName.empty()) return _localName; - if (_localName.empty()) { + if (_localName.empty()) return _scopeName; - } - return _scopeName + kSdfScopeDelimiter + _localName; + + + if (EndsWithDelimiter(_scopeName) && StartsWithDelimiter(_localName)) + return _scopeName + _localName.substr(kSdfScopeDelimiter.size()); + else if (EndsWithDelimiter(_scopeName) || StartsWithDelimiter(_localName)) + return _scopeName + _localName; + else + return _scopeName + kSdfScopeDelimiter + _localName; } } } diff --git a/src/Types_TEST.cc b/src/Types_TEST.cc index bf1f80930..a486025d8 100644 --- a/src/Types_TEST.cc +++ b/src/Types_TEST.cc @@ -120,6 +120,85 @@ TEST(Types, ErrorsOutputStream) EXPECT_EQ(expected, output.str()); } +TEST(Types, SplitName) +{ + { + const auto[basePath, tipName] = sdf::SplitName("a::b"); + EXPECT_EQ(basePath, "a"); + EXPECT_EQ(tipName, "b"); + } + { + const auto[basePath, tipName] = sdf::SplitName("a::b::c"); + EXPECT_EQ(basePath, "a::b"); + EXPECT_EQ(tipName, "c"); + } + { + const auto[basePath, tipName] = sdf::SplitName("b"); + EXPECT_EQ(basePath, ""); + EXPECT_EQ(tipName, "b"); + } + { + const auto[basePath, tipName] = sdf::SplitName("a::b::"); + EXPECT_EQ(basePath, "a::b"); + EXPECT_EQ(tipName, ""); + } + { + const auto[basePath, tipName] = sdf::SplitName("::b"); + EXPECT_EQ(basePath, ""); + EXPECT_EQ(tipName, "b"); + } + { + const auto[basePath, tipName] = sdf::SplitName(""); + EXPECT_EQ(basePath, ""); + EXPECT_EQ(tipName, ""); + } + { + const auto[basePath, tipName] = sdf::SplitName("a::b::c::d"); + EXPECT_EQ(basePath, "a::b::c"); + EXPECT_EQ(tipName, "d"); + } +} + +TEST(Types, JoinName) +{ + { + const auto joinedName = sdf::JoinName("a", "b"); + EXPECT_EQ(joinedName, "a::b"); + } + { + const auto joinedName = sdf::JoinName("a::b", "c"); + EXPECT_EQ(joinedName, "a::b::c"); + } + { + const auto joinedName = sdf::JoinName("a", "b::c"); + EXPECT_EQ(joinedName, "a::b::c"); + } + { + const auto joinedName = sdf::JoinName("a::", "b"); + EXPECT_EQ(joinedName, "a::b"); + } + { + const auto joinedName = sdf::JoinName("a", "::b"); + EXPECT_EQ(joinedName, "a::b"); + } + { + const auto joinedName = sdf::JoinName("a::", "::b"); + EXPECT_EQ(joinedName, "a::b"); + } + { + const auto joinedName = sdf::JoinName("", "b"); + EXPECT_EQ(joinedName, "b"); + } + { + const auto joinedName = sdf::JoinName("a", ""); + EXPECT_EQ(joinedName, "a"); + } + { + const auto joinedName = sdf::JoinName("", ""); + EXPECT_EQ(joinedName, ""); + } +} + ///////////////////////////////////////////////// /// Main int main(int argc, char **argv)