From 5816c68122b7c6ddd312836d2869983d0428e480 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Fri, 19 Apr 2024 22:53:37 +0100 Subject: [PATCH 1/4] Java generic decorators --- src/language/javaLanguage.ts | 2 +- test/language/testdata/java/StepDefinitions.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/language/javaLanguage.ts b/src/language/javaLanguage.ts index 63025817..f074e66d 100644 --- a/src/language/javaLanguage.ts +++ b/src/language/javaLanguage.ts @@ -90,7 +90,7 @@ export const javaLanguage: Language = { ) ) ) - (#match? @annotation-name "Given|When|Then") + (#match? @annotation-name "Given|When|Then|And|But") ) @root `, ], diff --git a/test/language/testdata/java/StepDefinitions.java b/test/language/testdata/java/StepDefinitions.java index 4a183e23..f0f18240 100644 --- a/test/language/testdata/java/StepDefinitions.java +++ b/test/language/testdata/java/StepDefinitions.java @@ -5,19 +5,19 @@ public class StepDefinitions { void a_uuid(String uuid) { } - @Given("a {date}" ) + @When("a {date}" ) void a_date(Date date) { } - @Given("a {planet}" ) + @Then("a {planet}" ) void a_date(Date date) { } - @Given("^a regexp$" ) + @And("^a regexp$" ) void a_regexp() { } - @Given("an {undefined-parameter}" ) + @But("an {undefined-parameter}" ) void an_undefined_parameter(Date date) { } From aa83e9fa8ccc6ac1b961c6a6604c507c738fbbd6 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Sat, 20 Apr 2024 20:23:25 +0100 Subject: [PATCH 2/4] Concatenated strings in Java step definitions --- src/language/javaLanguage.ts | 58 +++++++++++++------ .../testdata/java/StepDefinitions.java | 26 +++++---- .../testdata/python/StepDefinitions.py | 1 + 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/language/javaLanguage.ts b/src/language/javaLanguage.ts index f074e66d..dc76aab3 100644 --- a/src/language/javaLanguage.ts +++ b/src/language/javaLanguage.ts @@ -18,9 +18,12 @@ export const javaLanguage: Language = { return stringLiteral(node) }, toStepDefinitionExpression(node) { - const text = stringLiteral(node) - const hasRegExpAnchors = text[0] == '^' || text[text.length - 1] == '$' - return hasRegExpAnchors ? new RegExp(text) : text + if (node.type === 'string_literal') { + const text = stringLiteral(node) + const hasRegExpAnchors = text[0] == '^' || text[text.length - 1] == '$' + return hasRegExpAnchors ? new RegExp(text) : text + } + return collectStringFragments(node).join('') }, defineParameterTypeQueries: [ @@ -78,23 +81,33 @@ export const javaLanguage: Language = { `, ], defineStepDefinitionQueries: [ - ` -(method_declaration - (modifiers - (annotation - name: (identifier) @annotation-name - arguments: (annotation_argument_list - [ - (string_literal) @expression - ] + `(method_declaration + (modifiers + (annotation + name: (identifier) @annotation-name + arguments: (annotation_argument_list + [ + (string_literal) @expression + ] + ) + ) ) - ) - ) - (#match? @annotation-name "Given|When|Then|And|But") -) @root -`, + (#match? @annotation-name "Given|When|Then|And|But") + ) @root`, + `(method_declaration + (modifiers + (annotation + name: (identifier) @annotation-name + arguments: (annotation_argument_list + [ + (binary_expression) @expression + ] + ) + ) + ) + (#match? @annotation-name "Given|When|Then|And|But") + ) @root`, ], - snippetParameters: { int: { type: 'int', name: 'i' }, float: { type: 'float', name: 'f' }, @@ -126,3 +139,12 @@ export function stringLiteral(node: TreeSitterSyntaxNode | null): string { function unescapeString(s: string): string { return s.replace(/\\\\/g, '\\') } +function collectStringFragments(node: TreeSitterSyntaxNode): string[] { + if (node.type === 'string_fragment') { + return [unescapeString(node.text.replace('(?i)', ''))] + } + if (node.type === 'binary_expression' || node.type === 'string_literal') { + return node.children.flatMap(collectStringFragments) + } + return [] +} diff --git a/test/language/testdata/java/StepDefinitions.java b/test/language/testdata/java/StepDefinitions.java index f0f18240..ced711c8 100644 --- a/test/language/testdata/java/StepDefinitions.java +++ b/test/language/testdata/java/StepDefinitions.java @@ -1,27 +1,29 @@ import io.cucumber.java.en.Given; public class StepDefinitions { - @Given("a {uuid}" ) - void a_uuid(String uuid) { + @Given("a {uuid}") + void a_uuid(String uuid) { } - @When("a {date}" ) - void a_date(Date date) { + @When("a {date}") + void a_date(Date date) { } - @Then("a {planet}" ) - void a_date(Date date) { + @Then("a {planet}") + void a_date(Date date) { } - @And("^a regexp$" ) - void a_regexp() { + @And("^a regexp$") + void a_regexp() { } - @But("an {undefined-parameter}" ) - void an_undefined_parameter(Date date) { + @But("an {undefined-parameter}") + void an_undefined_parameter(Date date) { } - @Given("the bee's knees" ) - void the_bees_knees(Date date) { + @Given( + "the " + "bee's " + + "knees") + void the_bees_knees(Date date) { } } diff --git a/test/language/testdata/python/StepDefinitions.py b/test/language/testdata/python/StepDefinitions.py index 75d66ea8..cc61457f 100644 --- a/test/language/testdata/python/StepDefinitions.py +++ b/test/language/testdata/python/StepDefinitions.py @@ -1,4 +1,5 @@ """Port of givens for testdata.""" + from behave import step, given, when, then From ae2dd6d42d3ac16ec39080956e5f1706bf751657 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Sat, 20 Apr 2024 20:45:24 +0100 Subject: [PATCH 3/4] fix: missing java test data imports --- test/language/testdata/java/StepDefinitions.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/language/testdata/java/StepDefinitions.java b/test/language/testdata/java/StepDefinitions.java index ced711c8..9270d01a 100644 --- a/test/language/testdata/java/StepDefinitions.java +++ b/test/language/testdata/java/StepDefinitions.java @@ -1,4 +1,8 @@ +import io.cucumber.java.en.And; +import io.cucumber.java.en.But; import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import io.cucumber.java.en.Then; public class StepDefinitions { @Given("a {uuid}") From 6fbdb6eac9a5601a3f1b57193f8fa5f04d3a74a6 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Sat, 20 Apr 2024 20:58:36 +0100 Subject: [PATCH 4/4] Changelog Java @And, @But and conacatenated strings --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cfa5313..a10b78f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed +- (Java) Detect step definition patterns with concatenated strings ([#202](https://github.com/cucumber/language-service/pull/202)) +- (Java) Support `@And` and `@But` step definition annotations ([#202](https://github.com/cucumber/language-service/pull/202)) ## [1.5.0] - 2024-04-08 ### Added