102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
#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->failed_assertions = 0;
|
|
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);
|
|
}
|