|
9 | 9 | #include <exception> |
10 | 10 | #include <filesystem> |
11 | 11 | #include <format> |
| 12 | +#include <gtest/gtest-spi.h> |
12 | 13 | #include <gtest/gtest.h> |
13 | 14 | #include <iostream> |
14 | 15 | #include <ostream> |
| 16 | +#include <src/gtest-internal-inl.h> |
15 | 17 |
|
16 | 18 | namespace cucumber_cpp::library |
17 | 19 | { |
18 | | - EventListener::EventListener(cucumber::messages::test_step_result& testStepResult) |
19 | | - : testStepResult{ testStepResult } |
| 20 | + struct CucumberResultReporter : public testing::ScopedFakeTestPartResultReporter |
20 | 21 | { |
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 | + } |
28 | 27 |
|
29 | | - void EventListener::OnTestPartResult(const testing::TestPartResult& testPartResult) |
30 | | - { |
31 | | - if (testPartResult.failed()) |
| 28 | + void ReportTestPartResult(const testing::TestPartResult& testPartResult) |
32 | 29 | { |
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; |
34 | 33 |
|
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(); |
36 | 35 |
|
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() }; |
41 | 44 | } |
42 | 45 |
|
43 | | - if (testPartResult.fatally_failed()) |
44 | | - throw FatalError{ testPartResult.message() }; |
45 | | - } |
| 46 | + private: |
| 47 | + cucumber::messages::test_step_result& testStepResult; |
| 48 | + }; |
46 | 49 |
|
47 | 50 | cucumber::messages::test_step_result Body::ExecuteAndCatchExceptions(const ExecuteArgs& args) |
48 | 51 | { |
49 | 52 | cucumber::messages::test_step_result testStepResult{ .status = cucumber::messages::test_step_result_status::PASSED }; |
50 | | - EventListener eventListener{ testStepResult }; |
| 53 | + CucumberResultReporter reportListener{ testStepResult }; |
51 | 54 |
|
52 | 55 | try |
53 | 56 | { |
|
0 commit comments