Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/FeatureShowcase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

2 changes: 1 addition & 1 deletion examples/FeatureShowcase/README.txt
Original file line number Diff line number Diff line change
@@ -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.

32 changes: 32 additions & 0 deletions examples/FeatureShowcase/features/docstring.feature
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <gtest/gtest.h>
#include <cucumber-cpp/autodetect.hpp>

#include <string>

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<Quote> context;

context->text = text;
}

GIVEN("^(.+) said:$") {
REGEX_PARAM(std::string, author);
REGEX_PARAM(std::string, text);
ScenarioScope<Quote> context;

context->author = author;
context->text = text;
}

THEN("^the quote should keep its line breaks and indentation$") {
ScenarioScope<Quote> 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<Quote> 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<Quote> context;

ASSERT_EQ(author, context->author);
ASSERT_EQ("Get busy living,\nor get busy dying.", context->text);
}
74 changes: 74 additions & 0 deletions tests/integration/WireProtocolTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <cucumber-cpp/internal/connectors/wire/WireProtocol.hpp>
#include <cucumber-cpp/internal/connectors/wire/WireProtocolCommands.hpp>
#include <cucumber-cpp/internal/CukeEngineImpl.hpp>
#include <cucumber-cpp/internal/drivers/GenericDriver.hpp>
#include <cucumber-cpp/internal/step/StepMacros.hpp>
#include "utils/StepManagerTestDouble.hpp"

#include <gmock/gmock.h>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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> stepInfo(
std::make_shared<StepInvoker<StepWithArgAndDocString>>("^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"])");
}