-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7632 from dibarbet/sg_tests
Add integration test for navigating to source generated file
- Loading branch information
Showing
4 changed files
with
113 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
test/lsptoolshost/integrationTests/sourceGenerator.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import testAssetWorkspace from './testAssets/testAssetWorkspace'; | ||
import { | ||
activateCSharpExtension, | ||
closeAllEditorsAsync, | ||
navigate, | ||
openFileInWorkspaceAsync, | ||
sleep, | ||
sortLocations, | ||
} from './integrationHelpers'; | ||
import { beforeAll, beforeEach, afterAll, test, expect, afterEach, describe } from '@jest/globals'; | ||
|
||
describe(`Source Generator Tests`, () => { | ||
beforeAll(async () => { | ||
await activateCSharpExtension(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await openFileInWorkspaceAsync(path.join('src', 'app', 'SourceGenerator.cs')); | ||
|
||
// Unfortunately, due to the way source generators work we will not necessarily have the source generated files | ||
// as soon as the project finishes loading. It may be using a partial compilation which has not run generators yet. | ||
// So we have to wait here for a bit to ensure the source generated files are available. | ||
// Once we have enabled balanced mode in the extension, we will have an explicit command to run generators which | ||
// we can use here to force the generation instead of waiting. | ||
// See https://github.com/dotnet/roslyn/issues/75152 | ||
await sleep(5000); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testAssetWorkspace.cleanupWorkspace(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await closeAllEditorsAsync(); | ||
}); | ||
|
||
test('Navigates to reference in source generated file', async () => { | ||
const requestPosition = new vscode.Position(14, 25); | ||
const referencesList = <vscode.Location[]>( | ||
await vscode.commands.executeCommand( | ||
'vscode.executeReferenceProvider', | ||
vscode.window.activeTextEditor!.document.uri, | ||
requestPosition | ||
) | ||
); | ||
expect(referencesList.length).toEqual(8); | ||
const referencesInGeneratedFiles = sortLocations( | ||
referencesList.filter((r) => r.uri.scheme === 'roslyn-source-generated') | ||
); | ||
expect(referencesInGeneratedFiles.length).toEqual(7); | ||
const firstPath = referencesInGeneratedFiles[0].uri.path; | ||
expect(firstPath).toEqual('/SourceGenerationContext.g.cs'); | ||
|
||
await navigate(requestPosition, referencesInGeneratedFiles, 'SourceGenerationContext.g.cs'); | ||
expect(vscode.window.activeTextEditor?.document.getText()).toContain('// <auto-generated/>'); | ||
expect(vscode.window.activeTextEditor?.document.getText()).toContain( | ||
'internal partial class SourceGenerationContext' | ||
); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/app/SourceGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SourceGeneration; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
|
||
[JsonSourceGenerationOptions(WriteIndented = true)] | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class SourceGenerationContext : JsonSerializerContext | ||
{ | ||
} |