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/csharp cucumber expression support #65

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 26 additions & 4 deletions src/language/csharpLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const csharpLanguage: Language = {
defineParameterTypeQueries: [],
defineStepDefinitionQueries: [
`
(method_declaration
(method_declaration
(attribute_list
(attribute
name: (identifier) @annotation-name
Expand All @@ -20,16 +20,38 @@ export const csharpLanguage: Language = {
)
(#match? @annotation-name "Given|When|Then|And|But|StepDefinition")
) @root
`,
`
(method_declaration
(attribute_list
(attribute
name: (identifier) @annotation-name
(attribute_argument_list
(attribute_argument
(string_literal) @expression
)
)
)
)
(#match? @annotation-name "Given|When|Then|And|But|StepDefinition")
) @root
`,
],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
convertParameterTypeExpression(s) {
throw new Error('Unsupported operation')
},
convertStepDefinitionExpression(s) {
const match = s.match(/^([@$'"]+)(.*)"$/)
if (!match) throw new Error(`Could not match ${s}`)
return new RegExp(match[2])
const regexParamMatch = s.match(/(\([^)]+[*+]\)|\.\*)/)
const regexExpressionMatch = s.match(/^@"(\^.*\$)"$/)

if (regexExpressionMatch || regexParamMatch) {
// For regex step definitions, the string always needs to be verbatim_string_literal (i.e. Prefixed with an '@')
return new RegExp(s.substring(2, s.length - 1))
}

const exprStart = s.startsWith('@') ? 2 : 1
return s.substring(exprStart, s.length - 1)
},

snippetParameters: {
Expand Down
2 changes: 1 addition & 1 deletion test/language/ExpressionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ParserAdapter, Source } from '../../src/language/types.js'
import { NodeParserAdapter } from '../../src/tree-sitter-node/NodeParserAdapter.js'
import { WasmParserAdapter } from '../../src/tree-sitter-wasm/WasmParserAdapter.js'

const parameterTypeSupport: Set<LanguageName> = new Set(['typescript', 'java', 'ruby'])
const parameterTypeSupport: Set<LanguageName> = new Set(['typescript', 'java', 'ruby', 'c_sharp'])

function defineContract(makeParserAdapter: () => ParserAdapter) {
let expressionBuilder: ExpressionBuilder
Expand Down
30 changes: 23 additions & 7 deletions test/language/testdata/c_sharp/StepDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

namespace StepDefinitions
{
[Binding]
public class BowlingSteps
{
[Given(@"^a regexp$")]
public void a_regexp() {
}
}
[Binding]
public class BowlingSteps
{
[Given(@"^a regexp$")]
public void ARegexp()
{
}

[When("a {uuid}")]
public void AUniqueID(string uuid)
{
}

[Then("a {date}")]
public void ADate(Date date)
{
}

[Then("an {undefined-parameter}")]
public void AnUndefinedParameter(Date date)
{
}
}
}