Skip to content

Add ParameterInfo #124

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

Merged
merged 5 commits into from
May 24, 2022
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 @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- [.NET] Fix casing in "word" parameter type constant

### Added
- [JavaScript] Add `ParameterInfo` ([#124](https://github.com/cucumber/cucumber-expressions/pull/124))

## [15.1.1] - 2022-04-21
### Fixed
- [JavaScript] Make `CucumberExpression.ast` public (it was accidentally private in 15.1.0)
Expand Down
38 changes: 19 additions & 19 deletions docs/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions docs/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ select {
background-size: 1.5em 1.5em;
padding-right: 2.5rem;
-webkit-print-color-adjust: exact;
color-adjust: exact;
color-adjust: exact;
print-color-adjust: exact;
}

[multiple] {
Expand All @@ -455,7 +456,8 @@ select {
background-size: initial;
padding-right: 0.75rem;
-webkit-print-color-adjust: unset;
color-adjust: unset;
color-adjust: unset;
print-color-adjust: unset;
}

[type='checkbox'],[type='radio'] {
Expand All @@ -464,7 +466,8 @@ select {
appearance: none;
padding: 0;
-webkit-print-color-adjust: exact;
color-adjust: exact;
color-adjust: exact;
print-color-adjust: exact;
display: inline-block;
vertical-align: middle;
background-origin: border-box;
Expand Down Expand Up @@ -548,6 +551,7 @@ select {
}

[type='file']:focus {
outline: 1px solid ButtonText;
outline: 1px auto -webkit-focus-ring-color;
}

Expand Down
9 changes: 3 additions & 6 deletions javascript/src/CucumberExpressionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,9 @@ export default class CucumberExpressionGenerator {
parameterType: ParameterType<unknown>,
text: string
): ParameterTypeMatcher[] {
// TODO: [].map
const result = []
for (const regexp of parameterType.regexpStrings) {
result.push(new ParameterTypeMatcher(parameterType, regexp, text))
}
return result
return parameterType.regexpStrings.map(
(regexp) => new ParameterTypeMatcher(parameterType, regexp, text)
)
}
}

Expand Down
41 changes: 34 additions & 7 deletions javascript/src/GeneratedExpression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ParameterType from './ParameterType.js'
import { ParameterInfo } from './types.js'

export default class GeneratedExpression {
constructor(
Expand All @@ -16,17 +17,43 @@ export default class GeneratedExpression {
* @returns {ReadonlyArray.<String>}
*/
get parameterNames(): readonly string[] {
return this.parameterInfos.map((i) => `${i.name}${i.count === 1 ? '' : i.count.toString()}`)
}

/**
* Returns an array of ParameterInfo to use in generated function/method signatures
*/
get parameterInfos(): readonly ParameterInfo[] {
const usageByTypeName: { [key: string]: number } = {}
return this.parameterTypes.map((t) => getParameterName(t.name || '', usageByTypeName))
return this.parameterTypes.map((t) => getParameterInfo(t, usageByTypeName))
}
}

function getParameterName(typeName: string, usageByTypeName: { [key: string]: number }) {
let count = usageByTypeName[typeName]
count = count ? count + 1 : 1
usageByTypeName[typeName] = count

return count === 1 ? typeName : `${typeName}${count}`
function getParameterInfo(
parameterType: ParameterType<unknown>,
usageByName: { [key: string]: number }
): ParameterInfo {
const name = parameterType.name || ''
let counter = usageByName[name]
counter = counter ? counter + 1 : 1
usageByName[name] = counter
let type: string | null
if (parameterType.type) {
if (typeof parameterType.type === 'string') {
type = parameterType.type
} else if ('name' in parameterType.type) {
type = parameterType.type.name
} else {
type = null
}
} else {
type = null
}
return {
type,
name,
count: counter,
}
}

function format(pattern: string, ...args: readonly string[]): string {
Expand Down
13 changes: 10 additions & 3 deletions javascript/src/ParameterType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import CucumberExpressionError from './CucumberExpressionError.js'
const ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/
const UNESCAPE_PATTERN = () => /(\\([[$.|?*+\]]))/g

interface Constructor<T> extends Function {
new (...args: unknown[]): T
prototype: T
}

type Factory<T> = (...args: unknown[]) => T

export default class ParameterType<T> {
private transformFn: (...match: readonly string[]) => T
private transformFn: (...match: readonly string[]) => T | PromiseLike<T>

public static compare(pt1: ParameterType<unknown>, pt2: ParameterType<unknown>) {
if (pt1.preferForRegexpMatch && !pt2.preferForRegexpMatch) {
Expand Down Expand Up @@ -42,8 +49,8 @@ export default class ParameterType<T> {
constructor(
public readonly name: string | undefined,
regexps: readonly RegExp[] | readonly string[] | RegExp | string,
private readonly type: unknown,
transform: (...match: string[]) => T,
public readonly type: Constructor<T> | Factory<T> | string | null,
transform: (...match: string[]) => T | PromiseLike<T>,
public readonly useForSnippets: boolean,
public readonly preferForRegexpMatch: boolean
) {
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/defineDefaultParameterTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function defineDefaultParameterTypes(registry: DefinesParameterTy
new ParameterType(
'bigdecimal',
FLOAT_REGEXP,
Number,
String,
(s) => (s === undefined ? null : s),
false,
false
Expand Down Expand Up @@ -104,7 +104,7 @@ export default function defineDefaultParameterTypes(registry: DefinesParameterTy
new ParameterType(
'biginteger',
INTEGER_REGEXPS,
Number,
BigInt,
(s) => (s === undefined ? null : BigInt(s)),
false,
false
Expand Down
15 changes: 15 additions & 0 deletions javascript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ export interface Expression {
readonly source: string
match(text: string): readonly Argument[] | null
}

export type ParameterInfo = {
/**
* The string representation of the original ParameterType#type property
*/
type: string | null
/**
* The parameter type name
*/
name: string
/**
* The number of times this name has been used so far
*/
count: number
}
118 changes: 106 additions & 12 deletions javascript/test/CucumberExpressionGeneratorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CucumberExpression from '../src/CucumberExpression.js'
import CucumberExpressionGenerator from '../src/CucumberExpressionGenerator.js'
import ParameterType from '../src/ParameterType.js'
import ParameterTypeRegistry from '../src/ParameterTypeRegistry.js'
import { ParameterInfo } from '../src/types.js'

class Currency {
constructor(public readonly s: string) {}
Expand All @@ -15,11 +16,11 @@ describe('CucumberExpressionGenerator', () => {

function assertExpression(
expectedExpression: string,
expectedArgumentNames: string[],
expectedParameterInfo: ParameterInfo[],
text: string
) {
const generatedExpression = generator.generateExpressions(text)[0]
assert.deepStrictEqual(generatedExpression.parameterNames, expectedArgumentNames)
assert.deepStrictEqual(generatedExpression.parameterInfos, expectedParameterInfo)
assert.strictEqual(generatedExpression.source, expectedExpression)

const cucumberExpression = new CucumberExpression(
Expand All @@ -32,7 +33,7 @@ describe('CucumberExpressionGenerator', () => {
`Expected text '${text}' to match generated expression '${generatedExpression.source}'`
)
}
assert.strictEqual(match.length, expectedArgumentNames.length)
assert.strictEqual(match.length, expectedParameterInfo.length)
}

beforeEach(() => {
Expand Down Expand Up @@ -63,37 +64,110 @@ describe('CucumberExpressionGenerator', () => {
})

it('generates expression with escaped slashes', () => {
assertExpression('The {int}\\/{int}\\/{int} hey', ['int', 'int2', 'int3'], 'The 1814/05/17 hey')
assertExpression(
'The {int}\\/{int}\\/{int} hey',
[
{
type: 'Number',
name: 'int',
count: 1,
},
{
type: 'Number',
name: 'int',
count: 2,
},
{
type: 'Number',
name: 'int',
count: 3,
},
],
'The 1814/05/17 hey'
)
})

it('generates expression for int float arg', () => {
assertExpression(
'I have {int} cukes and {float} euro',
['int', 'float'],
[
{
type: 'Number',
name: 'int',
count: 1,
},
{
type: 'Number',
name: 'float',
count: 1,
},
],
'I have 2 cukes and 1.5 euro'
)
})

it('generates expression for strings', () => {
assertExpression(
'I like {string} and {string}',
['string', 'string2'],
[
{
type: 'String',
name: 'string',
count: 1,
},
{
type: 'String',
name: 'string',
count: 2,
},
],
'I like "bangers" and \'mash\''
)
})

it('generates expression with % sign', () => {
assertExpression('I am {int}%% foobar', ['int'], 'I am 20%% foobar')
assertExpression(
'I am {int}%% foobar',
[
{
type: 'Number',
name: 'int',
count: 1,
},
],
'I am 20%% foobar'
)
})

it('generates expression for just int', () => {
assertExpression('{int}', ['int'], '99999')
assertExpression(
'{int}',
[
{
type: 'Number',
name: 'int',
count: 1,
},
],
'99999'
)
})

it('numbers only second argument when builtin type is not reserved keyword', () => {
assertExpression(
'I have {float} cukes and {float} euro',
['float', 'float2'],
[
{
type: 'Number',
name: 'float',
count: 1,
},
{
type: 'Number',
name: 'float',
count: 2,
},
],
'I have 2.5 cukes and 1.5 euro'
)
})
Expand All @@ -103,18 +177,38 @@ describe('CucumberExpressionGenerator', () => {
new ParameterType('currency', /[A-Z]{3}/, Currency, (s) => new Currency(s), true, false)
)

assertExpression('I have a {currency} account', ['currency'], 'I have a EUR account')
assertExpression(
'I have a {currency} account',
[
{
type: 'Currency',
name: 'currency',
count: 1,
},
],
'I have a EUR account'
)
})

it('prefers leftmost match when there is overlap', () => {
parameterTypeRegistry.defineParameterType(
new ParameterType<Currency>('currency', /c d/, Currency, (s) => new Currency(s), true, false)
new ParameterType('currency', /c d/, Currency, (s) => new Currency(s), true, false)
)
parameterTypeRegistry.defineParameterType(
new ParameterType('date', /b c/, Date, (s) => new Date(s), true, false)
)

assertExpression('a {date} d e f g', ['date'], 'a b c d e f g')
assertExpression(
'a {date} d e f g',
[
{
type: 'Date',
name: 'date',
count: 1,
},
],
'a b c d e f g'
)
})

// TODO: prefers widest match
Expand Down
Loading