LCOV - code coverage report
Current view: top level - tests - logger.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 106 106 100.0 %
Date: 2021-09-08 13:54:31 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2021  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  */
      25             : 
      26             : // self
      27             : //
      28             : #include    "main.h"
      29             : 
      30             : // advgetopt lib
      31             : //
      32             : #include    <advgetopt/exception.h>
      33             : 
      34             : 
      35             : // cppthread lib
      36             : //
      37             : #include    <cppthread/exception.h>
      38             : 
      39             : 
      40             : // snapdev lib
      41             : //
      42             : #include    <snapdev/safe_setenv.h>
      43             : 
      44             : 
      45             : // C++ lib
      46             : //
      47             : #include    <fstream>
      48             : 
      49             : 
      50             : 
      51             : 
      52           5 : CATCH_TEST_CASE("logger", "[logger][valid][log]")
      53             : {
      54           6 :     CATCH_START_SECTION("Verify log levels")
      55           1 :         CATCH_REQUIRE(to_string(cppthread::log_level_t::debug)   == "debug");
      56           1 :         CATCH_REQUIRE(to_string(cppthread::log_level_t::info)    == "info");
      57           1 :         CATCH_REQUIRE(to_string(cppthread::log_level_t::warning) == "warning");
      58           1 :         CATCH_REQUIRE(to_string(cppthread::log_level_t::error)   == "error");
      59           1 :         CATCH_REQUIRE(to_string(cppthread::log_level_t::fatal)   == "fatal");
      60             :     CATCH_END_SECTION()
      61             : 
      62           6 :     CATCH_START_SECTION("Verify log string")
      63           1 :         SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test a regular string.");
      64           2 :         cppthread::log << cppthread::log_level_t::debug
      65           1 :                        << "Test a regular string."
      66           2 :                        << cppthread::end;
      67           1 :         SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      68             : 
      69           1 :         SNAP_CATCH2_NAMESPACE::push_expected_log("info: Test an std::string.");
      70           2 :         std::string const msg("Test an std::string.");
      71           2 :         cppthread::log << cppthread::log_level_t::info
      72           1 :                        << msg
      73           2 :                        << cppthread::end;
      74           1 :         SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      75             :     CATCH_END_SECTION()
      76             : 
      77           6 :     CATCH_START_SECTION("Verify log integers")
      78             :         // gcc sees this one as a char
      79             :         {
      80           1 :             std::int8_t v(rand());
      81           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("warning: Test an int8_t: ") + static_cast<char>(v) + ".");
      82           2 :             cppthread::log << cppthread::log_level_t::warning
      83           1 :                            << "Test an int8_t: "
      84           1 :                            << v
      85           1 :                            << "."
      86           2 :                            << cppthread::end;
      87           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      88             :         }
      89             : 
      90             :         {
      91           1 :             std::int16_t v(rand());
      92           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an int16_t: " + std::to_string(v) + ".");
      93           2 :             cppthread::log << cppthread::log_level_t::error
      94           1 :                            << "Test an int16_t: "
      95           1 :                            << v
      96           1 :                            << "."
      97           2 :                            << cppthread::end;
      98           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      99             :         }
     100             : 
     101             :         {
     102           1 :             std::int32_t v(rand());
     103           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an int32_t: " + std::to_string(v) + ".");
     104           2 :             cppthread::log << cppthread::log_level_t::fatal
     105           1 :                            << "Test an int32_t: "
     106           1 :                            << v
     107           1 :                            << "."
     108           2 :                            << cppthread::end;
     109           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     110             :         }
     111             : 
     112             :         {
     113           1 :             std::int64_t v(rand());
     114           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test an int64_t: " + std::to_string(v) + ".");
     115           2 :             cppthread::log << cppthread::log_level_t::debug
     116           1 :                            << "Test an int64_t: "
     117           1 :                            << v
     118           1 :                            << "."
     119           2 :                            << cppthread::end;
     120           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     121             :         }
     122             : 
     123             :         // gcc sees this one as a char
     124             :         {
     125           1 :             std::uint8_t v(rand());
     126           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("info: Test an uint8_t: ") + static_cast<char>(v) + ".");
     127           2 :             cppthread::log << cppthread::log_level_t::info
     128           1 :                            << "Test an uint8_t: "
     129           1 :                            << v
     130           1 :                            << "."
     131           2 :                            << cppthread::end;
     132           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     133             :         }
     134             : 
     135             :         {
     136           1 :             std::uint16_t v(rand());
     137           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("warning: Test an uint16_t: " + std::to_string(static_cast<int>(v)) + ".");
     138           2 :             cppthread::log << cppthread::log_level_t::warning
     139           1 :                            << "Test an uint16_t: "
     140           1 :                            << v
     141           1 :                            << "."
     142           2 :                            << cppthread::end;
     143           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     144             :         }
     145             : 
     146             :         {
     147           1 :             std::uint32_t v(rand());
     148           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an uint32_t: " + std::to_string(v) + ".");
     149           2 :             cppthread::log << cppthread::log_level_t::error
     150           1 :                            << "Test an uint32_t: "
     151           1 :                            << v
     152           1 :                            << "."
     153           2 :                            << cppthread::end;
     154           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     155             :         }
     156             : 
     157             :         {
     158           1 :             std::uint64_t v(rand());
     159           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an uint64_t: " + std::to_string(v) + ".");
     160           2 :             cppthread::log << cppthread::log_level_t::fatal
     161           1 :                            << "Test an uint64_t: "
     162           1 :                            << v
     163           1 :                            << "."
     164           2 :                            << cppthread::end;
     165           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     166             :         }
     167             : 
     168             :     CATCH_END_SECTION()
     169           3 : }
     170             : 
     171             : 
     172           3 : CATCH_TEST_CASE("logger_without_callback", "[logger][valid][log]")
     173             : {
     174           2 :     CATCH_START_SECTION("Verify log string")
     175             :         // cancel the callback for one test
     176           1 :         cppthread::set_log_callback(nullptr);
     177             : 
     178             :         //SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test without a callback."); -- not going to be registered!
     179           2 :         cppthread::log << cppthread::log_level_t::debug
     180           1 :                        << "Test without a callback."
     181           2 :                        << cppthread::end;
     182             : 
     183             :         // restore the callback
     184           1 :         cppthread::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
     185             :     CATCH_END_SECTION()
     186           1 : }
     187             : 
     188             : 
     189           3 : CATCH_TEST_CASE("invalid_logger", "[logger][invalid][log]")
     190             : {
     191           2 :     CATCH_START_SECTION("Verify invalid log levels")
     192         101 :         for(int i(0); i < 100; ++i)
     193             :         {
     194         100 :             cppthread::log_level_t level(cppthread::log_level_t::warning);
     195             :             for(;;)
     196             :             {
     197         100 :                 level = static_cast<cppthread::log_level_t>(rand());
     198         100 :                 if(level != cppthread::log_level_t::debug
     199         100 :                 && level != cppthread::log_level_t::info
     200         100 :                 && level != cppthread::log_level_t::warning
     201         100 :                 && level != cppthread::log_level_t::error
     202         100 :                 && level != cppthread::log_level_t::fatal)
     203             :                 {
     204         100 :                     break;
     205             :                 }
     206             :             }
     207             : 
     208         100 :             CATCH_REQUIRE_THROWS_MATCHES(cppthread::to_string(level)
     209             :                     , cppthread::cppthread_invalid_error
     210             :                     , Catch::Matchers::ExceptionMessage(
     211             :                               "cppthread_exception: unknown log level ("
     212             :                             + std::to_string(static_cast<int>(level))
     213             :                             + ")"));
     214             :         }
     215             :     CATCH_END_SECTION()
     216           7 : }
     217             : 
     218             : 
     219             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13