summaryrefslogtreecommitdiff
path: root/test/sbuild-log.cc
blob: b41188d5ab4da0006589db1b0289e6d0ef9be20c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Copyright © 2006  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 *********************************************************************/

#include <sbuild/sbuild-log.h>

#include <iostream>
#include <ios>
#include <sstream>

#include <cppunit/extensions/HelperMacros.h>

using namespace CppUnit;

class test_log : public TestFixture
{
  CPPUNIT_TEST_SUITE(test_log);
  CPPUNIT_TEST(test_info);
  CPPUNIT_TEST(test_warning);
  CPPUNIT_TEST(test_error);
  CPPUNIT_TEST(test_debug_none);
  CPPUNIT_TEST(test_debug_notice);
  CPPUNIT_TEST(test_debug_info);
  CPPUNIT_TEST(test_debug_warning);
  CPPUNIT_TEST(test_debug_critical);
  CPPUNIT_TEST_SUITE_END();

  std::streambuf *saved;
  std::stringbuf *monitor;

public:
  test_log()
  {}

  void setUp()
  {
    this->monitor = new std::stringbuf();
    this->saved = std::cerr.std::ios::rdbuf(this->monitor);
  }

  void tearDown()
  {
    std::cerr.std::ios::rdbuf(this->saved);
    delete this->monitor;
  }

  void test_info()
  {
    sbuild::log_info() << "Discard me please";
    CPPUNIT_ASSERT(this->monitor->str() == "I: Discard me please");
  }

  void test_warning()
  {
    sbuild::log_warning() << "Discard me please";
    CPPUNIT_ASSERT(this->monitor->str() == "W: Discard me please");
  }

  void test_error()
  {
    sbuild::log_error() << "Discard me please";
    CPPUNIT_ASSERT(this->monitor->str() == "E: Discard me please");
  }

  std::string
  debug(sbuild::DebugLevel level,
	     std::string const& msg)
  {
    this->monitor->str("");
    sbuild::log_debug(level) << msg;
    return this->monitor->str();
  }

  void test_debug_none()
  {
    sbuild::debug_level = sbuild::DEBUG_NONE;
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE,     "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE,   "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO,     "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING,  "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL, "Discard me") == "");
  }

  void test_debug_notice()
  {
    sbuild::debug_level = sbuild::DEBUG_NOTICE;
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE,
			 "Discard me") == "D(1): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO,
			 "Discard me") == "D(2): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING,
			 "Discard me") == "D(3): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL,
			 "Discard me") == "D(4): Discard me");
  }

  void test_debug_info()
  {
    sbuild::debug_level = sbuild::DEBUG_INFO;
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO,
			 "Discard me") == "D(2): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING,
			 "Discard me") == "D(3): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL,
			 "Discard me") == "D(4): Discard me");
  }

  void test_debug_warning()
  {
    sbuild::debug_level = sbuild::DEBUG_WARNING;
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING,
			 "Discard me") == "D(3): Discard me");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL,
			 "Discard me") == "D(4): Discard me");
  }

  void test_debug_critical()
  {
    sbuild::debug_level = sbuild::DEBUG_CRITICAL;
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NONE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_NOTICE,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_INFO,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_WARNING,
			 "Discard me") == "");
    CPPUNIT_ASSERT(debug(sbuild::DEBUG_CRITICAL,
			 "Discard me") == "D(4): Discard me");
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(test_log);