blob: 71978d01ba17530962782b1a62d99eae0ed202bc [file] [log] [blame]
Nan Zhou2f07bde2022-06-21 17:46:31 +00001#include "dbus_utility.hpp"
Manojkiran Eda17a897d2020-09-12 15:31:58 +05302
Nan Zhou2f07bde2022-06-21 17:46:31 +00003#include <string>
4
Nan Zhoud5c80ad2022-07-11 01:16:31 +00005#include <gtest/gtest.h> // IWYU pragma: keep
6
7// IWYU pragma: no_include <gtest/gtest-message.h>
8// IWYU pragma: no_include <gtest/gtest-test-part.h>
9// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
Manojkiran Eda17a897d2020-09-12 15:31:58 +053010
Nan Zhou5b28cdf2022-06-21 17:46:26 +000011namespace dbus::utility
12{
13namespace
14{
15
Nan Zhou623e5332022-06-21 17:46:37 +000016TEST(GetNthStringFromPath, ParsingSucceedsAndReturnsNthArg)
Manojkiran Eda17a897d2020-09-12 15:31:58 +053017{
18 std::string path("/0th/1st/2nd/3rd");
19 std::string result;
Nan Zhou5b28cdf2022-06-21 17:46:26 +000020 EXPECT_TRUE(getNthStringFromPath(path, 0, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053021 EXPECT_EQ(result, "0th");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000022 EXPECT_TRUE(getNthStringFromPath(path, 1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053023 EXPECT_EQ(result, "1st");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000024 EXPECT_TRUE(getNthStringFromPath(path, 2, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053025 EXPECT_EQ(result, "2nd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000026 EXPECT_TRUE(getNthStringFromPath(path, 3, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053027 EXPECT_EQ(result, "3rd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000028 EXPECT_FALSE(getNthStringFromPath(path, 4, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053029
Nan Zhou623e5332022-06-21 17:46:37 +000030 path = "////0th///1st//\2nd///3rd?/";
Nan Zhou5b28cdf2022-06-21 17:46:26 +000031 EXPECT_TRUE(getNthStringFromPath(path, 0, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053032 EXPECT_EQ(result, "0th");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000033 EXPECT_TRUE(getNthStringFromPath(path, 1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053034 EXPECT_EQ(result, "1st");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000035 EXPECT_TRUE(getNthStringFromPath(path, 2, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053036 EXPECT_EQ(result, "\2nd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000037 EXPECT_TRUE(getNthStringFromPath(path, 3, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053038 EXPECT_EQ(result, "3rd?");
Nan Zhou623e5332022-06-21 17:46:37 +000039}
40
41TEST(GetNthStringFromPath, InvalidIndexReturnsFalse)
42{
43 std::string path("////0th///1st//\2nd///3rd?/");
44 std::string result;
Nan Zhou5b28cdf2022-06-21 17:46:26 +000045 EXPECT_FALSE(getNthStringFromPath(path, -1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053046}
Nan Zhou5b28cdf2022-06-21 17:46:26 +000047} // namespace
48} // namespace dbus::utility