|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import * as path from 'path'; |
| 8 | +import testAssetWorkspace from './testAssets/testAssetWorkspace'; |
| 9 | +import { |
| 10 | + activateCSharpExtension, |
| 11 | + closeAllEditorsAsync, |
| 12 | + navigate, |
| 13 | + openFileInWorkspaceAsync, |
| 14 | + sleep, |
| 15 | + sortLocations, |
| 16 | +} from './integrationHelpers'; |
| 17 | +import { beforeAll, beforeEach, afterAll, test, expect, afterEach, describe } from '@jest/globals'; |
| 18 | + |
| 19 | +describe(`Source Generator Tests`, () => { |
| 20 | + beforeAll(async () => { |
| 21 | + await activateCSharpExtension(); |
| 22 | + }); |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + await openFileInWorkspaceAsync(path.join('src', 'app', 'SourceGenerator.cs')); |
| 26 | + |
| 27 | + // Unfortunately, due to the way source generators work we will not necessarily have the source generated files |
| 28 | + // as soon as the project finishes loading. It may be using a partial compilation which has not run generators yet. |
| 29 | + // So we have to wait here for a bit to ensure the source generated files are available. |
| 30 | + // Once we have enabled balanced mode in the extension, we will have an explicit command to run generators which |
| 31 | + // we can use here to force the generation instead of waiting. |
| 32 | + // See https://github.com/dotnet/roslyn/issues/75152 |
| 33 | + await sleep(5000); |
| 34 | + }); |
| 35 | + |
| 36 | + afterAll(async () => { |
| 37 | + await testAssetWorkspace.cleanupWorkspace(); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + await closeAllEditorsAsync(); |
| 42 | + }); |
| 43 | + |
| 44 | + test('Navigates to reference in source generated file', async () => { |
| 45 | + const requestPosition = new vscode.Position(14, 25); |
| 46 | + const referencesList = <vscode.Location[]>( |
| 47 | + await vscode.commands.executeCommand( |
| 48 | + 'vscode.executeReferenceProvider', |
| 49 | + vscode.window.activeTextEditor!.document.uri, |
| 50 | + requestPosition |
| 51 | + ) |
| 52 | + ); |
| 53 | + expect(referencesList.length).toEqual(8); |
| 54 | + const referencesInGeneratedFiles = sortLocations( |
| 55 | + referencesList.filter((r) => r.uri.scheme === 'roslyn-source-generated') |
| 56 | + ); |
| 57 | + expect(referencesInGeneratedFiles.length).toEqual(7); |
| 58 | + const firstPath = referencesInGeneratedFiles[0].uri.path; |
| 59 | + expect(firstPath).toEqual('/SourceGenerationContext.g.cs'); |
| 60 | + |
| 61 | + await navigate(requestPosition, referencesInGeneratedFiles, 'SourceGenerationContext.g.cs'); |
| 62 | + expect(vscode.window.activeTextEditor?.document.getText()).toContain('// <auto-generated/>'); |
| 63 | + expect(vscode.window.activeTextEditor?.document.getText()).toContain( |
| 64 | + 'internal partial class SourceGenerationContext' |
| 65 | + ); |
| 66 | + }); |
| 67 | +}); |
0 commit comments