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 FAIL() << "Received unexpected diagnostic of severity "
83 << level
84 << " at '" << location << "' reading '" << msg << "'.\n"
85 << "Suppress those using ROOT/TestSupport.hxx";
86 }
87
90
91
92CheckDiagsRAII * CheckDiagsRAII::sActiveInstance = nullptr;
93
97 gInterpreter->ReportDiagnosticsToErrorHandler(/*enable=*/false);
98
99 for (const auto &diag : fExpectedDiags) {
100 if (!diag.optional && diag.receivedCount < 1) {
101 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Expected diagnostic message missing:\n" << diag;
102 }
103 }
104
105 for (const auto &diag : fUnexpectedDiags) {
106 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Unexpected diagnostic message:\n" << diag;
107 }
108}
109
110/// Search list of expected diagnostics for given arguments, and increase the receivedCount.
111/// If no matching predefined diagnostic is found, this will trigger an unexpected diagnostic error on exit.
112void CheckDiagsRAII::checkDiag(int severity, const char * location, const char * msg) {
113 // Check that this received diagnostics was expected
114 const std::string message = msg;
115 const auto expectedMatch =
116 std::find_if(fExpectedDiags.begin(), fExpectedDiags.end(), [=](const Diag_t &expectedDiag) {
117 return expectedDiag.severity == severity
118 && strstr(location, expectedDiag.location.c_str()) != nullptr
119 && (expectedDiag.matchFullString ? expectedDiag.message == message : (message.find(expectedDiag.message) != std::string::npos));
120 });
121
122 if (expectedMatch != fExpectedDiags.end()) {
123 expectedMatch->receivedCount += 1;
124 } else if (severity <= kInfo) {
125 fOldErrorHandler(severity, false, location, msg);
126 } else {
127 fUnexpectedDiags.push_back({severity, location, std::move(message)});
128 }
129}
130
131std::ostream &operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
132{
133 std::map<int, std::string> dict = {
134 {kInfo, "kInfo"}, {kWarning, "kWarning"}, {kError, "kError"}, {kSysError, "kSysError"}};
135
136 constexpr auto indent = 15;
137 stream << std::right << std::setw(indent) << "severity: " << dict[diag.severity];
138 if (diag.receivedCount >= 0) {
139 stream << "\n"
140 << std::setw(indent) << "received: " << diag.receivedCount << " times ("
141 << (diag.optional ? "optional" : "required") << ", " << (diag.matchFullString ? "fullMatch" : "subMatch")
142 << ")\t";
143 }
144 stream << "\n"
145 << std::setw(indent) << "origin: " << '"' << diag.location << "\""
146 << "\n"
147 << std::setw(indent) << "message: " << diag.message << "\n";
148
149 return stream;
150}
151} } //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.