Skip to content

Commit ece0b62

Browse files
committed
use ScopedFakeTestPartResultReporter instead of EmptyTestEventListener to catch errors
1 parent 6eb2da0 commit ece0b62

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

cucumber_cpp/library/Body.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,48 @@
99
#include <exception>
1010
#include <filesystem>
1111
#include <format>
12+
#include <gtest/gtest-spi.h>
1213
#include <gtest/gtest.h>
1314
#include <iostream>
1415
#include <ostream>
16+
#include <src/gtest-internal-inl.h>
1517

1618
namespace cucumber_cpp::library
1719
{
18-
EventListener::EventListener(cucumber::messages::test_step_result& testStepResult)
19-
: testStepResult{ testStepResult }
20+
struct CucumberResultReporter : public testing::ScopedFakeTestPartResultReporter
2021
{
21-
testing::UnitTest::GetInstance()->listeners().Append(this);
22-
}
23-
24-
EventListener::~EventListener()
25-
{
26-
testing::UnitTest::GetInstance()->listeners().Release(this);
27-
}
22+
explicit CucumberResultReporter(cucumber::messages::test_step_result& testStepResult)
23+
: testing::ScopedFakeTestPartResultReporter{ nullptr }
24+
, testStepResult{ testStepResult }
25+
{
26+
}
2827

29-
void EventListener::OnTestPartResult(const testing::TestPartResult& testPartResult)
30-
{
31-
if (testPartResult.failed())
28+
void ReportTestPartResult(const testing::TestPartResult& testPartResult)
3229
{
33-
testStepResult.status = cucumber::messages::test_step_result_status::FAILED;
30+
if (testPartResult.failed())
31+
{
32+
testStepResult.status = cucumber::messages::test_step_result_status::FAILED;
3433

35-
auto fileName = std::filesystem::relative(testPartResult.file_name(), std::filesystem::current_path()).string();
34+
auto fileName = std::filesystem::relative(testPartResult.file_name(), std::filesystem::current_path()).string();
3635

37-
if (testStepResult.message)
38-
testStepResult.message = std::format("{}\n{}:{}: Failure\n{}", testStepResult.message.value(), fileName, testPartResult.line_number(), testPartResult.message());
39-
else
40-
testStepResult.message = std::format("{}:{}: Failure\n{}", fileName, testPartResult.line_number(), testPartResult.message());
36+
if (testStepResult.message)
37+
testStepResult.message = std::format("{}\n{}:{}: Failure\n{}", testStepResult.message.value(), fileName, testPartResult.line_number(), testPartResult.message());
38+
else
39+
testStepResult.message = std::format("{}:{}: Failure\n{}", fileName, testPartResult.line_number(), testPartResult.message());
40+
}
41+
42+
if (testPartResult.fatally_failed())
43+
throw FatalError{ testPartResult.message() };
4144
}
4245

43-
if (testPartResult.fatally_failed())
44-
throw FatalError{ testPartResult.message() };
45-
}
46+
private:
47+
cucumber::messages::test_step_result& testStepResult;
48+
};
4649

4750
cucumber::messages::test_step_result Body::ExecuteAndCatchExceptions(const ExecuteArgs& args)
4851
{
4952
cucumber::messages::test_step_result testStepResult{ .status = cucumber::messages::test_step_result_status::PASSED };
50-
EventListener eventListener{ testStepResult };
53+
CucumberResultReporter reportListener{ testStepResult };
5154

5255
try
5356
{

cucumber_cpp/library/Body.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ namespace cucumber_cpp::library
2727
using std::runtime_error::runtime_error;
2828
};
2929

30-
struct EventListener : testing::EmptyTestEventListener
31-
{
32-
explicit EventListener(cucumber::messages::test_step_result& testStepResult);
33-
~EventListener();
34-
35-
void OnTestPartResult(const testing::TestPartResult& testPartResult) override;
36-
37-
private:
38-
cucumber::messages::test_step_result& testStepResult;
39-
};
40-
4130
struct Body
4231
{
4332
virtual ~Body() = default;

0 commit comments

Comments
 (0)