Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: java concatenated strings in definitions and generic decorators #202

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 40 additions & 18 deletions src/language/javaLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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")
) @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' },
Expand Down Expand Up @@ -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 []
}
30 changes: 18 additions & 12 deletions test/language/testdata/java/StepDefinitions.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
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}" )
void a_uuid(String uuid) {
@Given("a {uuid}")
void a_uuid(String uuid) {
}

@Given("a {date}" )
void a_date(Date date) {
@When("a {date}")
void a_date(Date date) {
}

@Given("a {planet}" )
void a_date(Date date) {
@Then("a {planet}")
void a_date(Date date) {
}

@Given("^a regexp$" )
void a_regexp() {
@And("^a regexp$")
void a_regexp() {
}

@Given("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) {
}
}
1 change: 1 addition & 0 deletions test/language/testdata/python/StepDefinitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Port of givens for testdata."""

from behave import step, given, when, then


Expand Down