Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TestSupport.cxx
Go to the documentation of this file.
1/// \file
2/// \brief This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
3///
4/// \author Stephan Hageboeck <stephan.hageboeck@cern.ch>
5
6/*************************************************************************
7 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#include "ROOT/TestSupport.hxx"
15
16#include <algorithm>
17#include <cstring>
18#include <iostream>
19#include <iomanip>
20
21namespace ROOT {
22namespace TestSupport {
23
24/// Error handler for gtests that generates failures for every received diagnostic > kInfo when this file is linked to.
25static struct ForbidDiagnostics {
30
34
35 /// Diagnostic handler that's installed for all google tests.
36 /// It will generate a test failure when a diagnostic message is issued.
37 static void handler(int level, bool abort,
38 const char *location,
39 const char *msg) {
40 if (level <= gErrorIgnoreLevel) return;
41 if (level <= kInfo) {
42 std::cerr << "ROOT::TestSupport::ForbidDiagnostics::handler(): Diagnostic in '" << location << "':\n"
43 << msg << std::endl;
44 return;
45 }
46
47 if (abort) {
48 std::cerr << "ROOT::TestSupport::ForbidDiagnostics::handler(): Forced to abort because of diagnostic with severity "
49 << level << " in '" << location << "' reading '" << msg << "'\n";
50 ::abort();
51 }
52
53 // FIXME: Windows has problem finding PCMs.
54 if (level == kError && strcmp(location, "TCling::LoadPCM") == 0 && strstr(msg, "file does not exist") != nullptr) {
55 std::cerr << "Error in " << location << " " << msg << std::endl;
56 return;
57 }
58
59 // FIXME: RNTuple warns that it's in beta stage.
60 if (level == kWarning && strstr(msg, "Merging RNTuples is experimental") != nullptr) {
61 std::cerr << "Warning in " << location << " " << msg << std::endl;
62 return;
63 }
64
65 // FIXME: DOAS backend is exprimental.
66 if (level == kWarning
67 && strstr(msg, "The DAOS backend is experimental and still under development") != nullptr) {
68 std::cerr << "Warning in " << location << " " << msg << std::endl;
69 return;
70 }
71
72 // FIXME: RooNaNPacker warns about not being implemented for big endian
73 if (level == kWarning
74 && strcmp(msg, "Fast recovery from undefined function values only implemented for little-endian machines. If necessary, request an extension of functionality on https://root.cern") == 0) {
75 std::cerr << "Warning in " << location << " " << msg << std::endl;
76 return;
77 }
78
79 FAIL() << "Received unexpected diagnostic of severity "
80 << level
81 << " at '" << location << "' reading '" << msg << "'.\n"
82 << "Suppress those using ROOT/TestSupport.hxx";
83 }
84
87
88
89CheckDiagsRAII * CheckDiagsRAII::sActiveInstance = nullptr;
90
94 gInterpreter->ReportDiagnosticsToErrorHandler(/*enable=*/false);
95
96 for (const auto &diag : fExpectedDiags) {
97 if (!diag.optional && diag.receivedCount < 1) {
98 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Expected diagnostic message missing:\n" << diag;
99 }
100 }
101
102 for (const auto &diag : fUnexpectedDiags) {
103 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Unexpected diagnostic message:\n" << diag;
104 }
105}
106
107/// Search list of expected diagnostics for given arguments, and increase the receivedCount.
108/// If no matching predefined diagnostic is found, this will trigger an unexpected diagnostic error on exit.
109void CheckDiagsRAII::checkDiag(int severity, const char * location, const char * msg) {
110 // Check that this received diagnostics was expected
111 const std::string message = msg;
112 const auto expectedMatch =
113 std::find_if(fExpectedDiags.begin(), fExpectedDiags.end(), [=](const Diag_t &expectedDiag) {
114 return expectedDiag.severity == severity
115 && strstr(location, expectedDiag.location.c_str()) != nullptr
116 && (expectedDiag.matchFullString ? expectedDiag.message == message : (message.find(expectedDiag.message) != std::string::npos));
117 });
118
120 expectedMatch->receivedCount += 1;
121 } else if (severity <= kInfo) {
122 fOldErrorHandler(severity, false, location, msg);
123 } else {
124 fUnexpectedDiags.push_back({severity, location, std::move(message)});
125 }
126}
127
128std::ostream &operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
129{
130 std::map<int, std::string> dict = {
131 {kInfo, "kInfo"}, {kWarning, "kWarning"}, {kError, "kError"}, {kSysError, "kSysError"}};
132
133 constexpr auto indent = 15;
134 stream << std::right << std::setw(indent) << "severity: " << dict[diag.severity];
135 if (diag.receivedCount >= 0) {
136 stream << "\n"
137 << std::setw(indent) << "received: " << diag.receivedCount << " times ("
138 << (diag.optional ? "optional" : "required") << ", " << (diag.matchFullString ? "fullMatch" : "subMatch")
139 << ")\t";
140 }
141 stream << "\n"
142 << std::setw(indent) << "origin: " << '"' << diag.location << "\""
143 << "\n"
144 << std::setw(indent) << "message: " << diag.message << "\n";
145
146 return stream;
147}
148} } //ROOT::TestSupport
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:100
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition TError.h:71
Int_t gErrorIgnoreLevel
Error handling routines.
Definition TError.cxx:31
constexpr Int_t kSysError
Definition TError.h:49
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition TError.cxx:90
#define gInterpreter
The file contains facilities allowing easier writing of in-tree unit tests.
const_iterator end() const
ErrorHandlerFunc_t const fOldErrorHandler
Last active handler in case handlers are nested.
static CheckDiagsRAII * sActiveInstance
Last active error handler function.
std::vector< Diag_t > fUnexpectedDiags
std::vector< Diag_t > fExpectedDiags
void checkDiag(int severity, const char *location, const char *msg)
Check all received diags against list of expected ones.
CheckDiagsRAII *const fOldInstance
static struct ROOT::TestSupport::ForbidDiagnostics noDiagCheckerInstance
std::ostream & operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
@ kInfo
Informational messages; used for instance for tracing.
@ kError
An error.
@ kWarning
Warnings about likely unexpected behavior.
Error handler for gtests that generates failures for every received diagnostic > kInfo when this file...
ErrorHandlerFunc_t const sOldErrorHandler
static void handler(int level, bool abort, const char *location, const char *msg)
Diagnostic handler that's installed for all google tests.