Skip to content

Escape apostrophes in generated snippets#1888

Open
StantonMatt wants to merge 5 commits into
cucumber:mainfrom
StantonMatt:fix-nested-quote-snippets
Open

Escape apostrophes in generated snippets#1888
StantonMatt wants to merge 5 commits into
cucumber:mainfrom
StantonMatt:fix-nested-quote-snippets

Conversation

@StantonMatt

@StantonMatt StantonMatt commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Keep single-quoted generated Cucumber Expression snippets when the expression can be represented safely.
  • Use double-quoted Ruby snippets when the generated expression contains an apostrophe, with escaping for double quotes, backslashes, and interpolation markers.
  • Preserve backslash escaping for single-quoted snippets and cover apostrophe, interpolation, and trailing-backslash cases.

Fixes #1710

Verification

  • bundle exec rspec spec/cucumber/glue/snippet_spec.rb
  • bundle exec rspec
  • bundle exec cucumber
  • bundle exec rubocop
  • git diff --check

Local repros now print Then("Lucy hears Sean's message") and Then('Lucy hears trailing \\'), and both generated snippets compile with Ruby.

Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>

@luke-hill luke-hill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is what we previously had and we refactored it out many many moons ago.

The desired output here is to instead use double quoted snippets. Which is the previous behaviour. So we should use single quotes (If we can), then failing that, use double quotes. This avoids any issues.

Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
@StantonMatt

Copy link
Copy Markdown
Contributor Author

Updated this to keep single-quoted snippets when the generated expression has no apostrophe, and switch to double-quoted snippets when it does. I reran the full RSpec suite and RuboCop on the touched files after pushing this.

Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
Comment thread spec/cucumber/glue/snippet_spec.rb Outdated
expect(snippet.to_s).to eq(cucumber_output)
end

it 'uses double quotes for generated cucumber expression snippets with apostrophes' do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't cucumber expression snippets. They're all regular snippets

@StantonMatt StantonMatt Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right; I renamed those examples in 06591c7 so they refer to generated snippets rather than cucumber expression snippets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scenario having nested quotes produces syntactically-invalid step definition

2 participants