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

Added behat arg detection, added related test #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 27 additions & 1 deletion src/language/phpLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const phpLanguage: Language = {
const text = node.text
const match = text.match(/^(\/\*\*[\s*]*)([\s\S]*)(\n[\s]*\*\/)/)
if (!match) throw new Error(`Could not match ${text}`)
return new RegExp(match[2].replace(/@(Given |When |Then )/, '').trim())
return behatifyStep(match[2])
MolbioUnige marked this conversation as resolved.
Show resolved Hide resolved
},
// Empty array because Behat does not support Cucumber Expressions
defineParameterTypeQueries: [],
Expand Down Expand Up @@ -44,3 +44,29 @@ export const phpLanguage: Language = {
}
MolbioUnige marked this conversation as resolved.
Show resolved Hide resolved
`,
}

export function behatifyStep(step: string): RegExp {
const stepText = stripIdentifier(step)
if (stepText.startsWith('/')) {
return cleanRegExp(stepText)
} else if (/:[A-Za-z_][\w_]+/.test(stepText)) {
return argToRegex(stepText)
}

return RegExp(stepText)
}

function stripIdentifier(text: string): string {
return text.replace(/@(Given |When |Then )/, '').trim()
}

function cleanRegExp(re: string): RegExp {
const [body, modifier] = re.slice(1).split('/')

return RegExp(body, modifier)
}

function argToRegex(text: string): RegExp {
// arg must be a valid php variable name, see https://www.php.net/manual/en/language.variables.basics.php
return RegExp(text.replace(/:[A-Za-z_][\w_]+/g, '([\\S]+|"[^"]+")'))
}
35 changes: 35 additions & 0 deletions test/language/phpLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert from 'assert'

import { behatifyStep } from '../../src/language/phpLanguage.js'

describe('phpLanguage', () => {
it('should handle behat step definition', () => {
const cases = [
{
input: '@Given Hello world',
output: RegExp('Hello world'),
},
{
input: '@When Hello :world',
output: RegExp('Hello ([\\S]+|"[^"]+")'),
},
{
input: '@Then there is a :arg1, which costs £:arg2',
output: RegExp('there is a ([\\S]+|"[^"]+"), which costs £([\\S]+|"[^"]+")'),
},
{
input: '@Given /^there (?:is|are) (\\d+) monsters?$/',
output: RegExp('^there (?:is|are) (\\d+) monsters?$'),
},
{
input: '@Given /^Something (.*)$/i',
output: RegExp('^Something (.*)$', 'i'),
},
]
cases.forEach(function (c) {
const result = behatifyStep(c.input)
assert(result instanceof RegExp)
assert.equal(result.toString(), c.output.toString())
})
})
})