feat(testing-mock): complete P2 M-TESTINGMOCK-01..04 sessions, action-outside-lock, move-only, combinators
This commit is contained in:
1 parent
86d777eae6
commit
658c21a2fa
8 files changed
+1702
-30
No files matched your search
@@ -292,7 +292,7 @@ if(_has_fsm)
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
has_modules(_has_net Net Functional Text)
|
||||
has_modules(_has_net Net Functional Text Time)
|
||||
if(_has_net)
|
||||
run_consumer(NetOnly "${install_prefix}" "NetOnly"
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
|
||||
@@ -300,6 +300,11 @@ if(_has_net)
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
has_modules(_has_testing_mock TestingMock Testing Functional Core)
|
||||
if(_has_testing_mock)
|
||||
run_consumer(TestingMockOnly "${install_prefix}" "TestingMockOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||
if(_has_foundation)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidTestingMockOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 26)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS TestingMock)
|
||||
|
||||
add_executable(testing_mock_only_consumer main.cpp)
|
||||
target_link_libraries(testing_mock_only_consumer PRIVATE Void::TestingMock)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME testing_mock_only_consumer COMMAND testing_mock_only_consumer)
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <Void/TestingMock/Matcher.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
#include <Void/TestingMock/Session.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
|
||||
MockSession session;
|
||||
MockFunction<std::unique_ptr<int>(std::string)> fn{session, "lookup"};
|
||||
auto exp = fn.try_expect(
|
||||
AllOf<std::string>(StartsWith<std::string>("k_"),
|
||||
EndsWith<std::string>("_1")));
|
||||
if (!exp.has_value()) {
|
||||
return 1;
|
||||
}
|
||||
(*exp)->returns_once(std::make_unique<int>(42));
|
||||
if (!freeze(session, Concurrency::SingleThread).has_value()) {
|
||||
return 2;
|
||||
}
|
||||
auto out = fn("k_ok_1");
|
||||
if (!out || *out != 42) {
|
||||
return 3;
|
||||
}
|
||||
auto rep = verify_and_close(session);
|
||||
if (!rep.has_value() || !rep->ok()) {
|
||||
return 4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/TestingMock/Matcher.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
#include <Void/TestingMock/Session.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct NonDefaultResult {
|
||||
int value;
|
||||
explicit NonDefaultResult(int v) : value(v) {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test testing_mock_session_reentrant_move_only_and_sequences()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
|
||||
MockSession session;
|
||||
Sequence seq;
|
||||
|
||||
// 1. Same-thread reentrant mock action works because actions run outside
|
||||
// internal locks.
|
||||
MockFunction<int(int)> reentrant{session, "reentrant"};
|
||||
auto e1 = reentrant.try_expect(Eq(1));
|
||||
Void::Testing::require(e1.has_value());
|
||||
(*e1)->in_sequence(seq).invokes([&](int) -> int { return reentrant(2) + 1; });
|
||||
|
||||
auto e2 = reentrant.try_expect(Eq(2));
|
||||
Void::Testing::require(e2.has_value());
|
||||
(*e2)->in_sequence(seq).returns(41);
|
||||
|
||||
// 2. Move-only argument and move-only return value via returns_once /
|
||||
// invokes_once.
|
||||
MockFunction<std::unique_ptr<int>(std::unique_ptr<int>)> move_fn{session,
|
||||
"move_fn"};
|
||||
auto me1 = move_fn.try_expect(NotNull<std::unique_ptr<int>>());
|
||||
Void::Testing::require(me1.has_value());
|
||||
(*me1)->invokes_once([bonus = std::make_unique<int>(100)](
|
||||
std::unique_ptr<int> in) {
|
||||
return std::make_unique<int>(*in + *bonus);
|
||||
});
|
||||
|
||||
auto me2 = move_fn.try_expect(IsNull<std::unique_ptr<int>>());
|
||||
Void::Testing::require(me2.has_value());
|
||||
(*me2)->returns_once(std::make_unique<int>(7));
|
||||
|
||||
// 3. Matcher combinators: AllOf, AnyOf, NotOf, Ne, EndsWith.
|
||||
MockFunction<int(std::string)> combo{session, "combo"};
|
||||
auto ce = combo.try_expect(AllOf<std::string>(
|
||||
StartsWith<std::string>("pre_"), EndsWith<std::string>("_suf"),
|
||||
NotOf(Eq<std::string>("pre_bad_suf"))));
|
||||
Void::Testing::require(ce.has_value());
|
||||
(*ce)->returns(99).at_least(1);
|
||||
|
||||
// Freeze session in SingleThread mode.
|
||||
Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value());
|
||||
|
||||
// Mutating a frozen session returns InvalidState.
|
||||
auto frozen_add = reentrant.try_expect(Eq(999));
|
||||
Void::Testing::expect(!frozen_add.has_value());
|
||||
|
||||
// Execute reentrant sequenced calls.
|
||||
Void::Testing::expect_eq(reentrant(1), 42);
|
||||
|
||||
// Execute move-only calls.
|
||||
auto r1 = move_fn(std::make_unique<int>(23));
|
||||
Void::Testing::require(r1 != nullptr);
|
||||
Void::Testing::expect_eq(*r1, 123);
|
||||
auto r2 = move_fn(nullptr);
|
||||
Void::Testing::require(r2 != nullptr);
|
||||
Void::Testing::expect_eq(*r2, 7);
|
||||
|
||||
// Execute combinator match.
|
||||
Void::Testing::expect_eq(combo("pre_ok_suf"), 99);
|
||||
|
||||
// verify_and_close() is idempotent and succeeds.
|
||||
auto v1 = verify_and_close(session);
|
||||
Void::Testing::require(v1.has_value());
|
||||
Void::Testing::expect(v1->ok());
|
||||
auto v2 = verify_and_close(session);
|
||||
Void::Testing::require(v2.has_value());
|
||||
Void::Testing::expect(v2->ok());
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_mock_prohibited_never_non_default_and_deadlock_check()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
|
||||
MockSession session;
|
||||
MockFunction<NonDefaultResult(int)> fn{session, "strict_fn"};
|
||||
|
||||
// Explicit never() checked before ordinary wildcard matcher.
|
||||
auto p = fn.try_expect(Eq(13));
|
||||
Void::Testing::require(p.has_value());
|
||||
(*p)->never();
|
||||
|
||||
auto ok = fn.try_expect(_<int>());
|
||||
Void::Testing::require(ok.has_value());
|
||||
(*ok)->invokes([&](int x) -> NonDefaultResult {
|
||||
// Calling verify_and_close from inside an active action must return
|
||||
// WouldDeadlock instead of deadlocking.
|
||||
auto dl = verify_and_close(session);
|
||||
Void::Testing::expect(!dl.has_value());
|
||||
return NonDefaultResult{x * 2};
|
||||
}).times(1);
|
||||
|
||||
Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value());
|
||||
|
||||
auto res = fn(5);
|
||||
Void::Testing::expect_eq(res.value, 10);
|
||||
|
||||
// Prohibited call with non-default-constructible return type records
|
||||
// prohibited failure and throws InvocationAbortException.
|
||||
bool aborted = false;
|
||||
try {
|
||||
(void)fn(13);
|
||||
} catch (const InvocationAbortException&) {
|
||||
aborted = true;
|
||||
}
|
||||
Void::Testing::expect(aborted);
|
||||
|
||||
auto report = verify_and_close(session);
|
||||
Void::Testing::require(report.has_value());
|
||||
Void::Testing::expect(!report->ok());
|
||||
Void::Testing::expect(!report->prohibited.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_mock_concurrent_actions_and_bounds()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
|
||||
MockLimits lim;
|
||||
lim.max_expectations = 4;
|
||||
lim.max_events = 8;
|
||||
MockSession session{lim};
|
||||
|
||||
MockFunction<int(int)> fn{session, "concurrent_fn"};
|
||||
// Two separate expectations with 8 calls each, contested by 4 threads.
|
||||
auto e1 = fn.try_expect(Lt(100));
|
||||
auto e2 = fn.try_expect(Lt(100));
|
||||
Void::Testing::require(e1.has_value());
|
||||
Void::Testing::require(e2.has_value());
|
||||
(*e1)->invokes([](int x) { return x + 1; }).times(8);
|
||||
(*e2)->invokes([](int x) { return x + 2; }).times(8);
|
||||
|
||||
// Enforce max_expectations = 4.
|
||||
auto e3 = fn.try_expect(Eq(200));
|
||||
auto e4 = fn.try_expect(Eq(201));
|
||||
auto e5 = fn.try_expect(Eq(202));
|
||||
Void::Testing::require(e3.has_value());
|
||||
Void::Testing::require(e4.has_value());
|
||||
Void::Testing::expect(!e5.has_value());
|
||||
(*e3)->at_most(1);
|
||||
(*e4)->at_most(1);
|
||||
|
||||
Void::Testing::require(
|
||||
freeze(session, Concurrency::ConcurrentActions).has_value());
|
||||
|
||||
std::atomic<int> sum{0};
|
||||
std::vector<std::thread> threads;
|
||||
for (int t = 0; t < 4; ++t) {
|
||||
threads.emplace_back([&]() {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
sum.fetch_add(fn(10), std::memory_order_relaxed);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& th : threads) {
|
||||
th.join();
|
||||
}
|
||||
|
||||
// 8 * 11 + 8 * 12 = 184
|
||||
Void::Testing::expect_eq(sum.load(), 184);
|
||||
|
||||
auto report = verify_and_close(session);
|
||||
Void::Testing::require(report.has_value());
|
||||
Void::Testing::expect(report->ok());
|
||||
Void::Testing::expect_eq(report->total_events, std::uint64_t{16});
|
||||
Void::Testing::expect(report->events_truncated);
|
||||
Void::Testing::expect(report->events.size() <= lim.max_events);
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_mock_single_thread_detects_wrong_thread_and_matcher_exception()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
|
||||
MockSession session;
|
||||
MockFunction<int(int)> fn{session, "st_fn"};
|
||||
auto e = fn.try_expect(Pred<int>([](const int& v) -> bool {
|
||||
if (v < 0) {
|
||||
throw std::runtime_error("boom-matcher");
|
||||
}
|
||||
return v == 1;
|
||||
}));
|
||||
Void::Testing::require(e.has_value());
|
||||
(*e)->returns(7).at_most(1);
|
||||
|
||||
Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value());
|
||||
|
||||
// Throwing matcher becomes MatcherFailure instead of crashing/aborting.
|
||||
(void)fn(-5);
|
||||
|
||||
// Wrong-thread call under Concurrency::SingleThread records threading error.
|
||||
std::thread other([&]() { (void)fn(1); });
|
||||
other.join();
|
||||
|
||||
auto report = verify_and_close(session);
|
||||
Void::Testing::require(report.has_value());
|
||||
Void::Testing::expect(!report->ok());
|
||||
Void::Testing::expect(!report->matcher_errors.empty());
|
||||
Void::Testing::expect(!report->threading_errors.empty());
|
||||
}
|
||||
Reference in new issue
Block a user