Tyler-Keith-Thompson / CucumberSwift

A lightweight swift Cucumber implementation
https://tyler-keith-thompson.github.io/CucumberSwift/documentation/cucumberswift/
MIT License
74 stars 19 forks source link

"Given something #comment" vs "Given the color is #123456" #67

Closed DevMobileAS closed 1 year ago

DevMobileAS commented 1 year ago

In our project I tried defining a color for a test in hex representation.

To Reproduce The .feature file contained something like this: "Given the color is #123456"

Actual auto generated stub Then("^Given the color is$")

What I'd expect to be a correct implementation Then(CucumberExpression("Given the color is {color}") (using a custom Parameter implementation)

Additional context

I found that CucumberSwift treats the color as just a comment. There seems to be no way to escape the hashtag character. So the only way I found to work around this issue was to put the color into quotes.

After doing some quick research I found this interesting comment: https://stackoverflow.com/questions/26676543/whats-the-official-way-to-escape-a-comment-in-gherkin-cucumber

Essentially it is quoting some gherkin syntax description: "Comment lines are allowed anywhere in the file. They begin with zero or more spaces, followed by a hash sign (#) and some amount of text."

I'd try to find the issue in the code and create a PR, but at first I'd like to hear your thoughts about if that's even an issue or implemented as intended?

Kind regards Björn

Tyler-Keith-Thompson commented 1 year ago

What an interesting use-case.

You say it works in quotes? That's probably a good sign.

So I'd like to be able to support contextual comments such as:

Given I am logged in #switch to OAuth login.

There may even be an official cucumber test for that, I'd have to look through the AST files in the test suite to be sure. (Although the SO post seems to indicate otherwise).

What's more intriguing to me is perhaps allowing comments to be escaped. Gherkin treats escape sequences a little bit weird because \ is a literal character unless it's followed by one of the documented escape sequences. A change we might be able to make would be to always treat \symbol as an escape sequence and throw an error if the sequence is unknown. That'd fit better with how other languages (and Swift, in particular) work.

How would you feel about supporting \ # over only interpreting # as a comment if it's at the beginning of a line?

FWIW all this work would be done by the lexer, we simply don't tokenize comments except language comments.

Hsilgos commented 1 year ago

You say it works in quotes?

This is the consequence of the same issue as I just reported. Everything between quotes is treated as single token in Lexer.swift, ignoring any symbols like '<', '>' and '#'

Tyler-Keith-Thompson commented 1 year ago

@Hsilgos is correct, it's more of a bug that it works in quotes. To fix this we'll remove finding strings from the lexer at all, that should also solve #71 . We'll also allow \# as a valid escape sequence to allow for the color literal you're attempting to add.

One problem with this approach is that it may differ from other Cucumber implementations causing issues for people who want the literal character # in a shared .feature file.

As a follow-up I'll add a configuration for "whole line comments only". I am rather certain that some of the folks who use this library use comments at the end of a line, so changing that would break them.

Tyler-Keith-Thompson commented 1 year ago

CucumberSwift v4.1.1 (running through the pipeline at time of writing) should give you a better solution. Comments are allowed to be escaped, so you can write:

Given the color is \#123456

I'll be following up by fixing the bug with strings, so your current workaround will not work in later versions than 4.1.1.