]>
Commit | Line | Data |
---|---|---|
1 | #include <config.h> | |
2 | ||
3 | #include <apt-pkg/error.h> | |
4 | ||
5 | #include <stddef.h> | |
6 | #include <string> | |
7 | #include <errno.h> | |
8 | #include <string.h> | |
9 | ||
10 | #include <gtest/gtest.h> | |
11 | ||
12 | TEST(GlobalErrorTest,BasicDiscard) | |
13 | { | |
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()); | |
23 | ||
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(); | |
36 | ||
37 | EXPECT_TRUE(e.empty()); | |
38 | EXPECT_FALSE(e.PendingError()); | |
39 | } | |
40 | TEST(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()); | |
56 | ||
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()); | |
65 | ||
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 | } | |
90 | TEST(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 | } | |
104 | TEST(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); | |
115 | ||
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 | ||
120 | EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2)); | |
121 | std::ostringstream out; | |
122 | e.DumpErrors(out); | |
123 | EXPECT_EQ(std::string("E: ").append(longText).append(" horrible happened 2 times\n"), out.str()); | |
124 | ||
125 | EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", longText.c_str(), "happened", 2)); | |
126 | std::ostringstream out2; | |
127 | e.DumpErrors(out2); | |
128 | EXPECT_EQ(std::string("E: ").append(longText).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")\n"), out2.str()); | |
129 | } | |
130 | TEST(GlobalErrorTest,UTF8Message) | |
131 | { | |
132 | GlobalError e; | |
133 | std::string text; | |
134 | ||
135 | EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test")); | |
136 | EXPECT_FALSE(e.PopMessage(text)); | |
137 | EXPECT_EQ("Репозиторий не обновлён и будут 4 test", text); | |
138 | ||
139 | EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test")); | |
140 | std::ostringstream out; | |
141 | e.DumpErrors(out); | |
142 | EXPECT_EQ("W: Репозиторий не обновлён и будут 4 test\n", out.str()); | |
143 | ||
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); | |
150 | } | |
151 | TEST(GlobalErrorTest,MultiLineMessage) | |
152 | { | |
153 | GlobalError e; | |
154 | std::string text; | |
155 | ||
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); | |
159 | ||
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; | |
162 | e.DumpErrors(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()); | |
164 | ||
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; | |
167 | e.DumpErrors(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()); | |
169 | } |