Skip to content

Commit 47b436f

Browse files
authored
refactor(test): toTextDocument, toTextEditor #5660
Problem: Redundant code, suboptimal names and docstrings. Solution: - Introduce `toTextDocument` for tests such as `codeParsingUtil.test.ts` which don't need a TextEditor. - Rename `openATextEditorWithText` => `toTextEditor`.
1 parent e350308 commit 47b436f

File tree

11 files changed

+107
-90
lines changed

11 files changed

+107
-90
lines changed

packages/amazonq/test/unit/codewhisperer/service/telemetry.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
assertTelemetry,
1414
assertTextEditorContains,
1515
createTestWorkspaceFolder,
16-
openATextEditorWithText,
16+
toTextEditor,
1717
} from 'aws-core-vscode/test'
1818
import {
1919
DefaultCodeWhispererClient,
@@ -156,7 +156,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
156156
describe('tab and esc', function () {
157157
it('single accept', async function () {
158158
assertSessionClean()
159-
const editor = await openATextEditorWithText('', 'test.py')
159+
const editor = await toTextEditor('', 'test.py')
160160

161161
await manualTrigger(editor, client, config)
162162
await acceptByTab()
@@ -166,7 +166,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
166166

167167
it('single reject', async function () {
168168
assertSessionClean()
169-
const editor = await openATextEditorWithText('', 'test.py')
169+
const editor = await toTextEditor('', 'test.py')
170170

171171
await manualTrigger(editor, client, config)
172172
await rejectByEsc()
@@ -179,7 +179,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
179179

180180
it('accept - accept - accept', async function () {
181181
assertSessionClean()
182-
const editor = await openATextEditorWithText('', 'test.py')
182+
const editor = await toTextEditor('', 'test.py')
183183

184184
await manualTrigger(editor, client, config)
185185
await acceptByTab()
@@ -190,7 +190,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
190190
await acceptByTab()
191191
await assertTextEditorContains(`FooBaz${os.EOL}Baz`)
192192

193-
const anotherEditor = await openATextEditorWithText('', 'anotherTest.py')
193+
const anotherEditor = await toTextEditor('', 'anotherTest.py')
194194
assertSessionClean()
195195
await manualTrigger(anotherEditor, client, config)
196196
await acceptByTab()
@@ -205,7 +205,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
205205

206206
it('accept - reject - accept', async function () {
207207
assertSessionClean()
208-
const editor = await openATextEditorWithText('', 'test.py')
208+
const editor = await toTextEditor('', 'test.py')
209209

210210
await manualTrigger(editor, client, config)
211211
await acceptByTab()
@@ -216,7 +216,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
216216
await rejectByEsc()
217217
await assertTextEditorContains('Foo')
218218

219-
const anotherEditor = await openATextEditorWithText('', 'anotherTest.py')
219+
const anotherEditor = await toTextEditor('', 'anotherTest.py')
220220
assertSessionClean()
221221
await manualTrigger(anotherEditor, client, config)
222222
await rejectByEsc()
@@ -231,7 +231,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
231231

232232
it('reject - reject - reject', async function () {
233233
assertSessionClean()
234-
const editor = await openATextEditorWithText('', 'test.py')
234+
const editor = await toTextEditor('', 'test.py')
235235

236236
await manualTrigger(editor, client, config)
237237
await rejectByEsc()
@@ -256,7 +256,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
256256

257257
it('reject - accept - reject', async function () {
258258
assertSessionClean()
259-
const editor = await openATextEditorWithText('', 'test.py')
259+
const editor = await toTextEditor('', 'test.py')
260260

261261
await manualTrigger(editor, client, config)
262262
await rejectByEsc()
@@ -302,7 +302,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
302302
}
303303

304304
assertSessionClean()
305-
const editor = await openATextEditorWithText('', 'test.py')
305+
const editor = await toTextEditor('', 'test.py')
306306

307307
await manualTrigger(editor, client, config)
308308
await navigateNext()
@@ -318,7 +318,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
318318
}
319319

320320
assertSessionClean()
321-
const editor = await openATextEditorWithText('', 'test.py')
321+
const editor = await toTextEditor('', 'test.py')
322322

323323
await manualTrigger(editor, client, config)
324324
await navigateNext()
@@ -336,7 +336,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
336336
}
337337

338338
assertSessionClean()
339-
const editor = await openATextEditorWithText('', 'test.py')
339+
const editor = await toTextEditor('', 'test.py')
340340

341341
await manualTrigger(editor, client, config)
342342
await navigateNext()
@@ -351,7 +351,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
351351
describe('typing', function () {
352352
it('typeahead match accept', async function () {
353353
assertSessionClean()
354-
const editor = await openATextEditorWithText('', 'test.py')
354+
const editor = await toTextEditor('', 'test.py')
355355

356356
await manualTrigger(editor, client, config)
357357
await typing(editor, 'F')
@@ -365,7 +365,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
365365

366366
it('typeahead match, backspace and accept', async function () {
367367
assertSessionClean()
368-
const editor = await openATextEditorWithText('', 'test.py')
368+
const editor = await toTextEditor('', 'test.py')
369369

370370
await manualTrigger(editor, client, config)
371371
await typing(editor, 'F')
@@ -385,7 +385,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
385385
// the solution is to waitUntil VSCode InlineSuggestion to resolve the "typeahead" but currently no easy way to know if the InlinSuggestion is shown visible again
386386
this.skip()
387387
assertSessionClean()
388-
const editor = await openATextEditorWithText('', 'test.py')
388+
const editor = await toTextEditor('', 'test.py')
389389

390390
await manualTrigger(editor, client, config)
391391
await typing(editor, 'F')
@@ -398,7 +398,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
398398
await rejectByEsc()
399399
await assertTextEditorContains('Foo')
400400

401-
const anotherEditor = await openATextEditorWithText('', 'anotherTest.py')
401+
const anotherEditor = await toTextEditor('', 'anotherTest.py')
402402
await manualTrigger(anotherEditor, client, config)
403403
await typing(anotherEditor, 'Qo')
404404
await assertTextEditorContains('Qo')
@@ -415,7 +415,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
415415

416416
it('typeahead not match after suggestion is shown and reject', async function () {
417417
assertSessionClean()
418-
const editor = await openATextEditorWithText('', 'test.py')
418+
const editor = await toTextEditor('', 'test.py')
419419

420420
await manualTrigger(editor, client, config)
421421
await typing(editor, 'H')
@@ -441,13 +441,13 @@ describe.skip('CodeWhisperer telemetry', async function () {
441441

442442
it('reject - typeahead not matching after suggestion is shown then invoke another round and accept', async function () {
443443
assertSessionClean()
444-
const editor = await openATextEditorWithText('', 'test.py')
444+
const editor = await toTextEditor('', 'test.py')
445445

446446
await manualTrigger(editor, client, config)
447447
await typing(editor, 'H')
448448
await assertTextEditorContains('H')
449449

450-
const anotherEditor = await openATextEditorWithText('', 'anotherTest.py')
450+
const anotherEditor = await toTextEditor('', 'anotherTest.py')
451451
await manualTrigger(anotherEditor, client, config)
452452
await acceptByTab()
453453
await assertTextEditorContains(`Baz${os.EOL}Baz`)
@@ -462,14 +462,14 @@ describe.skip('CodeWhisperer telemetry', async function () {
462462
describe('on editor change, focus change', function () {
463463
it('reject: trigger then open another editor', async function () {
464464
assertSessionClean()
465-
const editor = await openATextEditorWithText('', 'test.py', tempFolder, { preview: false })
465+
const editor = await toTextEditor('', 'test.py', tempFolder, { preview: false })
466466

467467
await manualTrigger(editor, client, config)
468468

469-
await openATextEditorWithText('text in 2nd editor', 'another1.py', tempFolder, {
469+
await toTextEditor('text in 2nd editor', 'another1.py', tempFolder, {
470470
preview: false,
471471
})
472-
const anotherEditor = await openATextEditorWithText('text in 3rd editor', 'another2.py', tempFolder, {
472+
const anotherEditor = await toTextEditor('text in 3rd editor', 'another2.py', tempFolder, {
473473
preview: false,
474474
})
475475

@@ -483,7 +483,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
483483

484484
it('reject: trigger then close editor', async function () {
485485
assertSessionClean()
486-
const editor = await openATextEditorWithText('', 'test.py')
486+
const editor = await toTextEditor('', 'test.py')
487487

488488
await manualTrigger(editor, client, config)
489489
await closeActiveEditor()
@@ -496,7 +496,7 @@ describe.skip('CodeWhisperer telemetry', async function () {
496496

497497
it('reject: onFocusChange', async function () {
498498
assertSessionClean()
499-
const editor = await openATextEditorWithText('', 'test.py')
499+
const editor = await toTextEditor('', 'test.py')
500500

501501
await manualTrigger(editor, client, config)
502502
await assertTextEditorContains('')

packages/amazonq/test/unit/codewhisperer/tracker/lineTracker.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { LineSelection, LineTracker, AuthUtil } from 'aws-core-vscode/codewhisperer'
77
import sinon from 'sinon'
88
import { Disposable, TextEditor, Position, Range, Selection } from 'vscode'
9-
import { openATextEditorWithText } from 'aws-core-vscode/test'
9+
import { toTextEditor } from 'aws-core-vscode/test'
1010
import assert from 'assert'
1111
import { waitUntil } from 'aws-core-vscode/shared'
1212

@@ -82,7 +82,7 @@ describe('LineTracker class', function () {
8282
describe('includes', function () {
8383
// util function to help set up LineTracker.selections
8484
async function setEditorSelection(selections: LineSelection[]): Promise<TextEditor> {
85-
const editor = await openATextEditorWithText('\n\n\n\n\n\n\n\n\n\n', 'foo.py', undefined, {
85+
const editor = await toTextEditor('\n\n\n\n\n\n\n\n\n\n', 'foo.py', undefined, {
8686
preview: false,
8787
})
8888

@@ -200,7 +200,7 @@ describe('LineTracker class', function () {
200200

201201
describe('onContentChanged', function () {
202202
it('should fire lineChangedEvent and set current line selection', async function () {
203-
editor = await openATextEditorWithText('\n\n\n\n\n', 'foo.py', undefined, { preview: false })
203+
editor = await toTextEditor('\n\n\n\n\n', 'foo.py', undefined, { preview: false })
204204
editor.selection = new Selection(new Position(5, 0), new Position(5, 0))
205205
assertEmptyCounts()
206206

@@ -222,7 +222,7 @@ describe('LineTracker class', function () {
222222

223223
describe('onTextEditorSelectionChanged', function () {
224224
it('should fire lineChangedEvent if selection changes and set current line selection', async function () {
225-
editor = await openATextEditorWithText('\n\n\n\n\n', 'foo.py', undefined, { preview: false })
225+
editor = await toTextEditor('\n\n\n\n\n', 'foo.py', undefined, { preview: false })
226226
editor.selection = new Selection(new Position(3, 0), new Position(3, 0))
227227
assertEmptyCounts()
228228

@@ -259,7 +259,7 @@ describe('LineTracker class', function () {
259259
it('should not fire lineChangedEvent if uri scheme is debug || output', async function () {
260260
// if the editor is not a text editor, won't emit an event and selection will be set to undefined
261261
async function assertLineChanged(schema: string) {
262-
const anotherEditor = await openATextEditorWithText('', 'bar.log', undefined, { preview: false })
262+
const anotherEditor = await toTextEditor('', 'bar.log', undefined, { preview: false })
263263
const uri = anotherEditor.document.uri
264264
sandbox.stub(uri, 'scheme').get(() => schema)
265265

packages/amazonq/test/unit/codewhisperer/util/closingBracketUtil.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as vscode from 'vscode'
77
import assert from 'assert'
88
import { handleExtraBrackets } from 'aws-core-vscode/codewhisperer'
9-
import { openATextEditorWithText } from 'aws-core-vscode/test'
9+
import { toTextEditor } from 'aws-core-vscode/test'
1010

1111
describe('closingBracketUtil', function () {
1212
/**
@@ -20,7 +20,7 @@ describe('closingBracketUtil', function () {
2020
recommendation: string,
2121
expected: string
2222
) {
23-
const editor = await openATextEditorWithText(leftContext + recommendation + rightContext, 'test.txt')
23+
const editor = await toTextEditor(leftContext + recommendation + rightContext, 'test.txt')
2424
const document = editor.document
2525

2626
const startStart = document.positionAt(0)

packages/amazonq/test/unit/codewhisperer/util/codeParsingUtil.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as vscode from 'vscode'
76
import {
87
PlatformLanguageId,
98
extractClasses,
109
extractFunctions,
1110
isTestFile,
1211
utgLanguageConfigs,
1312
} from 'aws-core-vscode/codewhisperer'
14-
import * as path from 'path'
1513
import assert from 'assert'
16-
import { createTestWorkspaceFolder, toFile } from 'aws-core-vscode/test'
14+
import { createTestWorkspaceFolder, toTextDocument } from 'aws-core-vscode/test'
1715

1816
describe('RegexValidationForPython', () => {
1917
it('should extract all function names from a python file content', () => {
@@ -119,9 +117,7 @@ describe('isTestFile', () => {
119117
expected: boolean
120118
) {
121119
for (const fileName of fileNames) {
122-
const p = path.join(testWsFolder, fileName)
123-
await toFile('', p)
124-
const document = await vscode.workspace.openTextDocument(p)
120+
const document = await toTextDocument('', fileName, testWsFolder)
125121
const actual = await isTestFile(document.uri.fsPath, { languageId: config.languageId })
126122
assert.strictEqual(actual, expected)
127123
}

packages/amazonq/test/unit/codewhisperer/util/crossFileContextUtil.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
assertTabCount,
1414
closeAllEditors,
1515
createTestWorkspaceFolder,
16-
openATextEditorWithText,
16+
toTextEditor,
1717
shuffleList,
1818
toFile,
1919
} from 'aws-core-vscode/test'
@@ -38,8 +38,8 @@ describe('crossFileContextUtil', function () {
3838

3939
describe('should fetch 3 chunks and each chunk should contains 10 lines', function () {
4040
async function assertCorrectCodeChunk() {
41-
await openATextEditorWithText(sampleFileOf60Lines, 'CrossFile.java', tempFolder, { preview: false })
42-
const myCurrentEditor = await openATextEditorWithText('', 'TargetFile.java', tempFolder, {
41+
await toTextEditor(sampleFileOf60Lines, 'CrossFile.java', tempFolder, { preview: false })
42+
const myCurrentEditor = await toTextEditor('', 'TargetFile.java', tempFolder, {
4343
preview: false,
4444
})
4545
const actual = await crossFile.fetchSupplementalContextForSrc(myCurrentEditor, fakeCancellationToken)
@@ -120,12 +120,12 @@ describe('crossFileContextUtil', function () {
120120
const shuffledFilePaths = shuffleList(expectedFilePaths)
121121

122122
for (const filePath of shuffledFilePaths) {
123-
await openATextEditorWithText('', filePath, tempFolder, { preview: false })
123+
await toTextEditor('', filePath, tempFolder, { preview: false })
124124
}
125125

126-
await openATextEditorWithText('', testFile1, tempFolder, { preview: false })
127-
await openATextEditorWithText('', testFile2, tempFolder, { preview: false })
128-
const editor = await openATextEditorWithText('', targetFile, tempFolder, { preview: false })
126+
await toTextEditor('', testFile1, tempFolder, { preview: false })
127+
await toTextEditor('', testFile2, tempFolder, { preview: false })
128+
const editor = await toTextEditor('', targetFile, tempFolder, { preview: false })
129129

130130
await assertTabCount(shuffledFilePaths.length + 3)
131131

@@ -158,10 +158,10 @@ describe('crossFileContextUtil', function () {
158158

159159
fileExtLists.forEach((fileExt) => {
160160
it('should be empty if userGroup is control', async function () {
161-
const editor = await openATextEditorWithText('content-1', `file-1.${fileExt}`, tempFolder)
162-
await openATextEditorWithText('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
163-
await openATextEditorWithText('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
164-
await openATextEditorWithText('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
161+
const editor = await toTextEditor('content-1', `file-1.${fileExt}`, tempFolder)
162+
await toTextEditor('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
163+
await toTextEditor('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
164+
await toTextEditor('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
165165

166166
await assertTabCount(4)
167167

@@ -190,10 +190,10 @@ describe('crossFileContextUtil', function () {
190190

191191
fileExtLists.forEach((fileExt) => {
192192
it('should be non empty if usergroup is Crossfile', async function () {
193-
const editor = await openATextEditorWithText('content-1', `file-1.${fileExt}`, tempFolder)
194-
await openATextEditorWithText('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
195-
await openATextEditorWithText('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
196-
await openATextEditorWithText('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
193+
const editor = await toTextEditor('content-1', `file-1.${fileExt}`, tempFolder)
194+
await toTextEditor('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
195+
await toTextEditor('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
196+
await toTextEditor('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
197197

198198
await assertTabCount(4)
199199

@@ -222,10 +222,10 @@ describe('crossFileContextUtil', function () {
222222

223223
fileExtLists.forEach((fileExt) => {
224224
it('should be non empty', async function () {
225-
const editor = await openATextEditorWithText('content-1', `file-1.${fileExt}`, tempFolder)
226-
await openATextEditorWithText('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
227-
await openATextEditorWithText('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
228-
await openATextEditorWithText('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
225+
const editor = await toTextEditor('content-1', `file-1.${fileExt}`, tempFolder)
226+
await toTextEditor('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
227+
await toTextEditor('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
228+
await toTextEditor('content-4', `file-4.${fileExt}`, tempFolder, { preview: false })
229229

230230
await assertTabCount(4)
231231

packages/amazonq/test/unit/codewhisperer/util/editorContext.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
createMockTextEditor,
1010
createMockClientRequest,
1111
resetCodeWhispererGlobalVariables,
12-
openATextEditorWithText,
12+
toTextEditor,
1313
createTestWorkspaceFolder,
1414
closeAllEditors,
1515
} from 'aws-core-vscode/test'
@@ -104,7 +104,7 @@ describe('editorContext', function () {
104104
})
105105

106106
it('Should return relative path', async function () {
107-
const editor = await openATextEditorWithText('tttt', 'test.py', tempFolder)
107+
const editor = await toTextEditor('tttt', 'test.py', tempFolder)
108108
const actual = EditorContext.getFileRelativePath(editor)
109109
const expected = 'test.py'
110110
assert.strictEqual(actual, expected)

0 commit comments

Comments
 (0)