]> git.saurik.com Git - apt.git/blame - test/libapt/strutil_test.cc
Merge remote-tracking branch 'mvo/feature/README' into debian/sid
[apt.git] / test / libapt / strutil_test.cc
CommitLineData
453b82a3 1#include <config.h>
a513ace2 2#include <apt-pkg/strutl.h>
453b82a3
DK
3#include <string>
4#include <vector>
5
f00832cc 6#include <gtest/gtest.h>
a513ace2 7
f00832cc 8TEST(StrUtilTest,DeEscapeString)
a513ace2 9{
f00832cc
DK
10 // nothing special
11 EXPECT_EQ("", DeEscapeString(""));
12 EXPECT_EQ("foobar", DeEscapeString("foobar"));
a513ace2 13 // hex and octal
f00832cc
DK
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}
22TEST(StrUtilTest,StringSplitBasic)
23{
24 std::vector<std::string> result = StringSplit("", "");
25 EXPECT_EQ(result.size(), 0);
b9dc4706 26
f00832cc
DK
27 result = StringSplit("abc", "");
28 EXPECT_EQ(result.size(), 0);
00f4d9ff 29
f00832cc
DK
30 result = StringSplit("", "abc");
31 EXPECT_EQ(result.size(), 1);
00f4d9ff 32
f00832cc
DK
33 result = StringSplit("abc", "b");
34 ASSERT_EQ(result.size(), 2);
35 EXPECT_EQ(result[0], "a");
36 EXPECT_EQ(result[1], "c");
00f4d9ff 37
f00832cc
DK
38 result = StringSplit("abc", "abc");
39 ASSERT_EQ(result.size(), 2);
40 EXPECT_EQ(result[0], "");
41 EXPECT_EQ(result[1], "");
42}
43TEST(StrUtilTest,StringSplitDpkgStatus)
44{
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);
85bf0019 49
2ddab3fb 50 result = StringSplit(input, "");
f00832cc
DK
51 EXPECT_EQ(result.size(), 0);
52
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");
58
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");
63}
64TEST(StrUtilTest,EndsWith)
65{
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"));
a513ace2 72}
224dc038
DK
73TEST(StrUtilTest,SubstVar)
74{
75 EXPECT_EQ("", SubstVar("", "fails", "passes"));
76 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
77 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
78
79 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
80 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
81
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"));
88
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"));
93
94 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
95 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
96 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
97
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 "));
102
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"));
106
107}