Skip to content
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 5 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGenerators", "SourceG
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.Razor.SourceGenerators", "src\RazorSdk\SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.csproj", "{56C34654-DE8F-4F14-B2F8-6C37285B786E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.AspNetCoreDeltaApplier", "src\BuiltInTools\AspNetCoreDeltaApplier\Microsoft.Extensions.AspNetCoreDeltaApplier.csproj", "{1BBFA19C-03F0-4D27-9D0D-0F8172642107}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.AspNetCoreDeltaApplier", "src\BuiltInTools\AspNetCoreDeltaApplier\Microsoft.Extensions.AspNetCoreDeltaApplier.csproj", "{1BBFA19C-03F0-4D27-9D0D-0F8172642107}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.DotNet.NativeWrapper", "src\Resolvers\Microsoft.DotNet.NativeWrapper\Microsoft.DotNet.NativeWrapper.csproj", "{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.NativeWrapper", "src\Resolvers\Microsoft.DotNet.NativeWrapper\Microsoft.DotNet.NativeWrapper.csproj", "{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.Razor.SourceGenerators.Tests", "src\Tests\Microsoft.NET.Sdk.Razor.SourceGenerators.Tests\Microsoft.NET.Sdk.Razor.SourceGenerators.Tests.csproj", "{A71FC21D-D90A-49B5-9B5A-AD4776287B55}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -609,6 +611,10 @@ Global
{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0}.Release|Any CPU.Build.0 = Release|Any CPU
{A71FC21D-D90A-49B5-9B5A-AD4776287B55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A71FC21D-D90A-49B5-9B5A-AD4776287B55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A71FC21D-D90A-49B5-9B5A-AD4776287B55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A71FC21D-D90A-49B5-9B5A-AD4776287B55}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -721,6 +727,7 @@ Global
{56C34654-DE8F-4F14-B2F8-6C37285B786E} = {C2B15A41-A9C0-456A-A9FF-649E9237D850}
{1BBFA19C-03F0-4D27-9D0D-0F8172642107} = {71A9F549-0EB6-41F9-BC16-4A6C5007FC91}
{E97E9E7F-11B4-42F7-8B55-D0451F5E82A0} = {8F22FBD6-BDC8-431E-8402-B7460D3A9724}
{A71FC21D-D90A-49B5-9B5A-AD4776287B55} = {580D1AE7-AA8F-4912-8B76-105594E00B3B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FB8F26CE-4DE6-433F-B32A-79183020BBD6}
Expand Down
6 changes: 6 additions & 0 deletions src/RazorSdk/SourceGenerators/Properties/AssemblyInfo.cs
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")]
26 changes: 0 additions & 26 deletions src/RazorSdk/SourceGenerators/SourceTextRazorSourceDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,5 @@ public override void CopyTo(int sourceIndex, char[] destination, int destination
public override byte[] GetChecksum() => _sourceText.GetChecksum().ToArray();

public override string GetChecksumAlgorithm() => _sourceText.ChecksumAlgorithm.ToString().ToUpperInvariant();

private 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);
}
}
}
}
36 changes: 36 additions & 0 deletions src/RazorSdk/SourceGenerators/SourceTextSourceLineCollection.cs
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDK things?

Copy link
Member Author

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.

</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>
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);
}
}
}
Loading