Line data Source code
1 : /*
2 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * https://snapwebsites.org/project/snaplogger
5 : * contact@m2osw.com
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 2 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License along
18 : * with this program; if not, write to the Free Software Foundation, Inc.,
19 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : // self
23 : //
24 : #include "main.h"
25 :
26 :
27 : // snaplogger lib
28 : //
29 : #include <snaplogger/buffer_appender.h>
30 : #include <snaplogger/exception.h>
31 : #include <snaplogger/format.h>
32 : #include <snaplogger/logger.h>
33 : #include <snaplogger/map_diagnostic.h>
34 : #include <snaplogger/message.h>
35 : #include <snaplogger/severity.h>
36 : #include <snaplogger/version.h>
37 :
38 :
39 : // C lib
40 : //
41 : #include <unistd.h>
42 : #include <netdb.h>
43 : #include <sys/param.h>
44 :
45 :
46 :
47 :
48 4 : CATCH_TEST_CASE("severity", "[severity]")
49 : {
50 4 : CATCH_START_SECTION("Create Severity")
51 : {
52 1 : snaplogger::severity_t const err_plus_one(static_cast<snaplogger::severity_t>(static_cast<int>(snaplogger::severity_t::SEVERITY_ERROR) + 1));
53 :
54 : // user severity by name
55 : {
56 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(err_plus_one, "error"));
57 :
58 1 : CATCH_REQUIRE_THROWS_MATCHES(
59 : snaplogger::add_severity(s)
60 : , snaplogger::duplicate_error
61 : , Catch::Matchers::ExceptionMessage(
62 : "a system severity cannot be replaced."));
63 : }
64 :
65 : // system severity by name
66 : {
67 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(err_plus_one, "error", true));
68 :
69 1 : CATCH_REQUIRE_THROWS_MATCHES(
70 : snaplogger::add_severity(s)
71 : , snaplogger::duplicate_error
72 : , Catch::Matchers::ExceptionMessage(
73 : "a system severity cannot be replaced."));
74 : }
75 :
76 : // user severity by severity
77 : {
78 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(snaplogger::severity_t::SEVERITY_ERROR, "bad-error"));
79 :
80 1 : CATCH_REQUIRE_THROWS_MATCHES(
81 : snaplogger::add_severity(s)
82 : , snaplogger::duplicate_error
83 : , Catch::Matchers::ExceptionMessage(
84 : "a system severity cannot be replaced."));
85 : }
86 :
87 : // user severity by severity
88 : {
89 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(snaplogger::severity_t::SEVERITY_ERROR, "bad-error", true));
90 :
91 1 : CATCH_REQUIRE_THROWS_MATCHES(
92 : snaplogger::add_severity(s)
93 : , snaplogger::duplicate_error
94 : , Catch::Matchers::ExceptionMessage(
95 : "a system severity cannot be replaced."));
96 : }
97 :
98 : // actually create a valid severity
99 : {
100 1 : CATCH_REQUIRE(snaplogger::get_severity("bad-error") == nullptr);
101 1 : CATCH_REQUIRE(snaplogger::get_severity("big-error") == nullptr);
102 :
103 1 : snaplogger::severity_t const level(static_cast<snaplogger::severity_t>(205));
104 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(level, "bad-error"));
105 :
106 1 : CATCH_REQUIRE(s->get_severity() == level);
107 1 : CATCH_REQUIRE(s->get_name() == "bad-error");
108 :
109 1 : CATCH_REQUIRE(s->get_all_names().size() == 1);
110 1 : CATCH_REQUIRE(s->get_all_names()[0] == "bad-error");
111 :
112 1 : CATCH_REQUIRE(snaplogger::get_severity("bad-error") == nullptr);
113 1 : CATCH_REQUIRE(snaplogger::get_severity("big-error") == nullptr);
114 :
115 1 : snaplogger::add_severity(s);
116 :
117 1 : CATCH_REQUIRE(snaplogger::get_severity("bad-error") == s);
118 1 : CATCH_REQUIRE(snaplogger::get_severity("big-error") == nullptr);
119 :
120 1 : s->add_alias("big-error");
121 :
122 1 : CATCH_REQUIRE(s->get_all_names().size() == 2);
123 1 : CATCH_REQUIRE(s->get_all_names()[0] == "bad-error");
124 1 : CATCH_REQUIRE(s->get_all_names()[1] == "big-error");
125 :
126 1 : CATCH_REQUIRE(s->get_description() == "bad-error");
127 :
128 1 : s->set_description("bad error");
129 1 : CATCH_REQUIRE(s->get_description() == "bad error");
130 :
131 1 : s->set_description(std::string());
132 1 : CATCH_REQUIRE(s->get_description() == "bad-error");
133 :
134 1 : CATCH_REQUIRE(snaplogger::get_severity("bad-error") == s);
135 1 : CATCH_REQUIRE(snaplogger::get_severity("big-error") == s);
136 1 : CATCH_REQUIRE(snaplogger::get_severity(level) == s);
137 :
138 1 : s->set_styles("orange");
139 1 : CATCH_REQUIRE(s->get_styles() == "orange");
140 :
141 1 : snaplogger::severity_t const level_plus_one(static_cast<snaplogger::severity_t>(static_cast<int>(level) + 1));
142 1 : CATCH_REQUIRE(snaplogger::get_severity(level_plus_one) == nullptr);
143 :
144 2 : snaplogger::message msg(::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__);
145 1 : CATCH_REQUIRE(snaplogger::get_severity(msg, "bad-error") == s);
146 1 : CATCH_REQUIRE(snaplogger::get_severity(msg, "big-error") == s);
147 : }
148 :
149 : // actually create a valid severity
150 : {
151 1 : snaplogger::severity_t const level(static_cast<snaplogger::severity_t>(25));
152 2 : snaplogger::severity::pointer_t s(std::make_shared<snaplogger::severity>(level, "remark"));
153 :
154 1 : snaplogger::add_severity(s);
155 :
156 2 : snaplogger::severity::pointer_t r("remark"_sev);
157 1 : CATCH_REQUIRE(r == s);
158 :
159 1 : CATCH_REQUIRE(r->get_severity() == level);
160 1 : CATCH_REQUIRE(s->get_severity() == level);
161 1 : CATCH_REQUIRE(r->get_name() == "remark");
162 1 : CATCH_REQUIRE(s->get_name() == "remark");
163 : }
164 : }
165 : CATCH_END_SECTION()
166 :
167 4 : CATCH_START_SECTION("Print Severity")
168 : {
169 : {
170 2 : std::stringstream buffer;
171 1 : buffer << ::snaplogger::severity_t::SEVERITY_ERROR;
172 1 : CATCH_REQUIRE(buffer.str() == "error");
173 : }
174 :
175 : {
176 2 : std::stringstream buffer;
177 1 : buffer << static_cast<::snaplogger::severity_t>(254);
178 1 : CATCH_REQUIRE(buffer.str() == "(unknown severity: 254)");
179 : }
180 : }
181 : CATCH_END_SECTION()
182 8 : }
183 :
184 :
185 :
186 : // vim: ts=4 sw=4 et
|