blob: eb8ac1703c1274b0f57d327dce7c3db5370eaf95 [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
Ed Tanous478b7ad2024-07-15 19:11:54 -07005#include <gtest/gtest.h>
Manojkiran Eda17a897d2020-09-12 15:31:58 +05306
Nan Zhou5b28cdf2022-06-21 17:46:26 +00007namespace dbus::utility
8{
9namespace
10{
11
Nan Zhou623e5332022-06-21 17:46:37 +000012TEST(GetNthStringFromPath, ParsingSucceedsAndReturnsNthArg)
Manojkiran Eda17a897d2020-09-12 15:31:58 +053013{
14 std::string path("/0th/1st/2nd/3rd");
15 std::string result;
Nan Zhou5b28cdf2022-06-21 17:46:26 +000016 EXPECT_TRUE(getNthStringFromPath(path, 0, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053017 EXPECT_EQ(result, "0th");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000018 EXPECT_TRUE(getNthStringFromPath(path, 1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053019 EXPECT_EQ(result, "1st");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000020 EXPECT_TRUE(getNthStringFromPath(path, 2, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053021 EXPECT_EQ(result, "2nd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000022 EXPECT_TRUE(getNthStringFromPath(path, 3, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053023 EXPECT_EQ(result, "3rd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000024 EXPECT_FALSE(getNthStringFromPath(path, 4, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053025
Nan Zhou623e5332022-06-21 17:46:37 +000026 path = "////0th///1st//\2nd///3rd?/";
Nan Zhou5b28cdf2022-06-21 17:46:26 +000027 EXPECT_TRUE(getNthStringFromPath(path, 0, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053028 EXPECT_EQ(result, "0th");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000029 EXPECT_TRUE(getNthStringFromPath(path, 1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053030 EXPECT_EQ(result, "1st");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000031 EXPECT_TRUE(getNthStringFromPath(path, 2, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053032 EXPECT_EQ(result, "\2nd");
Nan Zhou5b28cdf2022-06-21 17:46:26 +000033 EXPECT_TRUE(getNthStringFromPath(path, 3, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053034 EXPECT_EQ(result, "3rd?");
Nan Zhou623e5332022-06-21 17:46:37 +000035}
36
37TEST(GetNthStringFromPath, InvalidIndexReturnsFalse)
38{
39 std::string path("////0th///1st//\2nd///3rd?/");
40 std::string result;
Nan Zhou5b28cdf2022-06-21 17:46:26 +000041 EXPECT_FALSE(getNthStringFromPath(path, -1, result));
Manojkiran Eda17a897d2020-09-12 15:31:58 +053042}
Nan Zhou5b28cdf2022-06-21 17:46:26 +000043} // namespace
Patrick Williams89492a12023-05-10 07:51:34 -050044} // namespace dbus::utility