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

Support doc strings and data tables #804

Closed
wants to merge 1 commit 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
12 changes: 9 additions & 3 deletions fake-cucumber/javascript/src/ExpressionStepDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ export default class ExpressionStepDefinition implements IStepDefinition {
public match(
pickleStep: messages.Pickle.IPickleStep
): SupportCodeExecutor | null {
const args = this.getArguments(pickleStep.text)
return args === null
const expressionArgs = this.getArguments(pickleStep.text)
return expressionArgs === null
? null
: new SupportCodeExecutor(this.id, this.body, args)
: new SupportCodeExecutor(
this.id,
this.body,
expressionArgs,
pickleStep.argument && pickleStep.argument.docString,
pickleStep.argument && pickleStep.argument.dataTable
)
}

public getArguments(text: string): Array<Argument<any>> {
Expand Down
31 changes: 23 additions & 8 deletions fake-cucumber/javascript/src/SupportCodeExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import { Argument } from 'cucumber-expressions'
import { Argument, Group } from 'cucumber-expressions'
import { messages } from 'cucumber-messages'

export default class SupportCodeExecutor {
constructor(
public readonly stepDefinitionId: string,
private readonly body: (...args: any) => any,
private readonly args: Array<Argument<any>>
private readonly args: Array<Argument<any>>,
private readonly docString: messages.PickleStepArgument.IPickleDocString,
private readonly dataTable: messages.PickleStepArgument.IPickleTable
) {}

public execute(): any {
const thisObj: any = null
return this.body.apply(
thisObj,
this.args.map(arg => arg.getValue(thisObj))
)
const argArray = this.args.map(arg => arg.getValue(thisObj))
if (this.docString) {
// TODO: Hand off to DocStringTransformer
argArray.push(this.docString.content)
}
if (this.dataTable) {
// TODO: Hand off to DataTableTransformer
argArray.push(this.dataTable.rows.map(r => r.cells.map(c => c.value)))
}
return this.body.apply(thisObj, argArray)
}

public argsToMessages(): messages.IStepMatchArgument[] {
return this.args.map(arg => {
return new messages.StepMatchArgument({
// TODO: add recursive transformation.
group: arg.group,
group: toMessageGroup(arg.group),
parameterTypeName: arg.parameterType.name,
})
})
}
}

function toMessageGroup(group: Group): messages.StepMatchArgument.IGroup {
return new messages.StepMatchArgument.Group({
value: group.value,
start: group.start,
children: group.children.map(g => toMessageGroup(g)),
})
}
7 changes: 7 additions & 0 deletions fake-cucumber/javascript/src/TestStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class TestStep {
return this.emitTestStepFinished(
testCaseStartedId,
messages.TestResult.Status.UNDEFINED,
null,
notifier
)
}
Expand All @@ -43,6 +44,7 @@ export default class TestStep {
return this.emitTestStepFinished(
testCaseStartedId,
messages.TestResult.Status.AMBIGUOUS,
null,
notifier
)
}
Expand All @@ -54,12 +56,14 @@ export default class TestStep {
result === 'pending'
? messages.TestResult.Status.PENDING
: messages.TestResult.Status.PASSED,
null,
notifier
)
} catch (error) {
return this.emitTestStepFinished(
testCaseStartedId,
messages.TestResult.Status.FAILED,
error.message,
notifier
)
}
Expand All @@ -72,6 +76,7 @@ export default class TestStep {
return this.emitTestStepFinished(
testCaseStartedId,
messages.TestResult.Status.SKIPPED,
null,
notifier
)
}
Expand All @@ -93,6 +98,7 @@ export default class TestStep {
protected emitTestStepFinished(
testCaseStartedId: string,
status: messages.TestResult.Status,
message: string,
notifier: MessageNotifier
): messages.TestResult.Status {
notifier(
Expand All @@ -102,6 +108,7 @@ export default class TestStep {
testStepId: this.id,
testResult: new messages.TestResult({
status,
message,
duration: new messages.Duration({
seconds: 123,
nanos: 456,
Expand Down
7 changes: 6 additions & 1 deletion fake-cucumber/javascript/test/TestCaseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class StubTestStep extends TestStep {
notifier: MessageNotifier,
testCaseStartedId: string
): messages.TestResult.Status {
return this.emitTestStepFinished(testCaseStartedId, this.status, notifier)
return this.emitTestStepFinished(
testCaseStartedId,
this.status,
null,
notifier
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion fake-cucumber/javascript/test/TestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function stubMatchingStepDefinition(
executor: SupportCodeExecutor = new SupportCodeExecutor(
'some-id',
() => null,
[]
[],
null,
null
)
): ExpressionStepDefinition {
const stepDefinitionStub = stubConstructor(ExpressionStepDefinition)
Expand Down
35 changes: 35 additions & 0 deletions fake-cucumber/javascript/test/TestStepTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
stubPendingSupportCodeExecutor,
} from './TestHelpers'
import makePickleTestStep from '../src/makePickleTestStep'
import SupportCodeExecutor from '../src/SupportCodeExecutor'

function execute(testStep: TestStep): messages.ITestStepFinished {
const receivedMessages: messages.IEnvelope[] = []
Expand Down Expand Up @@ -120,6 +121,40 @@ describe('TestStep', () => {
)
assert.strictEqual(testStepFinished.testStepId, testStep.id)
})

it('emits a TestStepFinished with error message from docstring', () => {
const docString = new messages.PickleStepArgument.PickleDocString({
content: 'hello',
})
const testStep = makePickleTestStep(
messages.Pickle.PickleStep.create({
text: 'a passed step',
argument: new messages.PickleStepArgument({
docString,
}),
}),
[
stubMatchingStepDefinition(
new SupportCodeExecutor(
'an-id',
(docStringArg: string) => {
throw new Error(`error from ${docStringArg}`)
},
[],
docString,
null
)
),
]
)

const testStepFinished = execute(testStep)
assert.strictEqual(
testStepFinished.testResult.message,
'error from hello'
)
assert.strictEqual(testStepFinished.testStepId, testStep.id)
})
})
})
})