LCOV - code coverage report
Current view: top level - tests - log_for_test.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 20 40 50.0 %
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/log.h>
      33             : 
      34             : // libexcept lib
      35             : //
      36             : #include <libexcept/exception.h>
      37             : 
      38             : 
      39             : 
      40             : namespace SNAP_CATCH2_NAMESPACE
      41             : {
      42             : 
      43             : 
      44             : 
      45             : namespace
      46             : {
      47             : 
      48           2 : std::vector<std::string>        g_expected_logs = std::vector<std::string>();
      49             : 
      50             : }
      51             : // no name namespace
      52             : 
      53             : 
      54             : 
      55         220 : void push_expected_log(std::string const & message)
      56             : {
      57         220 :     g_expected_logs.push_back(message);
      58         220 : }
      59             : 
      60             : 
      61         220 : void log_for_test(advgetopt::log_level_t level, std::string const & message)
      62             : {
      63         440 :     if(SNAP_CATCH2_NAMESPACE::g_verbose()
      64         220 :     || g_expected_logs.empty())
      65             :     {
      66           0 :         std::cerr << "logger sent:\n"
      67           0 :                   << advgetopt::to_string(level)
      68           0 :                   << ": "
      69           0 :                   << message
      70           0 :                   << std::endl;
      71             :     }
      72             : 
      73             :     // at this time it's impossible to debug the location of the empty
      74             :     // problem without a proper stack trace...
      75             :     //
      76         220 :     if(g_expected_logs.empty())
      77             :     {
      78           0 :         libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
      79           0 :         std::cerr << "*** STACK TRACE ***" << std::endl;
      80           0 :         for(auto const & l : trace)
      81             :         {
      82           0 :             std::cerr << l << std::endl;
      83             :         }
      84           0 :         std::cerr << "***" << std::endl;
      85             :     }
      86             : 
      87         220 :     CATCH_REQUIRE_FALSE(g_expected_logs.empty());
      88             : 
      89         440 :     std::stringstream ss;
      90         220 :     ss << advgetopt::to_string(level) << ": " << message;
      91             : 
      92             :     // again, the REQUIRE() is not going to be useful in terms of line number
      93             :     //
      94         220 :     if(g_expected_logs[0] != ss.str())
      95             :     {
      96           0 :         libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
      97           0 :         std::cerr << "*** STACK TRACE ***" << std::endl;
      98           0 :         for(auto const & l : trace)
      99             :         {
     100           0 :             std::cerr << l << std::endl;
     101             :         }
     102           0 :         std::cerr << "***" << std::endl;
     103             :     }
     104             : 
     105         440 :     std::string expected_msg(g_expected_logs[0]);
     106         220 :     g_expected_logs.erase(g_expected_logs.begin());
     107             : 
     108         220 :     CATCH_REQUIRE(expected_msg == ss.str());
     109         220 : }
     110             : 
     111             : 
     112          80 : void expected_logs_stack_is_empty()
     113             : {
     114          80 :     if(!g_expected_logs.empty())
     115             :     {
     116           0 :         std::cerr << "List of expected error logs which did not occur:" << std::endl;
     117           0 :         for(auto l : g_expected_logs)
     118             :         {
     119           0 :             std::cerr << "  " << l << std::endl;
     120             :         }
     121           0 :         g_expected_logs.clear();
     122           0 :         throw std::logic_error("a test left an unexpected error message in the g_expected_logs vector.");
     123             :     }
     124          80 : }
     125             : 
     126             : 
     127           6 : } // SNAP_CATCH2_NAMESPACE namespace
     128             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12