LCOV - code coverage report
Current view: top level - tests - logger.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 106 107 99.1 %
Date: 2019-08-10 16:09:07 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2019  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             : // snapdev lib
      35             : //
      36             : #include <snapdev/safe_setenv.h>
      37             : 
      38             : // C++ lib
      39             : //
      40             : #include <fstream>
      41             : 
      42             : 
      43             : 
      44             : 
      45           5 : CATCH_TEST_CASE("logger", "[logger][valid][log]")
      46             : {
      47           6 :     CATCH_START_SECTION("Verify log levels")
      48           1 :         CATCH_REQUIRE(to_string(advgetopt::log_level_t::debug)   == "debug");
      49           1 :         CATCH_REQUIRE(to_string(advgetopt::log_level_t::info)    == "info");
      50           1 :         CATCH_REQUIRE(to_string(advgetopt::log_level_t::warning) == "warning");
      51           1 :         CATCH_REQUIRE(to_string(advgetopt::log_level_t::error)   == "error");
      52           1 :         CATCH_REQUIRE(to_string(advgetopt::log_level_t::fatal)   == "fatal");
      53             :     CATCH_END_SECTION()
      54             : 
      55           6 :     CATCH_START_SECTION("Verify log string")
      56           1 :         SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test a regular string.");
      57           2 :         advgetopt::log << advgetopt::log_level_t::debug
      58           1 :                        << "Test a regular string."
      59           1 :                        << advgetopt::end;
      60           1 :         SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      61             : 
      62           1 :         SNAP_CATCH2_NAMESPACE::push_expected_log("info: Test an std::string.");
      63           2 :         std::string const msg("Test an std::string.");
      64           2 :         advgetopt::log << advgetopt::log_level_t::info
      65           1 :                        << msg
      66           1 :                        << advgetopt::end;
      67           1 :         SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      68             :     CATCH_END_SECTION()
      69             : 
      70           6 :     CATCH_START_SECTION("Verify log integers")
      71             :         // gcc sees this one as a char
      72             :         {
      73           1 :             std::int8_t v(rand());
      74           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("warning: Test an int8_t: ") + static_cast<char>(v) + ".");
      75           2 :             advgetopt::log << advgetopt::log_level_t::warning
      76           1 :                            << "Test an int8_t: "
      77           1 :                            << v
      78           1 :                            << "."
      79           1 :                            << advgetopt::end;
      80           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      81             :         }
      82             : 
      83             :         {
      84           1 :             std::int16_t v(rand());
      85           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an int16_t: " + std::to_string(v) + ".");
      86           2 :             advgetopt::log << advgetopt::log_level_t::error
      87           1 :                            << "Test an int16_t: "
      88           1 :                            << v
      89           1 :                            << "."
      90           1 :                            << advgetopt::end;
      91           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
      92             :         }
      93             : 
      94             :         {
      95           1 :             std::int32_t v(rand());
      96           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an int32_t: " + std::to_string(v) + ".");
      97           2 :             advgetopt::log << advgetopt::log_level_t::fatal
      98           1 :                            << "Test an int32_t: "
      99           1 :                            << v
     100           1 :                            << "."
     101           1 :                            << advgetopt::end;
     102           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     103             :         }
     104             : 
     105             :         {
     106           1 :             std::int64_t v(rand());
     107           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test an int64_t: " + std::to_string(v) + ".");
     108           2 :             advgetopt::log << advgetopt::log_level_t::debug
     109           1 :                            << "Test an int64_t: "
     110           1 :                            << v
     111           1 :                            << "."
     112           1 :                            << advgetopt::end;
     113           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     114             :         }
     115             : 
     116             :         // gcc sees this one as a char
     117             :         {
     118           1 :             std::uint8_t v(rand());
     119           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("info: Test an uint8_t: ") + static_cast<char>(v) + ".");
     120           2 :             advgetopt::log << advgetopt::log_level_t::info
     121           1 :                            << "Test an uint8_t: "
     122           1 :                            << v
     123           1 :                            << "."
     124           1 :                            << advgetopt::end;
     125           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     126             :         }
     127             : 
     128             :         {
     129           1 :             std::uint16_t v(rand());
     130           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("warning: Test an uint16_t: " + std::to_string(static_cast<int>(v)) + ".");
     131           2 :             advgetopt::log << advgetopt::log_level_t::warning
     132           1 :                            << "Test an uint16_t: "
     133           1 :                            << v
     134           1 :                            << "."
     135           1 :                            << advgetopt::end;
     136           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     137             :         }
     138             : 
     139             :         {
     140           1 :             std::uint32_t v(rand());
     141           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an uint32_t: " + std::to_string(v) + ".");
     142           2 :             advgetopt::log << advgetopt::log_level_t::error
     143           1 :                            << "Test an uint32_t: "
     144           1 :                            << v
     145           1 :                            << "."
     146           1 :                            << advgetopt::end;
     147           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     148             :         }
     149             : 
     150             :         {
     151           1 :             std::uint64_t v(rand());
     152           1 :             SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an uint64_t: " + std::to_string(v) + ".");
     153           2 :             advgetopt::log << advgetopt::log_level_t::fatal
     154           1 :                            << "Test an uint64_t: "
     155           1 :                            << v
     156           1 :                            << "."
     157           1 :                            << advgetopt::end;
     158           1 :             SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     159             :         }
     160             : 
     161             :     CATCH_END_SECTION()
     162           3 : }
     163             : 
     164             : 
     165           3 : CATCH_TEST_CASE("logger_without_callback", "[logger][valid][log]")
     166             : {
     167           2 :     CATCH_START_SECTION("Verify log string")
     168             :         // cancel the callback for one test
     169           1 :         advgetopt::set_log_callback(nullptr);
     170             : 
     171             :         //SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test without a callback."); -- not going to be registered!
     172           2 :         advgetopt::log << advgetopt::log_level_t::debug
     173           1 :                        << "Test without a callback."
     174           1 :                        << advgetopt::end;
     175             : 
     176             :         // restore the callback
     177           1 :         advgetopt::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
     178             :     CATCH_END_SECTION()
     179           1 : }
     180             : 
     181             : 
     182           3 : CATCH_TEST_CASE("invalid_logger", "[logger][invalid][log]")
     183             : {
     184           2 :     CATCH_START_SECTION("Verify invalid log levels")
     185         101 :         for(int i(0); i < 100; ++i)
     186             :         {
     187         100 :             advgetopt::log_level_t level(advgetopt::log_level_t::warning);
     188           0 :             for(;;)
     189             :             {
     190         100 :                 level = static_cast<advgetopt::log_level_t>(rand());
     191         100 :                 if(level != advgetopt::log_level_t::debug
     192         100 :                 && level != advgetopt::log_level_t::info
     193         100 :                 && level != advgetopt::log_level_t::warning
     194         100 :                 && level != advgetopt::log_level_t::error
     195         100 :                 && level != advgetopt::log_level_t::fatal)
     196             :                 {
     197         100 :                     break;
     198             :                 }
     199             :             }
     200             : 
     201         100 :             CATCH_REQUIRE_THROWS_MATCHES(advgetopt::to_string(level)
     202             :                     , advgetopt::getopt_exception_invalid
     203             :                     , Catch::Matchers::ExceptionMessage(
     204             :                               "unknown log level ("
     205             :                             + std::to_string(static_cast<int>(level))
     206             :                             + ")"));
     207             :         }
     208             :     CATCH_END_SECTION()
     209           7 : }
     210             : 
     211             : 
     212             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12