cucumber icon indicating copy to clipboard operation
cucumber copied to clipboard

Unable to match pattern with line continuation and no further characters on second line

Open kieran-ryan opened this issue 1 year ago • 4 comments

What did you observe?

Integrating support for line continuations in step definition patterns into the official cucumber VSCode extension (cucumber/vscode#179). Was unsure how to handle a line continuation no further characters on the following line. No error thrown, and was unable to find a matching gherkin step.

With the below example, I would have assumed the gherkin text (which includes a trailing space) would have matched the pattern.

#[given("a line continuation \
")]
fn hungry_cat(world: &mut AnimalWorld) {
    world.cat.hungry = true;
}
Given a line continuation 

For a working example, having a character on the second line would match as follows:

#[given("a line continuation \
!")]
fn hungry_cat(world: &mut AnimalWorld) {
    world.cat.hungry = true;
}
Given a line continuation !

Where are you looking for help?

Is there a gherkin step text example that matches the above pattern?

Version information

  • rustc 1.75.0
  • cucumber 0.20.2

kieran-ryan avatar Jan 15 '24 22:01 kieran-ryan

@kieran-ryan thanks! I'll look into this.

tyranron avatar Jan 16 '24 11:01 tyranron

@kieran-ryan I've looked into this, and it seems to be a correct behavior of Gherkin parser.

I run the test on the official reference inplementation:

Feature: foo
  Scenario: bar
    Given a line continuation 
    Then failure
$ npm install --save-dev @cucumber/cucumber
$ node_modules/.bin/gherkin-javascript test.feature | jq
{
  "source": {
    "data": "Feature: foo\n  Scenario: bar\n    Given a line continuation \n    Then failure\n",
    "uri": "test.feature",
    "mediaType": "text/x.cucumber.gherkin+plain"
  }
}
{
  "gherkinDocument": {
    "feature": {
      "tags": [],
      "location": {
        "line": 1,
        "column": 1
      },
      "language": "en",
      "keyword": "Feature",
      "name": "foo",
      "description": "",
      "children": [
        {
          "scenario": {
            "id": "ff6d7cc4-677a-4f0a-9c2c-8fdff4d0aef0",
            "tags": [],
            "location": {
              "line": 2,
              "column": 3
            },
            "keyword": "Scenario",
            "name": "bar",
            "description": "",
            "steps": [
              {
                "id": "833488c0-2611-4d0c-b567-bcf1b51d7dff",
                "location": {
                  "line": 3,
                  "column": 5
                },
                "keyword": "Given ",
                "keywordType": "Context",
                "text": "a line continuation"
              },
              {
                "id": "e186eb36-780c-4735-bc6a-17642089c1f8",
                "location": {
                  "line": 4,
                  "column": 5
                },
                "keyword": "Then ",
                "keywordType": "Outcome",
                "text": "failure"
              }
            ],
            "examples": []
          }
        }
      ]
    },
    "comments": [],
    "uri": "test.feature"
  }
}
{
  "pickle": {
    "id": "5f22b53d-0764-4ba4-8760-19f97bddf6c0",
    "uri": "test.feature",
    "astNodeIds": [
      "ff6d7cc4-677a-4f0a-9c2c-8fdff4d0aef0"
    ],
    "tags": [],
    "name": "bar",
    "language": "en",
    "steps": [
      {
        "id": "039d6e5f-0029-401f-86d1-a967d0aa2066",
        "text": "a line continuation",
        "type": "Context",
        "astNodeIds": [
          "833488c0-2611-4d0c-b567-bcf1b51d7dff"
        ]
      },
      {
        "id": "d4e8534d-47fc-4f30-bcfd-ea9f5abe0179",
        "text": "failure",
        "type": "Outcome",
        "astNodeIds": [
          "e186eb36-780c-4735-bc6a-17642089c1f8"
        ]
      }
    ]
  }
}

We can clearly see the trailing whitespace in the source.data, however, the parsed text of the step doesn't contain it.

I doubt we should deviate from the official reference implementation in this detail, since Gherkin doesn't expose any concrete formal grammar. And thus, unfortunately for you, you should consider in your tests that trailing spaces are omitted.

tyranron avatar Jan 16 '24 14:01 tyranron

Super, thank you for the immediate follow-up! That validates my assumption: no gherkin step will match a step definition pattern with trailing whitespace.

Thus, I would suggest a change request for cucumber-rs to provide a specific error message if a pattern contains trailing whitespace, that is invalid; making the failure explicit, rather than a soft failure where the step is considered unmatched and the user must figure out that trailing whitespace cannot be included in a step definition pattern. Only a suggestion though.

Thanks for the awesome support! 🙌

kieran-ryan avatar Jan 16 '24 19:01 kieran-ryan

@kieran-ryan

Thus, I would suggest a change request for cucumber-rs to provide a specific error message if a pattern contains trailing whitespace, that is invalid; making the failure explicit, rather than a soft failure where the step is considered unmatched and the user must figure out that trailing whitespace cannot be included in a step definition pattern. Only a suggestion though.

Makes sense. Would love to see a PR.

tyranron avatar Jan 16 '24 19:01 tyranron