]> git.saurik.com Git - apt.git/blame - test/libapt/globalerror_test.cc
cachefile: Only set members that were initialized successfully
[apt.git] / test / libapt / globalerror_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
c4ba7c44
DK
3#include <apt-pkg/error.h>
4
453b82a3 5#include <stddef.h>
c4ba7c44 6#include <string>
38f29703 7#include <errno.h>
18aea9e6 8#include <string.h>
c4ba7c44 9
f00832cc 10#include <gtest/gtest.h>
453b82a3 11
f00832cc 12TEST(GlobalErrorTest,BasicDiscard)
c4ba7c44 13{
f00832cc
DK
14 GlobalError e;
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());
18aea9e6 23
f00832cc
DK
24 std::string text;
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));
35 e.Discard();
c4ba7c44 36
f00832cc
DK
37 EXPECT_TRUE(e.empty());
38 EXPECT_FALSE(e.PendingError());
39}
40TEST(GlobalErrorTest,StackPushing)
41{
42 GlobalError e;
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));
47 e.PushToStack();
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());
53 e.RevertToStack();
54 EXPECT_FALSE(e.empty(GlobalError::ERROR));
55 EXPECT_TRUE(e.PendingError());
38f29703 56
f00832cc
DK
57 std::string text;
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());
38f29703 65
f00832cc
DK
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));
70 e.PushToStack();
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());
76 e.MergeWithStack();
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());
89}
90TEST(GlobalErrorTest,Errno)
91{
92 GlobalError e;
93 std::string const textOfErrnoZero(strerror(0));
94 errno = 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());
98 std::string text;
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());
103}
104TEST(GlobalErrorTest,LongMessage)
105{
106 GlobalError e;
107 std::string const textOfErrnoZero(strerror(0));
108 errno = 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);
38f29703 115
f00832cc
DK
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);
119}
120TEST(GlobalErrorTest,UTF8Message)
121{
122 GlobalError e;
123 std::string text;
38f29703 124
f00832cc
DK
125 EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test"));
126 EXPECT_FALSE(e.PopMessage(text));
127 EXPECT_EQ("Репозиторий не обновлён и будут 4 test", text);
38f29703 128
f00832cc
DK
129 std::string longText;
130 for (size_t i = 0; i < 50; ++i)
131 longText.append("РезийбёбAZ");
132 EXPECT_FALSE(e.Warning("%s", longText.c_str()));
133 EXPECT_FALSE(e.PopMessage(text));
134 EXPECT_EQ(longText, text);
c4ba7c44 135}