diff --git a/examples/FeatureShowcase/CMakeLists.txt b/examples/FeatureShowcase/CMakeLists.txt index 9706df68..96e5feff 100644 --- a/examples/FeatureShowcase/CMakeLists.txt +++ b/examples/FeatureShowcase/CMakeLists.txt @@ -13,6 +13,7 @@ if(TARGET GTest::gtest) add_cucumber_executable( features/step_definitions/TagSteps.cpp features/step_definitions/TableSteps.cpp + features/step_definitions/DocStringSteps.cpp ) endif() diff --git a/examples/FeatureShowcase/README.txt b/examples/FeatureShowcase/README.txt index 044d7175..190b49e3 100644 --- a/examples/FeatureShowcase/README.txt +++ b/examples/FeatureShowcase/README.txt @@ -1,2 +1,2 @@ -This sample shows a few advanced features: tags and table arguments. +This sample shows a few advanced features: tags, table and doc string arguments. diff --git a/examples/FeatureShowcase/features/docstring.feature b/examples/FeatureShowcase/features/docstring.feature new file mode 100644 index 00000000..491f9da4 --- /dev/null +++ b/examples/FeatureShowcase/features/docstring.feature @@ -0,0 +1,32 @@ +# language: en +Feature: Doc string + In order to explain how to use doc strings + I have to make this silly example + + Scenario: Doc string + Given the following quote: + """ + I'm gonna make him an offer + he can't refuse. + + "The Godfather", 1972 + """ + Then the quote should keep its line breaks and indentation + + Scenario: Doc string with content type + Given the following quote: + """json + { + "actor": "Marlon Brando", + "quote": "I'm gonna make him an offer he can't refuse." + } + """ + Then the quote should be the plain JSON text + + Scenario: Doc string after a regex parameter + Given Morgan Freeman said: + """ + Get busy living, + or get busy dying. + """ + Then Morgan Freeman should be quoted on two lines diff --git a/examples/FeatureShowcase/features/step_definitions/DocStringSteps.cpp b/examples/FeatureShowcase/features/step_definitions/DocStringSteps.cpp new file mode 100644 index 00000000..b9afe679 --- /dev/null +++ b/examples/FeatureShowcase/features/step_definitions/DocStringSteps.cpp @@ -0,0 +1,67 @@ +#include +#include + +#include + +using cucumber::ScenarioScope; + +struct Quote { + std::string author; + std::string text; +}; + +/* + * The doc string is passed as the last argument, after the regex parameters. + */ + +GIVEN("^the following quote:$") { + REGEX_PARAM(std::string, text); + ScenarioScope context; + + context->text = text; +} + +GIVEN("^(.+) said:$") { + REGEX_PARAM(std::string, author); + REGEX_PARAM(std::string, text); + ScenarioScope context; + + context->author = author; + context->text = text; +} + +THEN("^the quote should keep its line breaks and indentation$") { + ScenarioScope context; + + ASSERT_EQ( + "I'm gonna make him an offer\n" + " he can't refuse.\n" + "\n" + "\"The Godfather\", 1972", + context->text + ); +} + +/* + * The content type of a doc string is not sent over the wire, only its content. + */ + +THEN("^the quote should be the plain JSON text$") { + ScenarioScope context; + + ASSERT_EQ( + "{\n" + " \"actor\": \"Marlon Brando\",\n" + " \"quote\": \"I'm gonna make him an offer he can't refuse.\"\n" + "}", + context->text + ); +} + +THEN("^(.+) should be quoted on two lines$") { + REGEX_PARAM(std::string, author); + ScenarioScope context; + + ASSERT_EQ(author, context->author); + ASSERT_EQ("Get busy living,\nor get busy dying.", context->text); +} diff --git a/tests/integration/WireProtocolTest.cpp b/tests/integration/WireProtocolTest.cpp index d11fb041..21ea578a 100644 --- a/tests/integration/WireProtocolTest.cpp +++ b/tests/integration/WireProtocolTest.cpp @@ -1,5 +1,9 @@ #include #include +#include +#include +#include +#include "utils/StepManagerTestDouble.hpp" #include @@ -182,6 +186,25 @@ TEST_F(WireMessageCodecTest, handlesInvokeMessageWithNullArg) { .run(engine); } +TEST_F(WireMessageCodecTest, handlesInvokeMessageWithMultilineDocStringArg) { + MockCukeEngine engine; + EXPECT_CALL( + engine, + invokeStep( + "42", ElementsAre("p1", "first line\n \"indented\" line\n\nlast line"), ElementsAre() + ) + ) + .Times(1); + + decode(R"json([ + "invoke", { + "id": "42", + "args": ["p1", "first line\n \"indented\" line\n\nlast line"] + } + ])json") + .run(engine); +} + TEST_F(WireMessageCodecTest, handlesEndScenarioMessageWithoutArgument) { MockCukeEngine engine; EXPECT_CALL(engine, endScenario(ElementsAre())).Times(1); @@ -391,3 +414,54 @@ TEST(WireCommandsTest, throwingAnythingInvokeReturnsFailure) { EXPECT_PTRTYPE(FailureResponse, response.get()); // TODO Test empty } + +/* + * Request handling with step definitions + */ + +class StepWithArgAndDocString : public GenericStep { +public: + void body() override { + REGEX_PARAM(string, arg); + REGEX_PARAM(string, docString); + EXPECT_EQ("p1", arg); + EXPECT_EQ("first line\n indented line", docString); + } +}; + +class WireProtocolHandlerTest : public Test { +protected: + const JsonWireMessageCodec codec; + CukeEngineImpl engine; + const WireProtocolHandler handler; + + WireProtocolHandlerTest() : + handler(codec, engine) { + } + + void SetUp() override { + std::shared_ptr stepInfo( + std::make_shared>("^a step with (.+):$", "") + ); + stepInfo->id = 42; + StepManagerTestDouble::addStep(stepInfo); + } + + void TearDown() override { + StepManagerTestDouble::clearSteps(); + } +}; + +TEST_F(WireProtocolHandlerTest, passesDocStringArgToStepDefinition) { + EXPECT_EQ(handler.handle(R"(["begin_scenario"])"), R"(["success"])"); + EXPECT_EQ( + handler.handle(R"json([ + "invoke", { + "id": "42", + "args": ["p1", "first line\n indented line"] + } + ])json"), + R"(["success"])" + ); + EXPECT_EQ(handler.handle(R"(["end_scenario"])"), R"(["success"])"); +}