-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
keyword_type.ts
40 lines (37 loc) · 1 KB
/
keyword_type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import _ from 'lodash'
import Gherkin from 'gherkin'
import { doesHaveValue } from '../../value_checker'
import Dialect from 'gherkin/dist/src/Dialect'
export enum KeywordType {
Precondition = 'precondition',
Event = 'event',
Outcome = 'outcome',
}
export interface IGetStepKeywordTypeOptions {
keyword: string
language: string
previousKeywordType?: KeywordType
}
export function getStepKeywordType({
keyword,
language,
previousKeywordType,
}: IGetStepKeywordTypeOptions): KeywordType {
const dialect: Dialect = Gherkin.dialects()[language]
const stepKeywords = ['given', 'when', 'then', 'and', 'but'] as const
const type = _.find(stepKeywords, key => _.includes(dialect[key], keyword))
switch (type) {
case 'when':
return KeywordType.Event
case 'then':
return KeywordType.Outcome
case 'and':
case 'but':
if (doesHaveValue(previousKeywordType)) {
return previousKeywordType
}
// fallthrough
default:
return KeywordType.Precondition
}
}