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

Python u-string patterns #173

Merged
merged 2 commits into from
Jan 17, 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 @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- (Rust) Line continuation characters in rust step definition patterns ([#179](https://github.com/cucumber/language-service/pull/179))
- (Python) Unexpected spaces and commas in generated step definitions [#160](https://github.com/cucumber/language-service/issues/160)

### Added
- (Python) Support for u-strings with step definition patterns ([#173](https://github.com/cucumber/language-service/pull/173))

## [1.4.1] - 2023-07-16
### Fixed
- (Python) There was a bug in how long concatenated strings were handled for multi-line regexes
Expand Down
11 changes: 8 additions & 3 deletions src/language/pythonLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ function cleanRegExp(regExpString: string): string {
}
}
export function toStringOrRegExp(step: string): StringOrRegExp {
return isRegExp(step.slice(1, -1))
? RegExp(cleanRegExp(step.slice(1, -1).split('?P').join('')))
: step.slice(1, -1)
// Remove explicit 'u' unicode prefix
const isUString = step.startsWith('u')
const stepText = isUString ? step.slice(1) : step

const strippedStepText = stepText.slice(1, -1)
return isRegExp(strippedStepText)
? RegExp(cleanRegExp(strippedStepText.split('?P').join('')))
: strippedStepText
}
export function concatStringLiteral(text: string): string {
const isFString = text.startsWith('f')
Expand Down
27 changes: 22 additions & 5 deletions test/language/pythonLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,35 @@ describe('pythonLanguage', () => {
it('should identify normal strings and just return a string', () => {
const nonregexes = ['"test"']
nonregexes.forEach(function (nonregex) {
assert(toStringOrRegExp(nonregex) == 'test')
assert.strictEqual(toStringOrRegExp(nonregex), 'test')
})
})
it('should properly handle concatanated string', () => {
const concatedStrings = [
it('should properly handle concatenated string', () => {
const concatenatedStrings = [
'"Airnet Address|Superheat|Gas Pipe Temperature|Liquid Pipe Temperature|EEV Opening|"\\\n "Return Air Temperature|Room Temperature|Setpoint|Filter|On-Off|Reset Filter Indicator|"\\\n "Thermostat - LockMode|Thermostat - Lock All|Thermostat - Lock Temperature|Thermostat - Lock On-Off|"\\\n "Fan Speed|Louver Position|Mode|Group Address|Malfunction Code|Indoor Unit Model Code|"\\\n "Operation-Stop|Thermostat ON|Capacity Increase|Malfunction Cause|Point_1|Point_2"',
]
const expectedStrings = [
'Airnet Address|Superheat|Gas Pipe Temperature|Liquid Pipe Temperature|EEV Opening|Return Air Temperature|Room Temperature|Setpoint|Filter|On-Off|Reset Filter Indicator|Thermostat - LockMode|Thermostat - Lock All|Thermostat - Lock Temperature|Thermostat - Lock On-Off|Fan Speed|Louver Position|Mode|Group Address|Malfunction Code|Indoor Unit Model Code|Operation-Stop|Thermostat ON|Capacity Increase|Malfunction Cause|Point_1|Point_2',
]
const z = concatedStrings.map((x, i) => [x, expectedStrings[i]]) //use map to zip the concat with expected for assertion
const z = concatenatedStrings.map((x, i) => [x, expectedStrings[i]]) //use map to zip the concat with expected for assertion
console.log(concatStringLiteral(expectedStrings[0]))
z.forEach((x) => assert(concatStringLiteral(x[0]) == x[1]))
z.forEach((x) => assert.strictEqual(concatStringLiteral(x[0]), x[1]))
})

it('should strip explicit unicode string prefix', () => {
const cases = [
{
input: 'u"Explicit unicode string"',
expected: 'Explicit unicode string',
},
{
input: 'u"^Explicit regex unicode string$"',
expected: '^Explicit regex unicode string$',
},
]

cases.forEach(({ input, expected }) => {
assert.strictEqual(toStringOrRegExp(input), expected)
})
})
})