Skip to content

Commit

Permalink
fake-cucumber: WIP add StepDefinitionRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-psarga committed Nov 14, 2019
1 parent 8de76ad commit 70c0ead
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
Binary file added execution_sequence_diagram.monopic
Binary file not shown.
1 change: 1 addition & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"nyc": "^14.1.1",
"prettier": "^1.19.1",
"ts-node": "^8.5.0",
"ts-sinon": "^1.0.24",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
Expand Down
7 changes: 7 additions & 0 deletions javascript/src/Match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { messages } from 'cucumber-messages'

export default class Match {
public execute(): messages.TestResult.Status {
return null
}
}
11 changes: 11 additions & 0 deletions javascript/src/StepDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Match from './Match'

export default class StepDefinition {
constructor (
private readonly expression: string
) {}

public match(text: string): Match {
return null
}
}
24 changes: 24 additions & 0 deletions javascript/src/StepDefinitionRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { messages } from 'cucumber-messages'
import StepDefinition from './StepDefinition'

export default class StepDefinitionRegistry {
constructor(
private readonly stepDefinitions: StepDefinition[]
) {}

public execute(text: string): messages.TestResult.Status {
const matches = this.stepDefinitions
.map(sd => sd.match(text))
.filter(match => match !== null)

if (matches.length === 0) {
return messages.TestResult.Status.UNDEFINED
}

if (matches.length > 1) {
return messages.TestResult.Status.AMBIGUOUS
}

return matches[0].execute()
}
}
49 changes: 49 additions & 0 deletions javascript/test/StepDefinitionRegistryTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import assert from 'assert'
import { stubConstructor } from "ts-sinon";
import { messages } from 'cucumber-messages'

import Match from '../src/Match'
import StepDefinition from '../src/StepDefinition'
import StepDefinitionRegistry from '../src/StepDefinitionRegistry'

describe('StepDefinitionRegistry', () => {
context('execute', () => {
function stubMatch(result: messages.TestResult.Status): Match {
const matchStub = stubConstructor<Match>(Match)
matchStub.execute.returns(result)

return matchStub
}

function stubMatchingStepDefinition(match: Match = new Match): StepDefinition {
const stepDefinitionStub = stubConstructor<StepDefinition>(StepDefinition);
stepDefinitionStub.match.returns(match);

return stepDefinitionStub
}

it('returns UNDEFINED when there are no matching step definitions', () => {
const subject = new StepDefinitionRegistry([])
const status = subject.execute('whatever ...')
assert.strictEqual(status, messages.TestResult.Status.UNDEFINED)
})

it('returns AMBIGUOUS when there are multiple matching step definitions', () => {
const subject = new StepDefinitionRegistry([
stubMatchingStepDefinition(),
stubMatchingStepDefinition()
])
const status = subject.execute('ambiguous step')
assert.strictEqual(status, messages.TestResult.Status.AMBIGUOUS)
})

it ('returns the status after match execution', () => {
const subject = new StepDefinitionRegistry([
stubMatchingStepDefinition(stubMatch(messages.TestResult.Status.PASSED))
])
const status = subject.execute('whatever ...')
assert.strictEqual(status, messages.TestResult.Status.PASSED)

})
})
});

0 comments on commit 70c0ead

Please sign in to comment.