]>
git.saurik.com Git - apt.git/blob - test/libapt/globalerror_test.cc
3 #include <apt-pkg/error.h>
10 #include <gtest/gtest.h>
12 TEST(GlobalErrorTest
,BasicDiscard
)
15 EXPECT_TRUE(e
.empty());
16 EXPECT_FALSE(e
.PendingError());
17 EXPECT_FALSE(e
.Notice("%s Notice", "A"));
18 EXPECT_TRUE(e
.empty());
19 EXPECT_FALSE(e
.empty(GlobalError::DEBUG
));
20 EXPECT_FALSE(e
.PendingError());
21 EXPECT_FALSE(e
.Error("%s horrible %s %d times", "Something", "happened", 2));
22 EXPECT_TRUE(e
.PendingError());
25 EXPECT_FALSE(e
.PopMessage(text
));
26 EXPECT_TRUE(e
.PendingError());
27 EXPECT_EQ("A Notice", text
);
28 EXPECT_TRUE(e
.PopMessage(text
));
29 EXPECT_EQ("Something horrible happened 2 times", text
);
30 EXPECT_TRUE(e
.empty(GlobalError::DEBUG
));
31 EXPECT_FALSE(e
.PendingError());
32 EXPECT_FALSE(e
.Error("%s horrible %s %d times", "Something", "happened", 2));
33 EXPECT_TRUE(e
.PendingError());
34 EXPECT_FALSE(e
.empty(GlobalError::FATAL
));
37 EXPECT_TRUE(e
.empty());
38 EXPECT_FALSE(e
.PendingError());
40 TEST(GlobalErrorTest
,StackPushing
)
43 EXPECT_FALSE(e
.Notice("%s Notice", "A"));
44 EXPECT_FALSE(e
.Error("%s horrible %s %d times", "Something", "happened", 2));
45 EXPECT_TRUE(e
.PendingError());
46 EXPECT_FALSE(e
.empty(GlobalError::NOTICE
));
48 EXPECT_TRUE(e
.empty(GlobalError::NOTICE
));
49 EXPECT_FALSE(e
.PendingError());
50 EXPECT_FALSE(e
.Warning("%s Warning", "A"));
51 EXPECT_TRUE(e
.empty(GlobalError::ERROR
));
52 EXPECT_FALSE(e
.PendingError());
54 EXPECT_FALSE(e
.empty(GlobalError::ERROR
));
55 EXPECT_TRUE(e
.PendingError());
58 EXPECT_FALSE(e
.PopMessage(text
));
59 EXPECT_TRUE(e
.PendingError());
60 EXPECT_EQ("A Notice", text
);
61 EXPECT_TRUE(e
.PopMessage(text
));
62 EXPECT_EQ("Something horrible happened 2 times", text
);
63 EXPECT_FALSE(e
.PendingError());
64 EXPECT_TRUE(e
.empty());
66 EXPECT_FALSE(e
.Notice("%s Notice", "A"));
67 EXPECT_FALSE(e
.Error("%s horrible %s %d times", "Something", "happened", 2));
68 EXPECT_TRUE(e
.PendingError());
69 EXPECT_FALSE(e
.empty(GlobalError::NOTICE
));
71 EXPECT_TRUE(e
.empty(GlobalError::NOTICE
));
72 EXPECT_FALSE(e
.PendingError());
73 EXPECT_FALSE(e
.Warning("%s Warning", "A"));
74 EXPECT_TRUE(e
.empty(GlobalError::ERROR
));
75 EXPECT_FALSE(e
.PendingError());
77 EXPECT_FALSE(e
.empty(GlobalError::ERROR
));
78 EXPECT_TRUE(e
.PendingError());
79 EXPECT_FALSE(e
.PopMessage(text
));
80 EXPECT_TRUE(e
.PendingError());
81 EXPECT_EQ("A Notice", text
);
82 EXPECT_TRUE(e
.PopMessage(text
));
83 EXPECT_EQ("Something horrible happened 2 times", text
);
84 EXPECT_FALSE(e
.PendingError());
85 EXPECT_FALSE(e
.empty());
86 EXPECT_FALSE(e
.PopMessage(text
));
87 EXPECT_EQ("A Warning", text
);
88 EXPECT_TRUE(e
.empty());
90 TEST(GlobalErrorTest
,Errno
)
93 std::string
const textOfErrnoZero(strerror(0));
95 EXPECT_FALSE(e
.Errno("errno", "%s horrible %s %d times", "Something", "happened", 2));
96 EXPECT_FALSE(e
.empty());
97 EXPECT_TRUE(e
.PendingError());
99 EXPECT_TRUE(e
.PopMessage(text
));
100 EXPECT_FALSE(e
.PendingError());
101 EXPECT_EQ(std::string("Something horrible happened 2 times - errno (0: ").append(textOfErrnoZero
).append(")"), text
);
102 EXPECT_TRUE(e
.empty());
104 TEST(GlobalErrorTest
,LongMessage
)
107 std::string
const textOfErrnoZero(strerror(0));
109 std::string text
, longText
;
110 for (size_t i
= 0; i
< 500; ++i
)
111 longText
.append("a");
112 EXPECT_FALSE(e
.Error("%s horrible %s %d times", longText
.c_str(), "happened", 2));
113 EXPECT_TRUE(e
.PopMessage(text
));
114 EXPECT_EQ(std::string(longText
).append(" horrible happened 2 times"), text
);
116 EXPECT_FALSE(e
.Errno("errno", "%s horrible %s %d times", longText
.c_str(), "happened", 2));
117 EXPECT_TRUE(e
.PopMessage(text
));
118 EXPECT_EQ(std::string(longText
).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero
).append(")"), text
);
120 EXPECT_FALSE(e
.Error("%s horrible %s %d times", longText
.c_str(), "happened", 2));
121 std::ostringstream out
;
123 EXPECT_EQ(std::string("E: ").append(longText
).append(" horrible happened 2 times\n"), out
.str());
125 EXPECT_FALSE(e
.Errno("errno", "%s horrible %s %d times", longText
.c_str(), "happened", 2));
126 std::ostringstream out2
;
128 EXPECT_EQ(std::string("E: ").append(longText
).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero
).append(")\n"), out2
.str());
130 TEST(GlobalErrorTest
,UTF8Message
)
135 EXPECT_FALSE(e
.Warning("Репозиторий не обновлён и будут %d %s", 4, "test"));
136 EXPECT_FALSE(e
.PopMessage(text
));
137 EXPECT_EQ("Репозиторий не обновлён и будут 4 test", text
);
139 EXPECT_FALSE(e
.Warning("Репозиторий не обновлён и будут %d %s", 4, "test"));
140 std::ostringstream out
;
142 EXPECT_EQ("W: Репозиторий не обновлён и будут 4 test\n", out
.str());
144 std::string longText
;
145 for (size_t i
= 0; i
< 50; ++i
)
146 longText
.append("РезийбёбAZ");
147 EXPECT_FALSE(e
.Warning("%s", longText
.c_str()));
148 EXPECT_FALSE(e
.PopMessage(text
));
149 EXPECT_EQ(longText
, text
);
151 TEST(GlobalErrorTest
,MultiLineMessage
)
156 EXPECT_FALSE(e
.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.", "Right"));
157 EXPECT_FALSE(e
.PopMessage(text
));
158 EXPECT_EQ("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\nRight?\rGood because I don't.", text
);
160 EXPECT_FALSE(e
.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.", "Right"));
161 std::ostringstream out
;
163 EXPECT_EQ("W: Sometimes one line isn't enough.\n You do know what I mean, right?\n Right?\n Good because I don't.\n", out
.str());
165 EXPECT_FALSE(e
.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.\n", "Right"));
166 std::ostringstream out2
;
168 EXPECT_EQ("W: Sometimes one line isn't enough.\n You do know what I mean, right?\n Right?\n Good because I don't.\n", out2
.str());