Skip to content

Commit

Permalink
Add tests and robustify logic
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Brawner <brawner@gmail.com>
  • Loading branch information
brawner committed Jan 26, 2021
1 parent b62f7af commit cc42b34
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/sdf/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cstdint>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include <sdf/sdf_config.h>
Expand Down
29 changes: 26 additions & 3 deletions src/Types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,39 @@ std::pair<std::string, std::string> 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;
}
}
}
79 changes: 79 additions & 0 deletions src/Types_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cc42b34

Please sign in to comment.