]>
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
,StringSplitBasic
)
24 std::vector
<std::string
> result
= StringSplit("", "");
25 EXPECT_EQ(result
.size(), 0);
27 result
= StringSplit("abc", "");
28 EXPECT_EQ(result
.size(), 0);
30 result
= StringSplit("", "abc");
31 EXPECT_EQ(result
.size(), 1);
33 result
= StringSplit("abc", "b");
34 ASSERT_EQ(result
.size(), 2);
35 EXPECT_EQ(result
[0], "a");
36 EXPECT_EQ(result
[1], "c");
38 result
= StringSplit("abc", "abc");
39 ASSERT_EQ(result
.size(), 2);
40 EXPECT_EQ(result
[0], "");
41 EXPECT_EQ(result
[1], "");
43 TEST(StrUtilTest
,StringSplitDpkgStatus
)
45 std::string
const input
= "status: libnet1:amd64: unpacked";
46 std::vector
<std::string
> result
= StringSplit(input
, "xxx");
47 ASSERT_EQ(result
.size(), 1);
48 EXPECT_EQ(result
[0], input
);
50 result
= StringSplit(input
, "");
51 EXPECT_EQ(result
.size(), 0);
53 result
= StringSplit(input
, ": ");
54 ASSERT_EQ(result
.size(), 3);
55 EXPECT_EQ(result
[0], "status");
56 EXPECT_EQ(result
[1], "libnet1:amd64");
57 EXPECT_EQ(result
[2], "unpacked");
59 result
= StringSplit("x:y:z", ":", 2);
60 ASSERT_EQ(result
.size(), 2);
61 EXPECT_EQ(result
[0], "x");
62 EXPECT_EQ(result
[1], "y:z");
64 TEST(StrUtilTest
,EndsWith
)
66 using APT::String::Endswith
;
67 EXPECT_TRUE(Endswith("abcd", "d"));
68 EXPECT_TRUE(Endswith("abcd", "cd"));
69 EXPECT_TRUE(Endswith("abcd", "abcd"));
70 EXPECT_FALSE(Endswith("abcd", "x"));
71 EXPECT_FALSE(Endswith("abcd", "abcndefg"));
73 TEST(StrUtilTest
,SubstVar
)
75 EXPECT_EQ("", SubstVar("", "fails", "passes"));
76 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
77 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
79 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
80 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
82 EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts"));
83 EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt"));
84 EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in"));
85 EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der"));
86 EXPECT_EQ("does end", SubstVar("does enden", "enden", "end"));
87 EXPECT_EQ("does enden", SubstVar("does end", "end", "enden"));
89 EXPECT_EQ("abc", SubstVar("abc", "d", "a"));
90 EXPECT_EQ("abc", SubstVar("abd", "d", "c"));
91 EXPECT_EQ("abc", SubstVar("adc", "d", "b"));
92 EXPECT_EQ("abc", SubstVar("dbc", "d", "a"));
94 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
95 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
96 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
98 EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a"));
99 EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa"));
100 EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a"));
101 EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a "));
103 EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb"));
104 EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb"));
105 EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb"));