Commit fb410264 authored by Kamil Strzempowicz's avatar Kamil Strzempowicz
Browse files

My solution to issue #79

parent d855fcd9
......@@ -6,7 +6,7 @@
### Bugfixes
None yet
*Fixed suggested step definition when step sentence contains double quote ([#79](https://github.com/cucumber/cucumber-cpp/issues/79))
## [0.3.1](https://github.com/cucumber/cucumber-cpp/compare/v0.3...v0.3.1) (11 April 2016)
......
Feature: Escaping
In order to copy and paste the undefined steps snippets
As a developer
I want the regex string to be correctly escaped
Scenario Outline: <characters> in step definition
Given a scenario with:
"""
Given the step contains '<scenario step string>'
"""
And the steps have no mappings
When Cucumber executes the scenario
Then a step definition snippet with "^the step contains '<step definition string>'$" is suggested
Examples:
| characters | scenario step string | step definition string |
| Double quotes | " | \\" |
| Backslash | \ | \\\\\\ |
| Dot | . | \\. |
| Caret | ^ | \\^ |
| Dollar | $ | \\$ |
| Asterisk | * | \\* |
| Plus | + | \\\+ |
| Question mark | ? | \\\? |
| Brackets | () | \\(\\) |
| Square brackets | [] | \\[\\] |
| Curly brackets | {} | \\{\\} |
| Pipe | \| | \\\| |
......@@ -151,6 +151,26 @@ EOF
assert_success true
end
def unescape(string)
string.gsub('\n', "\n").gsub('\e', "\e")
end
def assert_include(expected, actual)
expect(unescape(actual)).to include(unescape(expected))
end
def assert_scenario_output(scenario_output)
assert_include("#{scenario_output}", all_output)
end
def assert_not_include(unexpected, actual)
expect(unescape(actual)).not_to include(unescape(unexpected))
end
def assert_scenario_output_not_include(scenario_output)
assert_not_include("#{scenario_output}", all_output)
end
def assert_scenario_reported_as_failing(scenario_name)
assert_partial_output("# Scenario: #{scenario_name}", all_output)
assert_success false
......
......@@ -6,3 +6,6 @@ Then /^the step output should contain:$/ do |output|
expect(@steps_out.gets).to eq(output)
end
Then /^a step definition snippet with "(.*)" is suggested$/ do |output|
assert_scenario_output output
end
......@@ -34,11 +34,16 @@ void CukeCommands::endScenario() {
}
const std::string CukeCommands::snippetText(const std::string stepKeyword, const std::string stepName) const {
std::stringstream snippetText; // TODO Escape stepName
snippetText << boost::to_upper_copy(stepKeyword) << "(\"^" << stepName << "$\") {" << std::endl;
snippetText << " pending();" << std::endl;
snippetText << "}" << std::endl;
return snippetText.str();
const boost::regex esc("[\".^$|()\\[\\]{}*+?\\\\]");
const std::string rep("\\\\&");
std::stringstream text;
text << boost::to_upper_copy(stepKeyword)
<< "(\"^"
<< regex_replace(stepName, esc, rep, boost::match_default | boost::format_sed)
<< "$\") {\n"
<< " pending();\n"
<< "}\n";
return text.str();
}
MatchResult CukeCommands::stepMatches(const std::string description) const {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment