]> git.saurik.com Git - apt.git/blob - test/libapt/strutil_test.cc
Merge branch 'debian/sid' into debian/experimental
[apt.git] / test / libapt / strutil_test.cc
1 #include <config.h>
2 #include <apt-pkg/strutl.h>
3 #include <string>
4 #include <vector>
5
6 #include <gtest/gtest.h>
7
8 TEST(StrUtilTest,DeEscapeString)
9 {
10 // nothing special
11 EXPECT_EQ("", DeEscapeString(""));
12 EXPECT_EQ("foobar", DeEscapeString("foobar"));
13 // hex and octal
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"));
18 // double slashes
19 EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x"));
20 EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\"));
21 }
22 TEST(StrUtilTest,StringStrip)
23 {
24 EXPECT_EQ("", APT::String::Strip(""));
25 EXPECT_EQ("foobar", APT::String::Strip("foobar"));
26 EXPECT_EQ("foo bar", APT::String::Strip("foo bar"));
27
28 EXPECT_EQ("", APT::String::Strip(" "));
29 EXPECT_EQ("", APT::String::Strip(" \r\n \t "));
30
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 "));
36 }
37 TEST(StrUtilTest,StringSplitBasic)
38 {
39 std::vector<std::string> result = StringSplit("", "");
40 EXPECT_EQ(result.size(), 0);
41
42 result = StringSplit("abc", "");
43 EXPECT_EQ(result.size(), 0);
44
45 result = StringSplit("", "abc");
46 EXPECT_EQ(result.size(), 1);
47
48 result = StringSplit("abc", "b");
49 ASSERT_EQ(result.size(), 2);
50 EXPECT_EQ(result[0], "a");
51 EXPECT_EQ(result[1], "c");
52
53 result = StringSplit("abc", "abc");
54 ASSERT_EQ(result.size(), 2);
55 EXPECT_EQ(result[0], "");
56 EXPECT_EQ(result[1], "");
57 }
58 TEST(StrUtilTest,StringSplitDpkgStatus)
59 {
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);
64
65 result = StringSplit(input, "");
66 EXPECT_EQ(result.size(), 0);
67
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");
73
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");
78 }
79 TEST(StrUtilTest,EndsWith)
80 {
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"));
87 }
88 TEST(StrUtilTest,StartWith)
89 {
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"));
96 }
97 TEST(StrUtilTest,SubstVar)
98 {
99 EXPECT_EQ("", SubstVar("", "fails", "passes"));
100 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
101 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
102
103 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
104 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
105
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"));
112
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"));
117
118 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
119 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
120 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
121
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 "));
126
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"));
130
131 }