Skip to content

Commit

Permalink
docs: extend README.md example
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Oct 11, 2024
1 parent 7e6744d commit 290900c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,40 @@ TEST_CASE("Interface overload-sets are directly supported.")

</details>

<details>
<summary>Watching object-instances</summary>

``mimicpp::Watched`` helper can report destruction and relocations of object-instances.

```cpp
#include <mimic++/mimic++.hpp>

namespace expect = mimicpp::expect;
namespace then = mimicpp::then;

TEST_CASE("LifetimeWatcher and RelocationWatcher can trace object instances.")
{
mimicpp::Watched<
mimicpp::Mock<void()>,
mimicpp::LifetimeWatcher,
mimicpp::RelocationWatcher> watched{};

SCOPED_EXP watched.expect_destruct();
int relocationCounter{};
SCOPED_EXP watched.expect_relocate()
and then::invoke([&] { ++relocationCounter; })
and expect::at_least(1);

std::optional wrapped{std::move(watched)}; // satisfies one relocate-expectation
std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation
wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead
other.reset(); // fulfills the destruct-expectation
REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated
}
```
</details>
### Other Choices
#### Always Stay Within The Language Definition
Expand Down
8 changes: 4 additions & 4 deletions examples/Watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ TEST_CASE(
and expect::at_least(1);

std::optional wrapped{std::move(watched)}; // satisfies one relocate-expectation
std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation
wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead
other.reset(); // fulfills the destruct-expectation
REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated
std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation
wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead
other.reset(); // fulfills the destruct-expectation
REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated
//! [watched lifetime relocation]
}
STOP_WARNING_SUPPRESSION

0 comments on commit 290900c

Please sign in to comment.