comparison tests/logger/main.cpp @ 115:9a8f321371f7

Irccd: add filter system in logging, #491
author David Demelier <markand@malikania.fr>
date Thu, 28 Apr 2016 20:48:23 +0200
parents
children 6635b9187d71
comparison
equal deleted inserted replaced
114:8cbbce7b4327 115:9a8f321371f7
1 /*
2 * main.cpp -- test logger functions
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <algorithm>
20
21 #include <gtest/gtest.h>
22
23 #include <irccd/logger.hpp>
24
25 using namespace irccd;
26
27 namespace {
28
29 std::string lineDebug;
30 std::string lineInfo;
31 std::string lineWarning;
32
33 } // !namespace
34
35 class MyInterface : public log::Interface {
36 public:
37 void debug(const std::string &line) override
38 {
39 lineDebug = line;
40 }
41
42 void info(const std::string &line) override
43 {
44 lineInfo = line;
45 }
46
47 void warning(const std::string &line) override
48 {
49 lineWarning = line;
50 }
51 };
52
53 class MyFilter : public log::Filter {
54 public:
55 std::string preDebug(std::string input) const override
56 {
57 return std::reverse(input.begin(), input.end()), input;
58 }
59
60 std::string preInfo(std::string input) const override
61 {
62 return std::reverse(input.begin(), input.end()), input;
63 }
64
65 std::string preWarning(std::string input) const override
66 {
67 return std::reverse(input.begin(), input.end()), input;
68 }
69 };
70
71 #if !defined(NDEBUG)
72
73 TEST(Logger, debug)
74 {
75 log::debug("debug");
76
77 ASSERT_EQ("gubed", lineDebug);
78 }
79
80 #endif
81
82 TEST(Logger, info)
83 {
84 log::info("info");
85
86 ASSERT_EQ("ofni", lineInfo);
87 }
88
89 TEST(Logger, warning)
90 {
91 log::warning("warning");
92
93 ASSERT_EQ("gninraw", lineWarning);
94 }
95
96 int main(int argc, char **argv)
97 {
98 log::setVerbose(true);
99 log::setInterface(std::make_unique<MyInterface>());
100 log::setFilter(std::make_unique<MyFilter>());
101
102 testing::InitGoogleTest(&argc, argv);
103
104 return RUN_ALL_TESTS();
105 }