mimic++ v9.2.1
Loading...
Searching...
No Matches
mimicpp::LifetimeWatcher Class Reference

A watcher type, which reports it's destructor calls. More...

#include <ObjectWatcher.hpp>

Public Member Functions

 ~LifetimeWatcher () noexcept(false)
 Destructor, which reports the call.
 
 LifetimeWatcher ()=default
 Default constructor.
 
template<typename Base>
 LifetimeWatcher (const for_base_tag< Base >)
 
 LifetimeWatcher (const LifetimeWatcher &other)
 Copy-constructor.
 
LifetimeWatcheroperator= (const LifetimeWatcher &other)
 Copy-assignment-operator.
 
 LifetimeWatcher (LifetimeWatcher &&)=default
 Defaulted move-constructor.
 
LifetimeWatcheroperator= (LifetimeWatcher &&)=default
 Defaulted move-assignment-operator.
 
auto expect_destruct ()
 Begins a destruction-expectation construction.
 

Detailed Description

A watcher type, which reports it's destructor calls.

This watcher is designed to track, whether the destructor has been called. During its destructor call, it reports the destruction to the framework, which can be tracked by a previously created destruction-expectation.

namespace expect = mimicpp::expect;
// imagine this to be a function, we wanted to test
constexpr auto some_function = [](auto fun) {
fun(); // let's just invoke the given fun.
};
mimicpp::Mock<void()>,
watched{}; // let's create a watched mock
// Let's say, we are very suspicious and want to get sure, that ``some_function``
// invokes the provided functional, before its getting destroyed.
mimicpp::Sequence sequence{};
SCOPED_EXP watched.expect_call()
and expect::in_sequence(sequence);
SCOPED_EXP watched.expect_destruct()
and expect::in_sequence(sequence);
// pass the mock to ``some_function`` and track from the outside, whether the expectations hold
some_function(std::move(watched));
// nothing to do here. Violations will be reported automatically (as usual).

Moving

This watcher can be freely moved around.

Copying

This watcher is copyable, but with very special behaviour.

As this watcher is generally designed to be part of a bigger object, it would be very limiting not supporting copy-operations at all. The question is, how should a copy look like?

In general a copy should be a logical duplicate of its source and the general expectation is: if B is a copy of A, then A == B should yield true.

Note
This doesn't say, that if B is not a copy of A, then A == B has to yield false!

This won't be the case for LifetimeWatchers, as active destruction-expectations won't be copied over to the target. In general, if a LifetimeWatcher is used, we want to be very precise with our object-lifetime, thus an implicit expectation copy would be against the purpose of this helper. Due to this, each LifetimeWatcher will be created as a fresh instance, when copy-construction is used. The same logic also applies to copy-assignment.


The documentation for this class was generated from the following file: