Skip to content

Commit

Permalink
create CucumberExpression instances once (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshski authored and charlierudolph committed Dec 4, 2018
1 parent 5da5cc8 commit c4bd822
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 56 deletions.
23 changes: 23 additions & 0 deletions src/models/definition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default class Definition {
constructor({ code, line, options, uri }) {
this.code = code
this.line = line
this.options = options
this.uri = uri
}

buildInvalidCodeLengthMessage(syncOrPromiseLength, callbackLength) {
return (
`function has ${this.code.length} arguments` +
`, should have ${syncOrPromiseLength} (if synchronous or returning a promise)` +
` or ${callbackLength} (if accepting a callback)`
)
}

getInvalidCodeLengthMessage(parameters) {
return this.buildInvalidCodeLengthMessage(
parameters.length,
parameters.length + 1
)
}
}
44 changes: 9 additions & 35 deletions src/models/step_definition.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import { CucumberExpression, RegularExpression } from 'cucumber-expressions'
import DataTable from './data_table'
import { buildStepArgumentIterator } from '../step_arguments'
import Definition from './definition'

export default class StepDefinition {
constructor({ code, line, options, pattern, uri }) {
this.code = code
this.line = line
this.options = options
export default class StepDefinition extends Definition {
constructor({ code, line, options, uri, pattern, expression }) {
super({ code, line, options, uri })
this.pattern = pattern
this.uri = uri
this.expression = expression
}

buildInvalidCodeLengthMessage(syncOrPromiseLength, callbackLength) {
return (
`function has ${this.code.length} arguments` +
`, should have ${syncOrPromiseLength} (if synchronous or returning a promise)` +
` or ${callbackLength} (if accepting a callback)`
)
}

getInvalidCodeLengthMessage(parameters) {
return this.buildInvalidCodeLengthMessage(
parameters.length,
parameters.length + 1
)
}

getInvocationParameters({ step, parameterTypeRegistry, world }) {
const cucumberExpression = this.getCucumberExpression(parameterTypeRegistry)
const stepNameParameters = cucumberExpression
getInvocationParameters({ step, world }) {
const stepNameParameters = this.expression
.match(step.text)
.map(arg => arg.getValue(world))
const iterator = buildStepArgumentIterator({
Expand All @@ -39,19 +21,11 @@ export default class StepDefinition {
return stepNameParameters.concat(stepArgumentParameters)
}

getCucumberExpression(parameterTypeRegistry) {
if (typeof this.pattern === 'string') {
return new CucumberExpression(this.pattern, parameterTypeRegistry)
}
return new RegularExpression(this.pattern, parameterTypeRegistry)
}

getValidCodeLengths(parameters) {
return [parameters.length, parameters.length + 1]
}

matchesStepName({ stepName, parameterTypeRegistry }) {
const cucumberExpression = this.getCucumberExpression(parameterTypeRegistry)
return Boolean(cucumberExpression.match(stepName))
matchesStepName(stepName) {
return Boolean(this.expression.match(stepName))
}
}
8 changes: 4 additions & 4 deletions src/models/test_case_hook_definition.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import PickleFilter from '../pickle_filter'
import StepDefinition from './step_definition'
import Definition from './definition'

export default class TestCaseHookDefinition extends StepDefinition {
constructor(data) {
super(data)
export default class TestCaseHookDefinition extends Definition {
constructor(...data) {
super(...data)
this.pickleFilter = new PickleFilter({
tagExpression: this.options.tags,
})
Expand Down
4 changes: 2 additions & 2 deletions src/models/test_run_hook_definition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StepDefinition from './step_definition'
import Definition from './definition'

export default class TestRunHookDefinition extends StepDefinition {
export default class TestRunHookDefinition extends Definition {
getInvalidCodeLengthMessage() {
return this.buildInvalidCodeLengthMessage('0', '1')
}
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/step_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const { beginTiming, endTiming } = Time
async function run({
defaultTimeout,
hookParameter,
parameterTypeRegistry,
step,
stepDefinition,
world,
Expand All @@ -21,7 +20,6 @@ async function run({
parameters = await Promise.all(
stepDefinition.getInvocationParameters({
hookParameter,
parameterTypeRegistry,
step,
world,
})
Expand Down
6 changes: 1 addition & 5 deletions src/runtime/test_case_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,14 @@ export default class TestCaseRunner {

getStepDefinitions(step) {
return this.supportCodeLibrary.stepDefinitions.filter(stepDefinition =>
stepDefinition.matchesStepName({
stepName: step.text,
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry,
})
stepDefinition.matchesStepName(step.text)
)
}

invokeStep(step, stepDefinition, hookParameter) {
return StepRunner.run({
defaultTimeout: this.supportCodeLibrary.defaultTimeout,
hookParameter,
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry,
step,
stepDefinition,
world: this.world,
Expand Down
24 changes: 20 additions & 4 deletions src/support_code_library_builder/build_helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { deprecate } from 'util'
import _ from 'lodash'
import { formatLocation } from '../formatter/helpers'
import { ParameterType } from 'cucumber-expressions'
import {
ParameterType,
CucumberExpression,
RegularExpression,
} from 'cucumber-expressions'
import path from 'path'
import StackTrace from 'stacktrace-js'
import StepDefinition from '../models/step_definition'
Expand Down Expand Up @@ -51,7 +55,7 @@ export function buildTestRunHookDefinition({ options, code, cwd }) {
})
}

export function buildStepDefinition({ pattern, options, code, cwd }) {
export function buildStepDefinitionConfig({ pattern, options, code, cwd }) {
if (typeof options === 'function') {
code = options
options = {}
Expand All @@ -62,13 +66,25 @@ export function buildStepDefinition({ pattern, options, code, cwd }) {
fnName: 'defineStep',
location: formatLocation({ line, uri }),
})
return new StepDefinition({
return {
code,
line,
options,
pattern,
uri,
})
}
}

export function buildStepDefinitionFromConfig({
config,
parameterTypeRegistry,
}) {
const { code, line, options, uri, pattern } = config
const Expression =
typeof pattern === 'string' ? CucumberExpression : RegularExpression

const expression = new Expression(pattern, parameterTypeRegistry)
return new StepDefinition({ code, line, options, uri, pattern, expression })
}

const projectPath = path.join(__dirname, '..', '..')
Expand Down
17 changes: 13 additions & 4 deletions src/support_code_library_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import util from 'util'
import TransformLookupBuilder from './parameter_type_registry_builder'
import {
buildParameterType,
buildStepDefinition,
buildStepDefinitionConfig,
buildStepDefinitionFromConfig,
buildTestCaseHookDefinition,
buildTestRunHookDefinition,
} from './build_helpers'
Expand Down Expand Up @@ -40,13 +41,13 @@ export class SupportCodeLibraryBuilder {
}

defineStep(pattern, options, code) {
const stepDefinition = buildStepDefinition({
const stepDefinitionConfig = buildStepDefinitionConfig({
pattern,
options,
code,
cwd: this.cwd,
})
this.options.stepDefinitions.push(stepDefinition)
this.options.stepDefinitionConfigs.push(stepDefinitionConfig)
}

defineTestCaseHook(collectionName) {
Expand All @@ -72,6 +73,14 @@ export class SupportCodeLibraryBuilder {
}

finalize() {
this.options.stepDefinitions = this.options.stepDefinitionConfigs.map(
config =>
buildStepDefinitionFromConfig({
config,
parameterTypeRegistry: this.options.parameterTypeRegistry,
})
)
delete this.options.stepDefinitionConfigs
wrapDefinitions({
cwd: this.cwd,
definitionFunctionWrapper: this.options.definitionFunctionWrapper,
Expand Down Expand Up @@ -100,7 +109,7 @@ export class SupportCodeLibraryBuilder {
beforeTestRunHookDefinitions: [],
defaultTimeout: 5000,
definitionFunctionWrapper: null,
stepDefinitions: [],
stepDefinitionConfigs: [],
parameterTypeRegistry: TransformLookupBuilder.build(),
World({ attach, parameters }) {
this.attach = attach
Expand Down

0 comments on commit c4bd822

Please sign in to comment.