]>
git.saurik.com Git - apt.git/blob - test/libapt/strutil_test.cc
2 #include <apt-pkg/strutl.h>
6 #include <gtest/gtest.h>
8 TEST(StrUtilTest
,DeEscapeString
)
11 EXPECT_EQ("", DeEscapeString(""));
12 EXPECT_EQ("foobar", DeEscapeString("foobar"));
14 EXPECT_EQ("foo bar\nbaz", DeEscapeString("foo\\040bar\\x0abaz"));
15 EXPECT_EQ("foo ", DeEscapeString("foo\\040"));
16 EXPECT_EQ("\nbaz", DeEscapeString("\\x0abaz"));
17 EXPECT_EQ("/media/Ubuntu 11.04 amd64", DeEscapeString("/media/Ubuntu\\04011.04\\040amd64"));
19 EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x"));
20 EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\"));
22 TEST(StrUtilTest
,StringStrip
)
24 EXPECT_EQ("", APT::String::Strip(""));
25 EXPECT_EQ("foobar", APT::String::Strip("foobar"));
26 EXPECT_EQ("foo bar", APT::String::Strip("foo bar"));
28 EXPECT_EQ("", APT::String::Strip(" "));
29 EXPECT_EQ("", APT::String::Strip(" \r\n \t "));
31 EXPECT_EQ("foo bar", APT::String::Strip("foo bar "));
32 EXPECT_EQ("foo bar", APT::String::Strip("foo bar \r\n \t "));
33 EXPECT_EQ("foo bar", APT::String::Strip("\r\n \t foo bar"));
34 EXPECT_EQ("bar foo", APT::String::Strip("\r\n \t bar foo \r\n \t "));
35 EXPECT_EQ("bar \t\r\n foo", APT::String::Strip("\r\n \t bar \t\r\n foo \r\n \t "));
37 TEST(StrUtilTest
,StringSplitBasic
)
39 std::vector
<std::string
> result
= StringSplit("", "");
40 EXPECT_EQ(result
.size(), 0);
42 result
= StringSplit("abc", "");
43 EXPECT_EQ(result
.size(), 0);
45 result
= StringSplit("", "abc");
46 EXPECT_EQ(result
.size(), 1);
48 result
= StringSplit("abc", "b");
49 ASSERT_EQ(result
.size(), 2);
50 EXPECT_EQ(result
[0], "a");
51 EXPECT_EQ(result
[1], "c");
53 result
= StringSplit("abc", "abc");
54 ASSERT_EQ(result
.size(), 2);
55 EXPECT_EQ(result
[0], "");
56 EXPECT_EQ(result
[1], "");
58 TEST(StrUtilTest
,StringSplitDpkgStatus
)
60 std::string
const input
= "status: libnet1:amd64: unpacked";
61 std::vector
<std::string
> result
= StringSplit(input
, "xxx");
62 ASSERT_EQ(result
.size(), 1);
63 EXPECT_EQ(result
[0], input
);
65 result
= StringSplit(input
, "");
66 EXPECT_EQ(result
.size(), 0);
68 result
= StringSplit(input
, ": ");
69 ASSERT_EQ(result
.size(), 3);
70 EXPECT_EQ(result
[0], "status");
71 EXPECT_EQ(result
[1], "libnet1:amd64");
72 EXPECT_EQ(result
[2], "unpacked");
74 result
= StringSplit("x:y:z", ":", 2);
75 ASSERT_EQ(result
.size(), 2);
76 EXPECT_EQ(result
[0], "x");
77 EXPECT_EQ(result
[1], "y:z");
79 TEST(StrUtilTest
,EndsWith
)
81 using APT::String::Endswith
;
82 EXPECT_TRUE(Endswith("abcd", "d"));
83 EXPECT_TRUE(Endswith("abcd", "cd"));
84 EXPECT_TRUE(Endswith("abcd", "abcd"));
85 EXPECT_FALSE(Endswith("abcd", "x"));
86 EXPECT_FALSE(Endswith("abcd", "abcndefg"));
88 TEST(StrUtilTest
,StartWith
)
90 using APT::String::Startswith
;
91 EXPECT_TRUE(Startswith("abcd", "a"));
92 EXPECT_TRUE(Startswith("abcd", "ab"));
93 EXPECT_TRUE(Startswith("abcd", "abcd"));
94 EXPECT_FALSE(Startswith("abcd", "x"));
95 EXPECT_FALSE(Startswith("abcd", "abcndefg"));
97 TEST(StrUtilTest
,SubstVar
)
99 EXPECT_EQ("", SubstVar("", "fails", "passes"));
100 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
101 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
103 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
104 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
106 EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts"));
107 EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt"));
108 EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in"));
109 EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der"));
110 EXPECT_EQ("does end", SubstVar("does enden", "enden", "end"));
111 EXPECT_EQ("does enden", SubstVar("does end", "end", "enden"));
113 EXPECT_EQ("abc", SubstVar("abc", "d", "a"));
114 EXPECT_EQ("abc", SubstVar("abd", "d", "c"));
115 EXPECT_EQ("abc", SubstVar("adc", "d", "b"));
116 EXPECT_EQ("abc", SubstVar("dbc", "d", "a"));
118 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
119 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
120 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
122 EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a"));
123 EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa"));
124 EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a"));
125 EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a "));
127 EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb"));
128 EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb"));
129 EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb"));