]>
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 | TEST(GlobalErrorTest,UTF8Message) | |
121 | { | |
122 | GlobalError e; | |
123 | std::string text; | |
124 | ||
125 | EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test")); | |
126 | EXPECT_FALSE(e.PopMessage(text)); | |
127 | EXPECT_EQ("Репозиторий не обновлён и будут 4 test", text); | |
128 | ||
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); | |
135 | } |