Skip to content

Commit

Permalink
docs: extend readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Sep 26, 2024
1 parent 67d703d commit 0816ea1
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ So, Mocks and Expectations are going together hand in hand.

### Examples

#### Mocks as function objects

As already said, ``mimicpp::mock``s are already function objects.

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

Expand Down Expand Up @@ -111,6 +115,81 @@ TEST_CASE("Mocks can be overloaded.")
}
```
#### Mocks as member functions
``mimicpp::mock``s can also be used as member functions.
```cpp
#include <mimic++/mimic++.hpp>
namespace finally = mimicpp::finally;
// let's build a functions, which actually expects an object, which has a .get() member function,
// which returns something printable
inline void foo(const auto& obj)
{
std::cout << obj.get();
}
TEST_CASE("Mocks can be used as member functions.")
{
struct Mock
{
mimicpp::Mock<int() const> get{}; // that serves as the .get() member function
};
Mock mock{};
SCOPED_EXP mock.get.expect_call()
and finally::returns(42);
foo(mock); // fine, foo calls the get() member-function
}
```

#### Mocking interfaces

``mimicpp`` also provides helpers for interface mocking.

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

namespace finally = mimicpp::finally;

// let's say, we have the following interface
class Interface
{
public:
virtual ~Interface() = default;
virtual int get() const = 0;
};

// and a functions, which this time actually requires an interface.
inline void foo(const Interface& obj)
{
std::cout << obj.get();
}

TEST_CASE("Interface can be mocked.")
{
class Derived
: public Interface
{
public:
~Derived() override = default;

// this generates the override method and a mock object named foo_
MOCK_METHOD(get, int, (), const);
};

Derived mock{};
SCOPED_EXP mock.get_.expect_call() // note the _ suffix. That's the name of the mock object.
and finally::returns(42);

foo(mock); // fine, foo calls the get() member-function, which forwards the call to the foo_ member.
}
```

Check notice on line 192 in README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

README.md#L192

Expected: 1; Actual: 2
### Other Choices
#### Always Stay Within The Language Definition
Expand Down

0 comments on commit 0816ea1

Please sign in to comment.