22// SPDX-License-Identifier: Apache-2.0
33
44#include < gtest/gtest.h>
5- #include < stdint .h>
5+ #include < opentelemetry/trace/propagation/b3_propagator .h>
66#include < string>
77
88#include " opentelemetry/nostd/string_view.h"
99
10- TEST (StringTest, SplitString)
10+ struct SplitStringTestData
1111{
12- // TODO(psx95): Add tests
13- }
12+ opentelemetry::nostd::string_view input;
13+ char separator;
14+ size_t max_count;
15+ std::vector<opentelemetry::nostd::string_view> splits;
16+ size_t expected_number_strings;
17+ };
18+
19+ const SplitStringTestData split_string_test_cases[] = {
20+ {" foo,bar,baz" , ' ,' , 4 ,
21+ std::vector<opentelemetry::nostd::string_view>{" foo" , " bar" , " baz" }, 3 },
22+ {" foo,bar,baz,foobar" , ' ,' , 4 ,
23+ std::vector<opentelemetry::nostd::string_view>{" foo" , " bar" , " baz" , " foobar" }, 4 },
24+ {" foo,bar,baz,foobar" , ' .' , 4 ,
25+ std::vector<opentelemetry::nostd::string_view>{" foo,bar,baz,foobar" }, 1 },
26+ {" foo,bar,baz," , ' ,' , 4 ,
27+ std::vector<opentelemetry::nostd::string_view>{" foo" ," bar" ," baz" ," " }, 4 },
28+ {" foo,bar,baz," , ' ,' , 2 ,
29+ std::vector<opentelemetry::nostd::string_view>{" foo" ," bar" }, 2 },
30+ {" foo ,bar, baz " , ' ,' , 4 ,
31+ std::vector<opentelemetry::nostd::string_view>{" foo " ," bar" ," baz " }, 3 },
32+ {" foo ,bar, baz " , ' ,' , 4 ,
33+ std::vector<opentelemetry::nostd::string_view>{" foo " ," bar" ," baz " }, 3 },
34+ {" 00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01" , ' -' , 4 ,
35+ std::vector<opentelemetry::nostd::string_view>{" 00" ," 0af7651916cd43dd8448eb211c80319c" ," 00f067aa0ba902b7" , " 01" }, 4 },
36+ };
37+
38+ // Test fixture
39+ class SplitStringTestFixture : public ::testing::TestWithParam<SplitStringTestData> {};
40+
41+ TEST_P (SplitStringTestFixture, SplitsAsExpected)
42+ {
43+ SplitStringTestData test_param = GetParam ();
44+ std::vector<opentelemetry::nostd::string_view> fields{};
45+ fields.reserve (test_param.expected_number_strings );
46+ size_t got_splits_num = opentelemetry::trace::propagation::detail::SplitString (test_param.input , test_param.separator , fields.data (), test_param.max_count );
47+
48+
49+ // Assert on the output
50+ EXPECT_EQ (got_splits_num, test_param.expected_number_strings );
51+ for (size_t i = 0 ; i < got_splits_num; i++)
52+ {
53+ // Checks for resulting strings in-order
54+ EXPECT_EQ (fields[i], test_param.splits [i]);
55+ }
56+ }
57+
58+ INSTANTIATE_TEST_SUITE_P (SplitStringTestCases, SplitStringTestFixture, ::testing::ValuesIn(split_string_test_cases));
0 commit comments