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 "gtest/gtest.h"
17
18#include <algorithm>
19#include <cstring>
20#include <iostream>
21#include <iomanip>
22
23namespace ROOT {
24namespace TestSupport {
25
26/// Error handler for gtests that generates failures for every received diagnostic > kInfo when this file is linked to.
27static struct ForbidDiagnostics {
32
36
37 /// Diagnostic handler that's installed for all google tests.
38 /// It will generate a test failure when a diagnostic message is issued.
39 static void handler(int level, bool abort,
40 const char *location,
41 const char *msg) {
42 if (level <= gErrorIgnoreLevel) return;
43 if (level <= kInfo) {
44 std::cerr << "ROOT::TestSupport::ForbidDiagnostics::handler(): Diagnostic in '" << location << "':\n"
45 << msg << std::endl;
46 return;
47 }
48
49 if (abort) {
50 std::cerr << "ROOT::TestSupport::ForbidDiagnostics::handler(): Forced to abort because of diagnostic with severity "
51 << level << " in '" << location << "' reading '" << msg << "'\n";
52 ::abort();
53 }
54
55 // FIXME: Windows has problem finding PCMs.
56 if (level == kError && strcmp(location, "TCling::LoadPCM") == 0 && strstr(msg, "file does not exist") != nullptr) {
57 std::cerr << "Error in " << location << " " << msg << std::endl;
58 return;
59 }
60
61 // FIXME: DOAS backend is experimental.
62 if (level == kWarning
63 && strstr(msg, "The DAOS backend is experimental and still under development") != nullptr) {
64 std::cerr << "Warning in " << location << " " << msg << std::endl;
65 return;
66 }
67
68 // FIXME: RooNaNPacker warns about not being implemented for big endian
69 if (level == kWarning
70 && 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) {
71 std::cerr << "Warning in " << location << " " << msg << std::endl;
72 return;
73 }
74
75 if (level == kWarning && strcmp(location, "RIoUring") == 0 &&
76 strstr(msg, "io_uring is not available because:") != nullptr) {
77 std::cerr << "Warning in " << location << " " << msg << std::endl;
78 return;
79 }
80
81 if (level == kWarning && strcmp(location, "RRawFileUnix") == 0 &&
82 strcmp(msg, "io_uring setup failed, falling back to blocking I/O in ReadV") == 0) {
83 std::cerr << "Warning in " << location << " " << msg << std::endl;
84 return;
85 }
86
87 FAIL() << "Received unexpected diagnostic of severity "
88 << level
89 << " at '" << location << "' reading '" << msg << "'.\n"
90 << "Suppress those using ROOT/TestSupport.hxx";
91 }
92
95
96
97CheckDiagsRAII * CheckDiagsRAII::sActiveInstance = nullptr;
98
102 gInterpreter->ReportDiagnosticsToErrorHandler(/*enable=*/false);
103
104 for (const auto &diag : fExpectedDiags) {
105 if (!diag.optional && diag.receivedCount < 1) {
106 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Expected diagnostic message missing:\n" << diag;
107 }
108 }
109
110 for (const auto &diag : fUnexpectedDiags) {
111 ADD_FAILURE() << "ROOT::TestSupport::CheckDiagsRAII: Unexpected diagnostic message:\n" << diag;
112 }
113}
114
115/// Search list of expected diagnostics for given arguments, and increase the receivedCount.
116/// If no matching predefined diagnostic is found, this will trigger an unexpected diagnostic error on exit.
117void CheckDiagsRAII::checkDiag(int severity, const char * location, const char * msg) {
118 // Check that this received diagnostics was expected
119 const std::string message = msg;
120 const auto expectedMatch =
121 std::find_if(fExpectedDiags.begin(), fExpectedDiags.end(), [=](const Diag_t &expectedDiag) {
122 return expectedDiag.severity == severity
123 && strstr(location, expectedDiag.location.c_str()) != nullptr
124 && (expectedDiag.matchFullString ? expectedDiag.message == message : (message.find(expectedDiag.message) != std::string::npos));
125 });
126
127 if (expectedMatch != fExpectedDiags.end()) {
128 expectedMatch->receivedCount += 1;
129 } else if (severity <= kInfo) {
130 fOldErrorHandler(severity, false, location, msg);
131 } else {
132 fUnexpectedDiags.push_back({severity, location, std::move(message)});
133 }
134}
135
136std::ostream &operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
137{
138 std::map<int, std::string> dict = {
139 {kInfo, "kInfo"}, {kWarning, "kWarning"}, {kError, "kError"}, {kSysError, "kSysError"}};
140
141 constexpr auto indent = 15;
142 stream << std::right << std::setw(indent) << "severity: " << dict[diag.severity];
143 if (diag.receivedCount >= 0) {
144 stream << "\n"
145 << std::setw(indent) << "received: " << diag.receivedCount << " times ("
146 << (diag.optional ? "optional" : "required") << ", " << (diag.matchFullString ? "fullMatch" : "subMatch")
147 << ")\t";
148 }
149 stream << "\n"
150 << std::setw(indent) << "origin: " << '"' << diag.location << "\""
151 << "\n"
152 << std::setw(indent) << "message: " << diag.message << "\n";
153
154 return stream;
155}
156} } //ROOT::TestSupport
static void indent(ostringstream &buf, int indent_level)
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:102
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition TError.h:80
Int_t gErrorIgnoreLevel
errors with level below this value will be ignored. Default is kUnset.
Definition TError.cxx:33
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:92
#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
std::ostream & operator<<(std::ostream &stream, CheckDiagsRAII::Diag_t const &diag)
@ 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.