|
| 1 | +import { ParameterTypeRegistry } from '@cucumber/cucumber-expressions' |
| 2 | +import assert from 'assert' |
| 3 | +import { CodeAction, LocationLink, Range } from 'vscode-languageserver-types' |
| 4 | + |
| 5 | +import { getGenerateSnippetCodeAction } from '../../src/service/getGenerateSnippetCodeAction.js' |
| 6 | +import { makeUndefinedStepDiagnostic } from '../../src/service/getGherkinDiagnostics.js' |
| 7 | + |
| 8 | +describe('getGenerateSnippetCodeAction', () => { |
| 9 | + it('generates code in a new file', () => { |
| 10 | + const diagnostic = makeUndefinedStepDiagnostic(10, 4, 'Given ', 'I have 43 cukes') |
| 11 | + |
| 12 | + const targetRange = Range.create(10, 0, 10, 0) |
| 13 | + const link: LocationLink = { |
| 14 | + targetUri: 'file://home/features/step_definitions/steps.ts', |
| 15 | + targetRange, |
| 16 | + targetSelectionRange: targetRange, |
| 17 | + } |
| 18 | + const action = getGenerateSnippetCodeAction( |
| 19 | + [diagnostic], |
| 20 | + link, |
| 21 | + 'step_definitions/steps.ts', |
| 22 | + true, |
| 23 | + undefined, |
| 24 | + 'typescript', |
| 25 | + new ParameterTypeRegistry() |
| 26 | + ) |
| 27 | + const expectedAction: CodeAction = { |
| 28 | + title: 'Define in step_definitions/steps.ts', |
| 29 | + diagnostics: [ |
| 30 | + { |
| 31 | + severity: 2, |
| 32 | + range: { |
| 33 | + start: { |
| 34 | + line: 10, |
| 35 | + character: 4, |
| 36 | + }, |
| 37 | + end: { |
| 38 | + line: 10, |
| 39 | + character: 19, |
| 40 | + }, |
| 41 | + }, |
| 42 | + message: 'Undefined step: I have 43 cukes', |
| 43 | + source: 'Cucumber', |
| 44 | + code: 'cucumber.undefined-step', |
| 45 | + codeDescription: { |
| 46 | + href: 'https://cucumber.io/docs/cucumber/step-definitions/', |
| 47 | + }, |
| 48 | + data: { |
| 49 | + stepKeyword: 'Given ', |
| 50 | + stepText: 'I have 43 cukes', |
| 51 | + }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + kind: 'quickfix', |
| 55 | + edit: { |
| 56 | + documentChanges: [ |
| 57 | + { |
| 58 | + kind: 'create', |
| 59 | + uri: 'file://home/features/step_definitions/steps.ts', |
| 60 | + options: { |
| 61 | + ignoreIfExists: true, |
| 62 | + overwrite: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + textDocument: { |
| 67 | + uri: 'file://home/features/step_definitions/steps.ts', |
| 68 | + version: 0, |
| 69 | + }, |
| 70 | + edits: [ |
| 71 | + { |
| 72 | + range: { |
| 73 | + start: { |
| 74 | + line: 10, |
| 75 | + character: 0, |
| 76 | + }, |
| 77 | + end: { |
| 78 | + line: 10, |
| 79 | + character: 0, |
| 80 | + }, |
| 81 | + }, |
| 82 | + newText: ` |
| 83 | +Given('I have {int} cukes', (int: number) => { |
| 84 | + // Write code here that turns the phrase above into concrete actions |
| 85 | +}) |
| 86 | +`, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + isPreferred: true, |
| 93 | + } |
| 94 | + |
| 95 | + assert.deepStrictEqual(action, expectedAction) |
| 96 | + }) |
| 97 | + |
| 98 | + it('generates code in an existing file', () => { |
| 99 | + const diagnostic = makeUndefinedStepDiagnostic(10, 4, 'Given ', 'I have 43 cukes') |
| 100 | + |
| 101 | + const targetRange = Range.create(10, 0, 10, 0) |
| 102 | + const link: LocationLink = { |
| 103 | + targetUri: 'file://home/features/step_definitions/steps.ts', |
| 104 | + targetRange, |
| 105 | + targetSelectionRange: targetRange, |
| 106 | + } |
| 107 | + |
| 108 | + const action = getGenerateSnippetCodeAction( |
| 109 | + [diagnostic], |
| 110 | + link, |
| 111 | + 'step_definitions/steps.ts', |
| 112 | + false, |
| 113 | + undefined, |
| 114 | + 'typescript', |
| 115 | + new ParameterTypeRegistry() |
| 116 | + ) |
| 117 | + const expectedAction: CodeAction = { |
| 118 | + title: 'Define in step_definitions/steps.ts', |
| 119 | + diagnostics: [ |
| 120 | + { |
| 121 | + severity: 2, |
| 122 | + range: { |
| 123 | + start: { |
| 124 | + line: 10, |
| 125 | + character: 4, |
| 126 | + }, |
| 127 | + end: { |
| 128 | + line: 10, |
| 129 | + character: 19, |
| 130 | + }, |
| 131 | + }, |
| 132 | + message: 'Undefined step: I have 43 cukes', |
| 133 | + source: 'Cucumber', |
| 134 | + code: 'cucumber.undefined-step', |
| 135 | + codeDescription: { |
| 136 | + href: 'https://cucumber.io/docs/cucumber/step-definitions/', |
| 137 | + }, |
| 138 | + data: { |
| 139 | + stepKeyword: 'Given ', |
| 140 | + stepText: 'I have 43 cukes', |
| 141 | + }, |
| 142 | + }, |
| 143 | + ], |
| 144 | + kind: 'quickfix', |
| 145 | + edit: { |
| 146 | + documentChanges: [ |
| 147 | + { |
| 148 | + textDocument: { |
| 149 | + uri: 'file://home/features/step_definitions/steps.ts', |
| 150 | + version: 0, |
| 151 | + }, |
| 152 | + edits: [ |
| 153 | + { |
| 154 | + range: { |
| 155 | + start: { |
| 156 | + line: 10, |
| 157 | + character: 0, |
| 158 | + }, |
| 159 | + end: { |
| 160 | + line: 10, |
| 161 | + character: 0, |
| 162 | + }, |
| 163 | + }, |
| 164 | + newText: ` |
| 165 | +Given('I have {int} cukes', (int: number) => { |
| 166 | + // Write code here that turns the phrase above into concrete actions |
| 167 | +}) |
| 168 | +`, |
| 169 | + }, |
| 170 | + ], |
| 171 | + }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + isPreferred: true, |
| 175 | + } |
| 176 | + assert.deepStrictEqual(action, expectedAction) |
| 177 | + }) |
| 178 | +}) |
0 commit comments