1 | /* Copyright © 2006-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <sbuild/sbuild-log.h> |
---|
20 | |
---|
21 | #include <iostream> |
---|
22 | #include <ios> |
---|
23 | #include <sstream> |
---|
24 | |
---|
25 | #include <cppunit/extensions/HelperMacros.h> |
---|
26 | |
---|
27 | using namespace CppUnit; |
---|
28 | |
---|
29 | class test_log : public TestFixture |
---|
30 | { |
---|
31 | CPPUNIT_TEST_SUITE(test_log); |
---|
32 | CPPUNIT_TEST(test_info); |
---|
33 | CPPUNIT_TEST(test_warning); |
---|
34 | CPPUNIT_TEST(test_error); |
---|
35 | CPPUNIT_TEST(test_debug_none); |
---|
36 | CPPUNIT_TEST(test_debug_notice); |
---|
37 | CPPUNIT_TEST(test_debug_info); |
---|
38 | CPPUNIT_TEST(test_debug_warning); |
---|
39 | CPPUNIT_TEST(test_debug_critical); |
---|
40 | CPPUNIT_TEST_SUITE_END(); |
---|
41 | |
---|
42 | std::streambuf *saved; |
---|
43 | std::stringbuf *monitor; |
---|
44 | |
---|
45 | public: |
---|
46 | test_log() |
---|
47 | {} |
---|
48 | |
---|
49 | void setUp() |
---|
50 | { |
---|
51 | this->monitor = new std::stringbuf(); |
---|
52 | this->saved = std::cerr.std::ios::rdbuf(this->monitor); |
---|
53 | } |
---|
54 | |
---|
55 | void tearDown() |
---|
56 | { |
---|
57 | std::cerr.std::ios::rdbuf(this->saved); |
---|
58 | delete this->monitor; |
---|
59 | } |
---|
60 | |
---|
61 | void test_info() |
---|
62 | { |
---|
63 | sbuild::log_info() << "Discard me please"; |
---|
64 | CPPUNIT_ASSERT(this->monitor->str() == "I: Discard me please"); |
---|
65 | } |
---|
66 | |
---|
67 | void test_warning() |
---|
68 | { |
---|
69 | sbuild::log_warning() << "Discard me please"; |
---|
70 | CPPUNIT_ASSERT(this->monitor->str() == "W: Discard me please"); |
---|
71 | } |
---|
72 | |
---|
73 | void test_error() |
---|
74 | { |
---|
75 | sbuild::log_error() << "Discard me please"; |
---|
76 | CPPUNIT_ASSERT(this->monitor->str() == "E: Discard me please"); |
---|
77 | } |
---|
78 | |
---|
79 | std::string |
---|
80 | debug(sbuild::debug_level level, |
---|
81 | std::string const& msg) |
---|
82 | { |
---|
83 | this->monitor->str(""); |
---|
84 | sbuild::log_debug(level) << msg; |
---|
85 | return this->monitor->str(); |
---|
86 | } |
---|
87 | |
---|
88 | void test_debug_none() |
---|
89 | { |
---|
90 | sbuild::debug_log_level = sbuild::DEBUG_NONE; |
---|
91 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE, "Discard me") == ""); |
---|
92 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE, "Discard me") == ""); |
---|
93 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO, "Discard me") == ""); |
---|
94 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING, "Discard me") == ""); |
---|
95 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, "Discard me") == ""); |
---|
96 | } |
---|
97 | |
---|
98 | void test_debug_notice() |
---|
99 | { |
---|
100 | sbuild::debug_log_level = sbuild::DEBUG_NOTICE; |
---|
101 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE, |
---|
102 | "Discard me") == ""); |
---|
103 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE, |
---|
104 | "Discard me") == "D(1): Discard me"); |
---|
105 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO, |
---|
106 | "Discard me") == "D(2): Discard me"); |
---|
107 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING, |
---|
108 | "Discard me") == "D(3): Discard me"); |
---|
109 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, |
---|
110 | "Discard me") == "D(4): Discard me"); |
---|
111 | } |
---|
112 | |
---|
113 | void test_debug_info() |
---|
114 | { |
---|
115 | sbuild::debug_log_level = sbuild::DEBUG_INFO; |
---|
116 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE, |
---|
117 | "Discard me") == ""); |
---|
118 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE, |
---|
119 | "Discard me") == ""); |
---|
120 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO, |
---|
121 | "Discard me") == "D(2): Discard me"); |
---|
122 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING, |
---|
123 | "Discard me") == "D(3): Discard me"); |
---|
124 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, |
---|
125 | "Discard me") == "D(4): Discard me"); |
---|
126 | } |
---|
127 | |
---|
128 | void test_debug_warning() |
---|
129 | { |
---|
130 | sbuild::debug_log_level = sbuild::DEBUG_WARNING; |
---|
131 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE, |
---|
132 | "Discard me") == ""); |
---|
133 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE, |
---|
134 | "Discard me") == ""); |
---|
135 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO, |
---|
136 | "Discard me") == ""); |
---|
137 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING, |
---|
138 | "Discard me") == "D(3): Discard me"); |
---|
139 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, |
---|
140 | "Discard me") == "D(4): Discard me"); |
---|
141 | } |
---|
142 | |
---|
143 | void test_debug_critical() |
---|
144 | { |
---|
145 | sbuild::debug_log_level = sbuild::DEBUG_CRITICAL; |
---|
146 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE, |
---|
147 | "Discard me") == ""); |
---|
148 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE, |
---|
149 | "Discard me") == ""); |
---|
150 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO, |
---|
151 | "Discard me") == ""); |
---|
152 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING, |
---|
153 | "Discard me") == ""); |
---|
154 | CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, |
---|
155 | "Discard me") == "D(4): Discard me"); |
---|
156 | } |
---|
157 | }; |
---|
158 | |
---|
159 | CPPUNIT_TEST_SUITE_REGISTRATION(test_log); |
---|