-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test project for Razor source generator #16453
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb7f931
Add test project for Razor source generator
captainsafia acb171f
Fix test with line endings on Windows
captainsafia 007f353
Shorten project name
captainsafia 45a4faa
Address feedback from peer review
captainsafia 4ff77f0
Update SourceTextSourceLineCollection.cs
captainsafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.NET.Sdk.Razor.SourceGenerators.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")] |
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
36 changes: 36 additions & 0 deletions
36
src/RazorSdk/SourceGenerators/SourceTextSourceLineCollection.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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
public class SourceTextSourceLineCollection : RazorSourceLineCollection | ||
{ | ||
private readonly string _filePath; | ||
private readonly TextLineCollection _textLines; | ||
|
||
public SourceTextSourceLineCollection(string filePath, TextLineCollection textLines) | ||
{ | ||
_filePath = filePath; | ||
_textLines = textLines; | ||
} | ||
|
||
public override int Count => _textLines.Count; | ||
|
||
public override int GetLineLength(int index) | ||
{ | ||
var line = _textLines[index]; | ||
return line.EndIncludingLineBreak - line.Start; | ||
} | ||
|
||
internal override SourceLocation GetLocation(int position) | ||
{ | ||
var line = _textLines.GetLineFromPosition(position); | ||
return new SourceLocation(_filePath, position, line.LineNumber, position); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ET.Sdk.Razor.SourceGenerators.Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests.csproj
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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<EnableDefaultItems>false</EnableDefaultItems> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(SdkTargetFramework)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>testSdkRSG</PackageId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpPackageVersion)" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguageVersion)"/> | ||
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" /> | ||
<PackageReference Include="Microsoft.Build.Framework" Version="$(MicrosoftBuildFrameworkPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildUtilitiesCorePackageVersion)" /> | ||
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\RazorSdk\SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.csproj" /> | ||
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
126 changes: 126 additions & 0 deletions
126
src/Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/SourceGeneratorProjectItemTest.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,126 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
using Moq; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators.Tests | ||
{ | ||
public class SourceGeneratorProjectItemTest | ||
{ | ||
[Fact] | ||
public void PhysicalPath_ReturnsSourceTextPath() | ||
{ | ||
// Arrange | ||
var emptyBasePath = "/"; | ||
var path = "/foo/bar.cshtml"; | ||
var projectItem = new SourceGeneratorProjectItem( | ||
filePath: path, | ||
basePath: emptyBasePath, | ||
relativePhysicalPath: "/foo", | ||
fileKind: FileKinds.Legacy, | ||
additionalText: new TestAdditionalText(string.Empty), | ||
cssScope: null, | ||
context: new GeneratorExecutionContext()); | ||
|
||
// Act | ||
var physicalPath = projectItem.PhysicalPath; | ||
|
||
// Assert | ||
Assert.Equal("dummy", physicalPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("/Home/Index")] | ||
[InlineData("EditUser")] | ||
public void Extension_ReturnsNullIfFileDoesNotHaveExtension(string path) | ||
{ | ||
// Arrange | ||
var projectItem = new SourceGeneratorProjectItem( | ||
filePath: path, | ||
basePath: "/views", | ||
relativePhysicalPath: "/foo", | ||
fileKind: FileKinds.Legacy, | ||
additionalText: new TestAdditionalText(string.Empty), | ||
cssScope: null, | ||
context: new GeneratorExecutionContext()); | ||
|
||
// Act | ||
var extension = projectItem.Extension; | ||
|
||
// Assert | ||
Assert.Null(extension); | ||
} | ||
|
||
[Theory] | ||
[InlineData("/Home/Index.cshtml", ".cshtml")] | ||
[InlineData("/Home/Index.en-gb.cshtml", ".cshtml")] | ||
[InlineData("EditUser.razor", ".razor")] | ||
public void Extension_ReturnsFileExtension(string path, string expected) | ||
{ | ||
// Arrange | ||
var projectItem = new SourceGeneratorProjectItem( | ||
filePath: path, | ||
basePath: "/views", | ||
relativePhysicalPath: "/foo", | ||
fileKind: FileKinds.Legacy, | ||
additionalText: new TestAdditionalText(string.Empty), | ||
cssScope: null, | ||
context: new GeneratorExecutionContext()); | ||
|
||
// Act | ||
var extension = projectItem.Extension; | ||
|
||
// Assert | ||
Assert.Equal(expected, extension); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Home/Index.cshtml", "Index.cshtml")] | ||
[InlineData("/Accounts/Customers/Manage-en-us.razor", "Manage-en-us.razor")] | ||
public void FileName_ReturnsFileNameWithExtension(string path, string expected) | ||
{ | ||
// Arrange | ||
var projectItem = new SourceGeneratorProjectItem( | ||
filePath: path, | ||
basePath: "/", | ||
relativePhysicalPath: "/foo", | ||
fileKind: FileKinds.Legacy, | ||
additionalText: new TestAdditionalText(string.Empty), | ||
cssScope: null, | ||
context: new GeneratorExecutionContext()); | ||
|
||
// Act | ||
var fileName = projectItem.FileName; | ||
|
||
// Assert | ||
Assert.Equal(expected, fileName); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Home/Index", "Home/Index")] | ||
[InlineData("Home/Index.cshtml", "Home/Index")] | ||
[InlineData("/Accounts/Customers/Manage.en-us.razor", "/Accounts/Customers/Manage.en-us")] | ||
[InlineData("/Accounts/Customers/Manage-en-us.razor", "/Accounts/Customers/Manage-en-us")] | ||
public void PathWithoutExtension_ExcludesExtension(string path, string expected) | ||
{ | ||
// Arrange | ||
var projectItem = new SourceGeneratorProjectItem( | ||
filePath: path, | ||
basePath: "/", | ||
relativePhysicalPath: "/foo", | ||
fileKind: FileKinds.Legacy, | ||
additionalText: new TestAdditionalText(string.Empty), | ||
cssScope: null, | ||
context: new GeneratorExecutionContext()); | ||
|
||
// Act | ||
var fileName = projectItem.FilePathWithoutExtension; | ||
|
||
// Assert | ||
Assert.Equal(expected, fileName); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SDK things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep -- this is set to false in all test projects.
One thing I did differently here is remove the config to generate a NuGet package from the tests since it was causing some long path weirdness. The tests are still running on Helix so I don't think that was totally needed.