xref: /freebsd/contrib/kyua/utils/logging/macros_test.cpp (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/logging/macros.hpp"
30 
31 #include <fstream>
32 #include <string>
33 
34 #include <atf-c++.hpp>
35 
36 #include "utils/datetime.hpp"
37 #include "utils/fs/path.hpp"
38 #include "utils/logging/operations.hpp"
39 
40 namespace datetime = utils::datetime;
41 namespace fs = utils::fs;
42 namespace logging = utils::logging;
43 
44 
45 ATF_TEST_CASE_WITHOUT_HEAD(ld);
46 ATF_TEST_CASE_BODY(ld)
47 {
48     logging::set_persistency("debug", fs::path("test.log"));
49     datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);
50     LD("Debug message");
51 
52     std::ifstream input("test.log");
53     ATF_REQUIRE(input);
54 
55     std::string line;
56     ATF_REQUIRE(std::getline(input, line).good());
57     ATF_REQUIRE_MATCH("20110221-183000 D .*: Debug message", line);
58 }
59 
60 
61 ATF_TEST_CASE_WITHOUT_HEAD(le);
62 ATF_TEST_CASE_BODY(le)
63 {
64     logging::set_persistency("debug", fs::path("test.log"));
65     datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);
66     LE("Error message");
67 
68     std::ifstream input("test.log");
69     ATF_REQUIRE(input);
70 
71     std::string line;
72     ATF_REQUIRE(std::getline(input, line).good());
73     ATF_REQUIRE_MATCH("20110221-183000 E .*: Error message", line);
74 }
75 
76 
77 ATF_TEST_CASE_WITHOUT_HEAD(li);
78 ATF_TEST_CASE_BODY(li)
79 {
80     logging::set_persistency("debug", fs::path("test.log"));
81     datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);
82     LI("Info message");
83 
84     std::ifstream input("test.log");
85     ATF_REQUIRE(input);
86 
87     std::string line;
88     ATF_REQUIRE(std::getline(input, line).good());
89     ATF_REQUIRE_MATCH("20110221-183000 I .*: Info message", line);
90 }
91 
92 
93 ATF_TEST_CASE_WITHOUT_HEAD(lw);
94 ATF_TEST_CASE_BODY(lw)
95 {
96     logging::set_persistency("debug", fs::path("test.log"));
97     datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);
98     LW("Warning message");
99 
100     std::ifstream input("test.log");
101     ATF_REQUIRE(input);
102 
103     std::string line;
104     ATF_REQUIRE(std::getline(input, line).good());
105     ATF_REQUIRE_MATCH("20110221-183000 W .*: Warning message", line);
106 }
107 
108 
109 ATF_INIT_TEST_CASES(tcs)
110 {
111     ATF_ADD_TEST_CASE(tcs, ld);
112     ATF_ADD_TEST_CASE(tcs, le);
113     ATF_ADD_TEST_CASE(tcs, li);
114     ATF_ADD_TEST_CASE(tcs, lw);
115 }
116