template <typename T>
class ScopedValueChange
Defined at line 68 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
Sets a variable to a value. Restores the old value when going out of scope.
This implementation is geared towards use in testing code. It helps catch
usage errors, at the cost of efficiency.
This implementation is not thread-safe. The variable managed by a
ScopedValueChange must only be used in the thread where the ScopedValueChange
instance is created. ScopedValueChange instances must not be moved across
threads.
Each memory location can be covered by at most one ScopedValueChange at a
time. This limitation is enforced with a ZX_ASSERT(). The limitation removes
the mental model complexity stemming from having a variable covered by
multiple ScopedValueChanges with overlapping lifetimes. That complexity is
considered incompatible with the requirement for simplicity in testing code.
Example usage:
namespace {
int g_timeout_ms = 1'200;
} // namespace
ScopedValueChange
<int
> SomeSystem::OverrideTimeoutMsForTesting(
int timeout_ms) {
return ScopedValueChange(g_timeout_ms, timeout_ms);
}
TEST(SomeSystemTest, TimeoutScenario) {
ScopedValueChange
<int
> timeout_change =
SomeSystem::OverrideTimeoutMsForTesting(0);
// `g_timeout_ms` will be zero for the duration of the test.
// When `timeout_change` goes out of scope, the timeout will be restored
// to its initial value.
}
class ComplexSystemTest : public ::testing::Test {
public:
ComplexSystemTest() :
timeout_change_(SomeSystem::OverrideTimeoutMsForTesting(0)) {}
~ComplexSystemTest() override = default;
protected:
ScopedValueChange
<int
> timeout_change_;
};
TEST(ComplexSystemTest, ComplexTimeoutScenario) {
// `g_timeout_ms` will be zero here.
// This test needs to undo a variable change in a surrounding scope.
timeout_change_.reset();
timeout_change_ = SomeSystem::OverrideTimeoutMsForTesting(32);
// `g_timeout_ms` will be 32 for the rest of the test.
}
Public Methods
void ScopedValueChange<T> (T & variable, T temporary_value)
Sets `variable` to `temporary_value` and stashes the original value.
The caller must ensure that `variable` outlives the newly created instance.
The easiest way to meet this guarantee is to use static variables, whose
lifetime extends to the end of the process.
The caller must ensure that `variable` is not already covered by another
ScopedValueChange instance.
Defined at line 85 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
void ScopedValueChange<T> (const ScopedValueChange<T> & )
Defined at line 91 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
ScopedValueChange<T> & operator= (const ScopedValueChange<T> & )
Defined at line 92 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
void ScopedValueChange<T> (ScopedValueChange<T> && rhs)
Moving allowed so ScopedValueChange can be used as a return type.
Defined at line 95 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
ScopedValueChange<T> & operator= (ScopedValueChange<T> && rhs)
Defined at line 99 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
void ~ScopedValueChange<T> ()
Defined at line 105 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h
void reset ()
Empties this change, restoring the variable to its initial value.
After reset(), this ScopedValueChange will be empty, so it will no longer
change the variable when it goes out of scope.
Defined at line 111 of file ../../src/graphics/display/lib/driver-utils/scoped-value-change.h