-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fake-cucumber: WIP add StepDefinitionRegistry
- Loading branch information
1 parent
8de76ad
commit 70c0ead
Showing
6 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
}) | ||
}) | ||
}); |