Skip to content

Commit c3cbe4a

Browse files
authored
Improve robustness to whitespace in gz_TEST (#180)
The amount of indentation in `gz plugin --help` can vary, so add a helper function that looks for two substrings separated only by spaces and use it instead of hard-coding the expected amount of whitespace. Signed-off-by: Steve Peters <scpeters@openrobotics.org>
1 parent 4341e1b commit c3cbe4a

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

loader/src/gz_TEST.cc

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,41 @@ std::string custom_exec_str(std::string _cmd)
5757
return result;
5858
}
5959

60+
//////////////////////////////////////////////////
61+
/// \brief Verify that two substrings are found within a larger string,
62+
/// separated only by spaces
63+
void verifySpaceSeparatedSubstrings(const std::string &_stringToSearch,
64+
const std::string &_substring1,
65+
const std::string &_substring2)
66+
{
67+
auto iterator1 = _stringToSearch.find(_substring1);
68+
EXPECT_NE(std::string::npos, iterator1)
69+
<< " failed to find \"" << _substring1 << "\" in:\n" << _stringToSearch;
70+
ASSERT_LE(iterator1 + _substring1.size(), _stringToSearch.size());
71+
auto iterator2 =
72+
_stringToSearch.find_first_not_of(' ', iterator1 + _substring1.size());
73+
EXPECT_NE(std::string::npos, iterator2);
74+
ASSERT_LE(iterator2 + _substring2.size(), _stringToSearch.size());
75+
EXPECT_EQ(_substring2, _stringToSearch.substr(iterator2, _substring2.size()));
76+
}
77+
6078
//////////////////////////////////////////////////
6179
/// \brief Check 'gz plugin --help'.
62-
TEST(gzTest, IgnPluginHelp)
80+
TEST(gzTest, GzPluginHelp)
6381
{
6482
// Path to gz executable
6583
std::string gz = std::string(GZ_PATH);
6684
std::string output = custom_exec_str(gz + " plugin --help");
67-
EXPECT_NE(std::string::npos,
68-
output.find("-i [--info] Get info about a plugin."))
69-
<< output;
70-
EXPECT_NE(std::string::npos,
71-
output.find("-p [--plugin] TEXT Path to a plugin."))
72-
<< output;
85+
verifySpaceSeparatedSubstrings(
86+
output, "-i [--info]", "Get info about a plugin.");
87+
verifySpaceSeparatedSubstrings(
88+
output, "-p [--plugin] TEXT", "Path to a plugin.");
7389

7490
output = custom_exec_str(gz + " plugin");
75-
EXPECT_NE(std::string::npos,
76-
output.find("-i [--info] Get info about a plugin."))
77-
<< output;
78-
EXPECT_NE(std::string::npos,
79-
output.find("-p [--plugin] TEXT Path to a plugin."))
80-
<< output;
91+
verifySpaceSeparatedSubstrings(
92+
output, "-i [--info]", "Get info about a plugin.");
93+
verifySpaceSeparatedSubstrings(
94+
output, "-p [--plugin] TEXT", "Path to a plugin.");
8195
}
8296

8397
//////////////////////////////////////////////////

0 commit comments

Comments
 (0)