1 parent
f0825fbd1a
commit
42268c6c35
45 files changed
+2732
-262
No files matched your search
@@ -0,0 +1,100 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
// Capture any failed assertions added by `fn()` and clear them from
|
||||
// the current test result so this test can itself pass while still
|
||||
// inspecting the captured messages.
|
||||
template <typename Fn> std::vector<std::string> capture_failures(Fn&& fn)
|
||||
{
|
||||
auto& ctx = Void::Testing::Detail::TestContext::current();
|
||||
auto& assertions = ctx.current_result->assertions;
|
||||
const std::size_t saved = assertions.size();
|
||||
|
||||
std::forward<Fn>(fn)();
|
||||
|
||||
std::vector<std::string> messages;
|
||||
for (std::size_t i = saved; i < assertions.size(); ++i) {
|
||||
if (!assertions[i].passed) {
|
||||
messages.push_back(assertions[i].message);
|
||||
}
|
||||
}
|
||||
// Drop everything we recorded so the test result stays clean.
|
||||
assertions.erase(assertions.begin() + saved, assertions.end());
|
||||
ctx.current_result->state = Void::Testing::TestState::Passed;
|
||||
return messages;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test failure_message_includes_actual_args()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
auto messages = capture_failures([] {
|
||||
MockFunction<int(int, std::string)> fn{"do_thing"};
|
||||
fn.expect(Eq(1), Eq(std::string{"alpha"})).returns(0);
|
||||
(void)fn(99, std::string{"beta"});
|
||||
});
|
||||
|
||||
Void::Testing::expect(!messages.empty());
|
||||
const auto& msg = messages.front();
|
||||
Void::Testing::expect(msg.find("do_thing(99") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("\"beta\"") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test failure_message_lists_each_expectation()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
auto messages = capture_failures([] {
|
||||
MockFunction<int(int)> fn{"do_thing"};
|
||||
fn.expect(Eq(1)).returns(10);
|
||||
fn.expect(Range(50, 60)).returns(20);
|
||||
(void)fn(99);
|
||||
});
|
||||
|
||||
Void::Testing::expect(!messages.empty());
|
||||
const auto& msg = messages.front();
|
||||
Void::Testing::expect(msg.find("expectation 1") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("Eq(1)") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("expectation 2") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("Range(50, 60)") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("0/1 calls") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test failure_message_when_no_expectations_registered()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
auto messages = capture_failures([] {
|
||||
MockFunction<int(int)> fn{"do_thing"};
|
||||
(void)fn(42);
|
||||
});
|
||||
|
||||
Void::Testing::expect(!messages.empty());
|
||||
Void::Testing::expect(messages.front().find("no expectations registered") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test verify_failure_message_describes_unsatisfied_expectation()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
auto messages = capture_failures([] {
|
||||
MockFunction<int(int)> fn{"do_thing"};
|
||||
fn.expect(Gt(100)).returns(0).times(2);
|
||||
// Never called — destructor fires verify(), which fails.
|
||||
});
|
||||
|
||||
Void::Testing::expect(!messages.empty());
|
||||
const auto& msg = messages.front();
|
||||
Void::Testing::expect(msg.find("unsatisfied expectation") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(msg.find("do_thing") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("Gt(100)") != std::string::npos);
|
||||
Void::Testing::expect(msg.find("0/2 calls") != std::string::npos);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
Void::Testing::Test lt_le_gt_ge_match_appropriately()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(int)> fn{"f"};
|
||||
fn.expect(Lt(10)).returns(1).times(1);
|
||||
fn.expect(Le(20)).returns(2).times(1);
|
||||
fn.expect(Gt(30)).returns(3).times(1);
|
||||
fn.expect(Ge(40)).returns(4).times(1);
|
||||
|
||||
Void::Testing::expect_eq(fn(5), 1);
|
||||
Void::Testing::expect_eq(fn(15), 2);
|
||||
Void::Testing::expect_eq(fn(35), 3);
|
||||
Void::Testing::expect_eq(fn(40), 4);
|
||||
}
|
||||
|
||||
Void::Testing::Test range_matches_inclusive_bounds()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(int)> fn{"in_range"};
|
||||
fn.expect(Range(10, 20)).returns(99).times(3);
|
||||
|
||||
Void::Testing::expect_eq(fn(10), 99); // lower bound
|
||||
Void::Testing::expect_eq(fn(15), 99); // middle
|
||||
Void::Testing::expect_eq(fn(20), 99); // upper bound
|
||||
}
|
||||
|
||||
Void::Testing::Test pred_matcher_invokes_user_callable()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(int)> fn{"is_even"};
|
||||
fn.expect(Pred<int>([](const int& v) { return v % 2 == 0; }))
|
||||
.returns(1)
|
||||
.times(2);
|
||||
|
||||
Void::Testing::expect_eq(fn(4), 1);
|
||||
Void::Testing::expect_eq(fn(8), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test starts_with_matches_string_prefix()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(std::string_view)> fn{"prefix"};
|
||||
fn.expect(StartsWith<std::string_view>("hello")).returns(1).times(2);
|
||||
|
||||
Void::Testing::expect_eq(fn("hello world"), 1);
|
||||
Void::Testing::expect_eq(fn("hello"), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test contains_matches_substring()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(std::string_view)> fn{"contains"};
|
||||
fn.expect(Contains<std::string_view>("foo")).returns(1).times(2);
|
||||
|
||||
Void::Testing::expect_eq(fn("a foo b"), 1);
|
||||
Void::Testing::expect_eq(fn("foobar"), 1);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
// std::move_only_function support: a unique_ptr-capturing lambda used to
|
||||
// fail to compile against std::function. With Phase 2.3.4 the action
|
||||
// holder is std::move_only_function, so this works.
|
||||
Void::Testing::Test invokes_accepts_unique_ptr_capturing_lambda()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockFunction<int(int)> fn{"counter"};
|
||||
|
||||
auto state = std::make_unique<int>(10);
|
||||
|
||||
fn.expect(_<int>())
|
||||
.invokes(
|
||||
[s = std::move(state)](int delta) -> int { return *s + delta; })
|
||||
.times(2);
|
||||
|
||||
Void::Testing::expect_eq(fn(1), 11);
|
||||
Void::Testing::expect_eq(fn(5), 15);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/TestingMock/Mock.hpp>
|
||||
#include <Void/TestingMock/MockFunction.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
// Minimal interface to mock.
|
||||
struct Service {
|
||||
virtual ~Service() = default;
|
||||
virtual int compute(int) = 0;
|
||||
virtual void log(int) = 0;
|
||||
};
|
||||
|
||||
class MockService : public Void::TestingMock::Mock<MockService>,
|
||||
public Service
|
||||
{
|
||||
public:
|
||||
|
||||
Void::TestingMock::MockFunction<int(int)> compute_mock{*this,
|
||||
"compute"};
|
||||
Void::TestingMock::MockFunction<void(int)> log_mock{*this, "log"};
|
||||
|
||||
int compute(int n) override { return compute_mock(n); }
|
||||
void log(int n) override { log_mock(n); }
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// Both registered MockFunctions are exercised — verify_all() succeeds.
|
||||
Void::Testing::Test verify_all_passes_when_all_expectations_met()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockService svc;
|
||||
svc.compute_mock.expect(Eq(1)).returns(42);
|
||||
svc.log_mock.expect(Eq(2));
|
||||
|
||||
Void::Testing::expect_eq(svc.compute(1), 42);
|
||||
svc.log(2);
|
||||
|
||||
svc.verify_all();
|
||||
}
|
||||
|
||||
// register_function is reachable directly for users who don't want to
|
||||
// use the constructor-overload registration (e.g. dynamically created
|
||||
// MockFunctions).
|
||||
Void::Testing::Test register_function_is_publicly_callable()
|
||||
{
|
||||
using namespace Void::TestingMock;
|
||||
MockService svc;
|
||||
// The two members already auto-registered; svc.verify_all() should
|
||||
// be a no-op since the two MockFunctions in the class were never
|
||||
// expected to be called.
|
||||
svc.compute_mock.expect(Eq(7)).returns(0);
|
||||
svc.log_mock.expect(Eq(8));
|
||||
(void)svc.compute(7);
|
||||
svc.log(8);
|
||||
svc.verify_all();
|
||||
}
|
||||
Reference in new issue
Block a user