9#include <framework/utilities/ScopeGuard.h>
10#include <framework/utilities/Utils.h>
11#include <gtest/gtest.h>
16 struct IntSetterGetterFunctor {
18 explicit IntSetterGetterFunctor(
int& reference): ref(reference) {}
20 void operator()(
const int& v) { ref = v; }
22 int operator()()
const {
return ref; }
28 TEST(ScopeGuards, IntRef)
51 TEST(ScopeGuards, IntSetterGetter)
54 auto setter = [&old](
int v) {old = v;};
55 auto getter = [&old]() {
return old;};
76 TEST(ScopeGuards, IntSetterGetterFunctor)
79 IntSetterGetterFunctor functor(old);
100 [&old](
int v) { old = v; },
101 [&old]() {
return old; }
111 TEST(ScopeGuards, StringReference)
113 std::string value{
"before"};
116 ASSERT_EQ(value,
"after");
118 ASSERT_EQ(value,
"before");
122 [&value](
const std::string & v) { value = v; },
123 [&value]() {
return value; }
125 ASSERT_EQ(value,
"after");
127 ASSERT_EQ(value,
"before");
131 TEST(ScopeGuards, StreamState)
133 std::stringstream buf;
134 buf <<
"a:" << std::setprecision(4) << 1.2;
137 buf <<
":b:" << std::fixed << std::setprecision(4) << 2.3;
141 buf <<
":c:" << std::setprecision(5) << std::setw(10) << std::setfill(
'-') << 3.4;
144 buf <<
":d:" << std::scientific << 4.5;
147 ASSERT_EQ(buf.str(),
"a:1.2:b:2.3000:c:---3.40000:d:4.5000e+00:e:5.4");
151 TEST(ScopeGuards, WorkingDirectory)
153 std::string start{std::filesystem::current_path().c_str()};
154 std::string tmpdir(std::filesystem::temp_directory_path().c_str());
155 std::string root(
"/");
158 std::filesystem::temp_directory_path().c_str());
159 ASSERT_EQ(tmpdir, std::filesystem::current_path().c_str());
162 ASSERT_EQ(root, std::filesystem::current_path().c_str());
165 ASSERT_EQ(root, std::filesystem::current_path().c_str());
167 ASSERT_EQ(start, std::filesystem::current_path().c_str());
172 TEST(ScopeGuards, Batch)
174 const bool start = gROOT->IsBatch();
177 ASSERT_EQ(
true, gROOT->IsBatch());
180 ASSERT_EQ(
false, gROOT->IsBatch());
182 ASSERT_EQ(
true, gROOT->IsBatch());
184 ASSERT_EQ(start, gROOT->IsBatch());
static ScopeGuard guardStreamState(std::basic_ios< CharT, Traits > &stream)
Create a ScopeGuard for the state of a stream to reset all the formatting at the end of the object li...
static ScopeGuard guardValue(T &reference)
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned ...
static ScopeGuard guardGetterSetter(Getter getter, Setter setter)
Create a ScopeGuard from a getter and setter: On construction the getter object is called to obtain a...
static ScopeGuard guardFunctor(Functor functor)
Create a ScopeGuard from a functor object with appropriately overloaded operator() calls to get and s...
static ScopeGuard guardBatchMode(bool batchMode=true)
Create a ScopeGuard to turn ROOT into batch mode and restore the initial batch mode status after the ...
static ScopeGuard guardWorkingDirectory()
Create a ScopeGuard of the current working directory.
Helper struct for the C++17 std::visit overload pattern to allow simple use of variants.