Add coverage for fail path

This commit is contained in:
Alex Kremer
2026-09-02 12:08:55 +01:00
parent a1e9e4943a
commit cad37682eb
2 changed files with 45 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
#include <gmock/gmock.h>
#include <rpcspec/Aliases.hpp>
#include <rpcspec/Converters.hpp>
#include <rpcspec/Errors.hpp>
#include <rpcspec/FieldSpec.hpp>
#include <rpcspec/HandlerFor.hpp>
#include <rpcspec/Typed.hpp>
@@ -201,4 +202,16 @@ public:
}
};
class FailingTypedHandlerFake : public rpc::spec::HandlerFor<typed_fake::TypedInput> {
public:
using Output = TestOutput;
using Result = rpc::HandlerReturnType<Output>;
static Result
process([[maybe_unused]] Input const& input, [[maybe_unused]] rpc::Context const& ctx)
{
return rpc::Error{rpc::Status{"Very custom error"}};
}
};
} // namespace tests::common

View File

@@ -1,3 +1,4 @@
#include "rpc/Errors.hpp"
#include "rpc/FakesAndMocks.hpp"
#include "rpc/common/Concepts.hpp"
#include "rpc/common/Specs.hpp"
@@ -74,6 +75,7 @@ TEST_F(RPCDefaultProcessorTest, InvalidInput)
// every test below would still pass.
static_assert(SomeHandlerWithTypedInput<TypedHandlerFake>);
static_assert(not SomeHandlerWithInput<TypedHandlerFake>);
static_assert(SomeHandlerWithTypedInput<FailingTypedHandlerFake>);
static_assert(SomeHandlerWithInput<HandlerMock>);
static_assert(not SomeHandlerWithTypedInput<HandlerMock>);
@@ -125,6 +127,36 @@ TEST_F(RPCDefaultProcessorTest, NewSpecHandler_DeprecatedField_WarningsForwarded
});
}
TEST_F(RPCDefaultProcessorTest, NewSpecHandler_HandlerReturnsError_ForwardsError)
{
runSpawn([](auto yield) {
FailingTypedHandlerFake const handler;
rpc::impl::DefaultProcessor<FailingTypedHandlerFake> const processor;
auto const input = boost::json::parse(R"JSON({ "hello": "world", "limit": 42 })JSON");
auto const ret = processor(handler, input, Context{yield});
ASSERT_FALSE(ret);
EXPECT_EQ(rpc::makeError(ret.result.error()).at("error").as_string(), "Very custom error");
EXPECT_TRUE(ret.warnings.empty());
});
}
TEST_F(RPCDefaultProcessorTest, NewSpecHandler_HandlerReturnsError_StillForwardsWarnings)
{
runSpawn([](auto yield) {
FailingTypedHandlerFake const handler;
rpc::impl::DefaultProcessor<FailingTypedHandlerFake> const processor;
auto const input = boost::json::parse(R"JSON({ "hello": "world", "old_field": true })JSON");
auto const ret = processor(handler, input, Context{yield});
ASSERT_FALSE(ret);
EXPECT_EQ(rpc::makeError(ret.result.error()).at("error").as_string(), "Very custom error");
EXPECT_EQ(ret.warnings.size(), 1);
});
}
TEST_F(RPCDefaultProcessorTest, NewSpecHandler_DeprecatedFieldAbsent_NoWarnings)
{
runSpawn([](auto yield) {