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 {
29 }
30
33 }
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
61 && strstr(msg, "The RNTuple file format will change. Do not store real data with this version of RNTuple!") != nullptr) {
62 std::cerr << "Warning in " << location << " " << msg << std::endl;
63 return;
64 }
65 if (level == kWarning
66 && strstr(msg, "Pre-release format version: RC ") != nullptr) {
67 std::cerr << "Warning in " << location << " " << msg << std::endl;
68 return;
69 }
70 if (level == kWarning && strstr(msg, "Merging RNTuples is experimental") != nullptr) {
71 std::cerr << "Warning in " << location << " " << msg << std::endl;
72 return;
73 }
74
75 // FIXME: DOAS backend is exprimental.
76 if (level == kWarning
77 && strstr(msg, "The DAOS backend is experimental and still under development") != nullptr) {
78 std::cerr << "Warning in " << location << " " << msg << std::endl;
79 return;
80 }
81
82 // FIXME: RooNaNPacker warns about not being implemented for big endian
83 if (level == kWarning
84 && 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) {
85 std::cerr << "Warning in " << location << " " << msg << std::endl;
86 return;
87 }
88
89 FAIL() << "Received unexpected diagnostic of severity "
90 << level
91 << " at '" << location << "' reading '" << msg << "'.\n"
92 << "Suppress those using ROOT/TestSupport.hxx";
93 }
94
97
98
99CheckDiagsRAII * CheckDiagsRAII::sActiveInstance = nullptr;
100
104 gInterpreter->ReportDiagnosticsToErrorHandler(/*enable=*/false);
105
106 for (const auto &diag : fExpectedDiags) {
107 if (!diag.optional && diag.receivedCount < 1) {
108 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Expected diagnostic message missing:\n" << diag;
109 }
110 }
111
112 for (const auto &diag : fUnexpectedDiags) {
113 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Unexpected diagnostic message:\n" << diag;
114 }
115}
116
117/// Search list of expected diagnostics for given arguments, and increase the receivedCount.
118/// If no matching predefined diagnostic is found, this will trigger an unexpected diagnostic error on exit.
119void CheckDiagsRAII::checkDiag(int severity, const char * location, const char * msg) {
120 // Check that this received diagnostics was expected
121 const std::string message = msg;
122 const auto expectedMatch =
123 std::find_if(fExpectedDiags.begin(), fExpectedDiags.end(), [=](const Diag_t &expectedDiag) {
124 return expectedDiag.severity == severity
125 && strstr(location, expectedDiag.location.c_str()) != nullptr
126 && (expectedDiag.matchFullString ? expectedDiag.message == message : (message.find(expectedDiag.message) != std::string::npos));
127 });
128
129 if (expectedMatch != fExpectedDiags.end()) {
130 expectedMatch->receivedCount += 1;
131 } else if (severity <= kInfo) {
132 fOldErrorHandler(severity, false, location, msg);
133 } else {
134 fUnexpectedDiags.push_back({severity, location, std::move(message)});
135 }
136}
137
138std::ostream &operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
139{
140 std::map<int, std::string> dict = {
141 {kInfo, "kInfo"}, {kWarning, "kWarning"}, {kError, "kError"}, {kSysError, "kSysError"}};
142
143 constexpr auto indent = 15;
144 stream << std::right << std::setw(indent) << "severity: " << dict[diag.severity];
145 if (diag.receivedCount >= 0) {
146 stream << "\n"
147 << std::setw(indent) << "received: " << diag.receivedCount << " times ("
148 << (diag.optional ? "optional" : "required") << ", " << (diag.matchFullString ? "fullMatch" : "subMatch")
149 << ")\t";
150 }
151 stream << "\n"
152 << std::setw(indent) << "origin: " << '"' << diag.location << "\""
153 << "\n"
154 << std::setw(indent) << "message: " << diag.message << "\n";
155
156 return stream;
157}
158} } //ROOT::TestSupport
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:397
static void indent(ostringstream &buf, int indent_level)
constexpr Int_t kError
Definition TError.h:46
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:100
constexpr Int_t kWarning
Definition TError.h:45
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition TError.h:70
constexpr Int_t kInfo
Definition TError.h:44
Int_t gErrorIgnoreLevel
Error handling routines.
Definition TError.cxx:31
constexpr Int_t kSysError
Definition TError.h:48
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.
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
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
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.