|
| 1 | +#include "cucumber_cpp/library/engine/ExecutionContext.hpp" |
| 2 | +#include "base64.hpp" |
| 3 | +#include "cucumber/messages/attachment.hpp" |
| 4 | +#include "cucumber/messages/attachment_content_encoding.hpp" |
| 5 | +#include "cucumber/messages/test_run_hook_started.hpp" |
| 6 | +#include "cucumber/messages/test_step_started.hpp" |
| 7 | +#include "cucumber_cpp/CucumberCpp.hpp" |
| 8 | +#include "cucumber_cpp/library/support/Timestamp.hpp" |
| 9 | +#include "cucumber_cpp/library/util/Broadcaster.hpp" |
| 10 | +#include <istream> |
| 11 | +#include <iterator> |
| 12 | +#include <optional> |
| 13 | +#include <string> |
| 14 | +#include <utility> |
| 15 | +#include <variant> |
| 16 | + |
| 17 | +namespace cucumber_cpp::library::engine |
| 18 | +{ |
| 19 | + namespace |
| 20 | + { |
| 21 | + constexpr auto LogMediaType{ "text/x.cucumber.log+plain" }; |
| 22 | + constexpr auto LinkMediaType{ "text/uri-list" }; |
| 23 | + |
| 24 | + std::pair<std::optional<std::string>, std::optional<std::string>> ReadTestStepStartedIds(StepOrHookStarted stepOrHookStarted) |
| 25 | + { |
| 26 | + if (std::holds_alternative<cucumber::messages::test_step_started>(stepOrHookStarted)) |
| 27 | + { |
| 28 | + return { |
| 29 | + std::get<cucumber::messages::test_step_started>(stepOrHookStarted).test_case_started_id, |
| 30 | + std::get<cucumber::messages::test_step_started>(stepOrHookStarted).test_step_id, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + return {}; |
| 35 | + } |
| 36 | + |
| 37 | + std::pair<std::optional<std::string>, std::optional<std::string>> ReadTestRunHookStartedIds(StepOrHookStarted stepOrHookStarted) |
| 38 | + { |
| 39 | + if (std::holds_alternative<cucumber::messages::test_run_hook_started>(stepOrHookStarted)) |
| 40 | + { |
| 41 | + return { |
| 42 | + std::get<cucumber::messages::test_run_hook_started>(stepOrHookStarted).test_run_started_id, |
| 43 | + std::get<cucumber::messages::test_run_hook_started>(stepOrHookStarted).id, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + return {}; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + ExecutionContext::ExecutionContext(util::Broadcaster& broadCaster, Context& context, StepOrHookStarted stepOrHookStarted) |
| 52 | + : context{ context } |
| 53 | + , broadCaster{ broadCaster } |
| 54 | + , stepOrHookStarted{ std::move(stepOrHookStarted) } |
| 55 | + {} |
| 56 | + |
| 57 | + void ExecutionContext::Attach(std::string data, OptionsOrMediaType mediaType) |
| 58 | + { |
| 59 | + Attach(std::move(data), cucumber::messages::attachment_content_encoding::IDENTITY, std::move(mediaType)); |
| 60 | + } |
| 61 | + |
| 62 | + void ExecutionContext::Attach(std::istream& data, OptionsOrMediaType mediaType) |
| 63 | + { |
| 64 | + std::string buffer{ std::istreambuf_iterator<char>{ data }, std::istreambuf_iterator<char>{} }; |
| 65 | + |
| 66 | + buffer = base64::to_base64(buffer); |
| 67 | + |
| 68 | + Attach(std::move(buffer), cucumber::messages::attachment_content_encoding::BASE64, mediaType); |
| 69 | + } |
| 70 | + |
| 71 | + void ExecutionContext::Log(std::string text) |
| 72 | + { |
| 73 | + Attach(std::move(text), std::string{ LogMediaType }); |
| 74 | + } |
| 75 | + |
| 76 | + void ExecutionContext::Link(std::string url, std::optional<std::string> title) |
| 77 | + { |
| 78 | + Attach(std::move(url), AttachOptions{ |
| 79 | + .mediaType = LinkMediaType, |
| 80 | + .fileName = std::move(title), |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + void ExecutionContext::Attach(std::string data, cucumber::messages::attachment_content_encoding encoding, OptionsOrMediaType mediaType) |
| 85 | + { |
| 86 | + const auto options = std::holds_alternative<std::string>(mediaType) |
| 87 | + ? AttachOptions{ .mediaType = std::get<std::string>(mediaType) } |
| 88 | + : std::get<AttachOptions>(mediaType); |
| 89 | + |
| 90 | + auto [test_case_started_id, test_step_id] = ReadTestStepStartedIds(stepOrHookStarted); |
| 91 | + auto [test_run_started_id, test_run_hook_started_id] = ReadTestRunHookStartedIds(stepOrHookStarted); |
| 92 | + |
| 93 | + broadCaster.BroadcastEvent({ |
| 94 | + .attachment = cucumber::messages::attachment{ |
| 95 | + .body = std::move(data), |
| 96 | + .content_encoding = encoding, |
| 97 | + .file_name = std::move(options.fileName), |
| 98 | + .media_type = std::move(options.mediaType), |
| 99 | + .test_case_started_id = std::move(test_case_started_id), |
| 100 | + .test_step_id = std::move(test_step_id), |
| 101 | + .test_run_started_id = std::move(test_run_started_id), |
| 102 | + .test_run_hook_started_id = std::move(test_run_hook_started_id), |
| 103 | + .timestamp = support::TimestampNow(), |
| 104 | + }, |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments