|
| 1 | +import { |
| 2 | + Examples, |
| 3 | + Feature, |
| 4 | + GherkinDocument, |
| 5 | + Pickle, |
| 6 | + Rule, |
| 7 | + Scenario, |
| 8 | + TableRow, |
| 9 | +} from '@cucumber/messages' |
| 10 | + |
| 11 | +export interface Lineage { |
| 12 | + gherkinDocument?: GherkinDocument |
| 13 | + feature?: Feature |
| 14 | + rule?: Rule |
| 15 | + scenario?: Scenario |
| 16 | + examples?: Examples |
| 17 | + examplesIndex?: number |
| 18 | + example?: TableRow |
| 19 | + exampleIndex?: number |
| 20 | +} |
| 21 | + |
| 22 | +export interface LineageReducer<T> { |
| 23 | + reduce: (lineage: Lineage, pickle: Pickle) => T |
| 24 | +} |
| 25 | + |
| 26 | +export type NamingStrategy = LineageReducer<string> |
| 27 | + |
| 28 | +export enum NamingStrategyLength { |
| 29 | + LONG = 'LONG', |
| 30 | + SHORT = 'SHORT', |
| 31 | +} |
| 32 | + |
| 33 | +export enum NamingStrategyFeatureName { |
| 34 | + INCLUDE = 'INCLUDE', |
| 35 | + EXCLUDE = 'EXCLUDE', |
| 36 | +} |
| 37 | + |
| 38 | +export enum NamingStrategyExampleName { |
| 39 | + NUMBER = 'NUMBER', |
| 40 | + PICKLE = 'PICKLE', |
| 41 | + NUMBER_AND_PICKLE_IF_PARAMETERIZED = 'NUMBER_AND_PICKLE_IF_PARAMETERIZED', |
| 42 | +} |
| 43 | + |
| 44 | +class BuiltinNamingStrategy implements NamingStrategy { |
| 45 | + constructor( |
| 46 | + private readonly length: NamingStrategyLength, |
| 47 | + private readonly featureName: NamingStrategyFeatureName, |
| 48 | + private readonly exampleName: NamingStrategyExampleName |
| 49 | + ) {} |
| 50 | + |
| 51 | + reduce(lineage: Lineage, pickle: Pickle): string { |
| 52 | + const parts: string[] = [] |
| 53 | + |
| 54 | + if (lineage.feature?.name && this.featureName === NamingStrategyFeatureName.INCLUDE) { |
| 55 | + parts.push(lineage.feature.name) |
| 56 | + } |
| 57 | + |
| 58 | + if (lineage.rule?.name) { |
| 59 | + parts.push(lineage.rule.name) |
| 60 | + } |
| 61 | + |
| 62 | + if (lineage.scenario?.name) { |
| 63 | + parts.push(lineage.scenario.name) |
| 64 | + } else { |
| 65 | + parts.push(pickle.name) |
| 66 | + } |
| 67 | + |
| 68 | + if (lineage.examples?.name) { |
| 69 | + parts.push(lineage.examples.name) |
| 70 | + } |
| 71 | + |
| 72 | + if (lineage.example) { |
| 73 | + const exampleNumber = [ |
| 74 | + '#', |
| 75 | + (lineage.examplesIndex ?? 0) + 1, |
| 76 | + '.', |
| 77 | + (lineage.exampleIndex ?? 0) + 1, |
| 78 | + ].join('') |
| 79 | + |
| 80 | + switch (this.exampleName) { |
| 81 | + case NamingStrategyExampleName.NUMBER: |
| 82 | + parts.push(exampleNumber) |
| 83 | + break |
| 84 | + case NamingStrategyExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED: |
| 85 | + if (lineage.scenario?.name !== pickle.name) { |
| 86 | + parts.push(exampleNumber + ': ' + pickle.name) |
| 87 | + } else { |
| 88 | + parts.push(exampleNumber) |
| 89 | + } |
| 90 | + break |
| 91 | + case NamingStrategyExampleName.PICKLE: |
| 92 | + parts.push(pickle.name) |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (this.length === NamingStrategyLength.SHORT) { |
| 98 | + return parts.at(-1) as string |
| 99 | + } |
| 100 | + return parts.filter((part) => !!part).join(' - ') |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export function namingStrategy( |
| 105 | + length: NamingStrategyLength, |
| 106 | + featureName: NamingStrategyFeatureName, |
| 107 | + exampleName: NamingStrategyExampleName |
| 108 | +): NamingStrategy { |
| 109 | + return new BuiltinNamingStrategy(length, featureName, exampleName) |
| 110 | +} |
0 commit comments