diff options
| author | Leitwolf11 | 2026-11-01 19:04:52 +0300 |
|---|---|---|
| committer | Leitwolf11 | 2026-11-01 19:04:52 +0300 |
| commit | c51d15273e87267215e5877edc268e7db354187b (patch) | |
| tree | bc314b97cbb793ea136ceabecc3d565240ae7cf2 /tests/Progmasoft/RunnerFixture.cpp | |
| parent | 26a1588ae3ae183ae5c900caf3c294bdcb3f8855 (diff) | |
| download | catch3-c51d15273e87267215e5877edc268e7db354187b.tar.gz | |
Add mocks that report through the assertion path
Step 3 of the roadmap. MockFunction checks the calls of one function
against its expectations; CATCH3_MOCK_METHOD declares one for a virtual
member, and CATCH3_MOCK_FUNCTION and ScopedSeam serve free functions
behind a seam.
Expectations take values, Catch matchers, Any or Where; count calls with
Times, Between, AtLeast, AtMost, AnyTimes and Never; answer with Returns,
Does or Throws; and order calls across mocks with Sequence. A failure
goes through AssertionHandler at the line of the expectation, so every
report of the runner shows it. Met expectations count as passed
assertions. A test case that is already being left does not get unmet
expectations on top of its own failure.
Composed matchers refer to temporaries, so they are rejected at compile
time rather than left dangling. A call that fails returns a
value-initialized result, or leaves the test case when no such result
exists.
The failure paths are tested through the runner fixture, reading the
messages from its JSON report.
Diffstat (limited to 'tests/Progmasoft/RunnerFixture.cpp')
| -rw-r--r-- | tests/Progmasoft/RunnerFixture.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/Progmasoft/RunnerFixture.cpp b/tests/Progmasoft/RunnerFixture.cpp index 37d196a3..2796912f 100644 --- a/tests/Progmasoft/RunnerFixture.cpp +++ b/tests/Progmasoft/RunnerFixture.cpp @@ -4,6 +4,7 @@ // A test binary whose tests misbehave on purpose. The runner's integration // tests start it and check what the runner made of each misbehaviour. +#include <Progmasoft/Catch3/Mock.hpp> #include <Progmasoft/Catch3/Runner.hpp> #include <Progmasoft/Catch3/TestMacros.hpp> #include <chrono> @@ -184,6 +185,72 @@ TEST_CASE("fixture slow four", "[fixture][slow]") CHECK(true); } +TEST_CASE("fixture mock unexpected call", "[fixture][mock]") +{ + Progmasoft::Catch3::MockFunction<int(std::string)> get{ "Get" }; + CATCH3_EXPECT_CALL(get)("a").Returns(1); + CHECK(get("a") == 1); + CHECK(get("b") == 0); +} + +TEST_CASE("fixture mock with no expectations", "[fixture][mock]") +{ + CATCH3_MOCK_FUNCTION(tick, void()); + tick(); +} + +TEST_CASE("fixture mock unmet expectation", "[fixture][mock]") +{ + CATCH3_MOCK_FUNCTION(tick, void()); + CATCH3_EXPECT_CALL(tick)().Times(2); + tick(); +} + +TEST_CASE("fixture mock too many calls", "[fixture][mock]") +{ + CATCH3_MOCK_FUNCTION(tick, void()); + CATCH3_EXPECT_CALL(tick)(); + tick(); + tick(); +} + +TEST_CASE("fixture mock out of order", "[fixture][mock]") +{ + Progmasoft::Catch3::Sequence order; + CATCH3_MOCK_FUNCTION(first, void()); + CATCH3_MOCK_FUNCTION(second, void()); + CATCH3_EXPECT_CALL(first)().InSequence(order); + CATCH3_EXPECT_CALL(second)().InSequence(order); + second(); +} + +TEST_CASE("fixture mock past its step", "[fixture][mock]") +{ + Progmasoft::Catch3::Sequence order; + CATCH3_MOCK_FUNCTION(first, void()); + CATCH3_MOCK_FUNCTION(second, void()); + CATCH3_EXPECT_CALL(first)().AtLeast(1).InSequence(order); + CATCH3_EXPECT_CALL(second)().InSequence(order); + first(); + second(); + first(); +} + +TEST_CASE("fixture mock with nothing to return", "[fixture][mock]") +{ + CATCH3_MOCK_FUNCTION(name, std::string & ()); + CATCH3_EXPECT_CALL(name)(); + name(); + FAIL("the call above leaves the test case"); +} + +TEST_CASE("fixture mock left by a failure", "[fixture][mock]") +{ + CATCH3_MOCK_FUNCTION(tick, void()); + CATCH3_EXPECT_CALL(tick)(); + FAIL("the test's own failure"); +} + TEST_CASE("fixture skips", "[fixture][skip]") { SKIP("not on this machine"); |