|
3 | 3 |
|
4 | 4 | #include <gtest/gtest.h> |
5 | 5 | #include <cstring> |
| 6 | +#include <ostream> |
6 | 7 | #include <string> |
7 | 8 |
|
| 9 | +#include <opentelemetry/common/macros.h> |
8 | 10 | #include <opentelemetry/common/string_util.h> |
9 | 11 |
|
10 | 12 | // ------------------------- StringUtil class tests --------------------------------- |
@@ -56,3 +58,69 @@ TEST(StringUtilTest, TrimStringOutOfRange) |
56 | 58 | { |
57 | 59 | EXPECT_EQ(StringUtil::Trim("x", 2, 1), ""); |
58 | 60 | } |
| 61 | + |
| 62 | +struct SubstrTestData |
| 63 | +{ |
| 64 | + const char *input; |
| 65 | + size_t pos; |
| 66 | + size_t count; |
| 67 | + |
| 68 | + friend void PrintTo(const SubstrTestData &data, std::ostream *os) |
| 69 | + { |
| 70 | + *os << "(\"" << data.input << "\"," << data.pos << "," << data.count << ")"; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +const SubstrTestData substr_in_range_test_cases[] = { |
| 75 | + {"k1=v1", 0, std::string::npos}, |
| 76 | + {"k1=v1", 3, std::string::npos}, |
| 77 | + {"k1=v1", 0, 2}, |
| 78 | + {"k1=v1", 3, 100}, |
| 79 | + {"k1=v1", 2, 0}, |
| 80 | + {"k1=v1", 5, std::string::npos}, |
| 81 | + {"x", 0, std::string::npos}, |
| 82 | + {"x", 1, std::string::npos}, |
| 83 | + {"", 0, std::string::npos}, |
| 84 | + {"", 0, 0}, |
| 85 | +}; |
| 86 | + |
| 87 | +using SubstrTestFixture = ::testing::TestWithParam<SubstrTestData>; |
| 88 | + |
| 89 | +TEST_P(SubstrTestFixture, MatchesStdSubstr) |
| 90 | +{ |
| 91 | + const SubstrTestData test_param = GetParam(); |
| 92 | + const std::string input(test_param.input); |
| 93 | + |
| 94 | + EXPECT_EQ(StringUtil::Substr(input, test_param.pos, test_param.count), |
| 95 | + input.substr(test_param.pos, test_param.count)); |
| 96 | +} |
| 97 | + |
| 98 | +INSTANTIATE_TEST_SUITE_P(SubstrTestCases, |
| 99 | + SubstrTestFixture, |
| 100 | + ::testing::ValuesIn(substr_in_range_test_cases)); |
| 101 | + |
| 102 | +const SubstrTestData substr_out_of_range_test_cases[] = { |
| 103 | + {"k1=v1", 6, std::string::npos}, |
| 104 | + {"k1=v1", 100, 2}, |
| 105 | + {"k1=v1", std::string::npos, std::string::npos}, |
| 106 | + {"x", 2, std::string::npos}, |
| 107 | + {"", 1, std::string::npos}, |
| 108 | +}; |
| 109 | + |
| 110 | +using SubstrOutOfRangeTestFixture = ::testing::TestWithParam<SubstrTestData>; |
| 111 | + |
| 112 | +TEST_P(SubstrOutOfRangeTestFixture, HandlesPosPastTheEnd) |
| 113 | +{ |
| 114 | + const SubstrTestData test_param = GetParam(); |
| 115 | + |
| 116 | +#if OPENTELEMETRY_HAVE_EXCEPTIONS |
| 117 | + EXPECT_EQ(StringUtil::Substr(test_param.input, test_param.pos, test_param.count), ""); |
| 118 | +#else |
| 119 | + // Without exceptions the underlying calls std::terminate(), so helper cannot recover. |
| 120 | + EXPECT_DEATH({ StringUtil::Substr(test_param.input, test_param.pos, test_param.count); }, ""); |
| 121 | +#endif |
| 122 | +} |
| 123 | + |
| 124 | +INSTANTIATE_TEST_SUITE_P(SubstrOutOfRangeTestCases, |
| 125 | + SubstrOutOfRangeTestFixture, |
| 126 | + ::testing::ValuesIn(substr_out_of_range_test_cases)); |
0 commit comments