Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -12149,6 +12149,34 @@ public void LinePragma_Multiline()

#region RenderMode

[IntegrationTestFact]
public void RenderMode_Directive_WithTypeParam()
{
var generated = CompileToCSharp("""
@typeparam T
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
""");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[IntegrationTestFact]
public void RenderMode_Directive_WithTypeParam_First()
{
var generated = CompileToCSharp("""
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
@typeparam T
""");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[IntegrationTestFact]
public void RenderMode_Directive_FullyQualified()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Roslyn.Test.Utilities;
using Xunit;

Expand All @@ -12,6 +13,64 @@ public class ComponentRenderModeDirectiveIntegrationTests : RazorIntegrationTest
{
internal override RazorFileKind? FileKind => RazorFileKind.Component;

[Fact]
public void RenderMode_GenericComponent_CSharp11()
{
// Arrange & Act
var component = CompileToComponent("""
@typeparam T

@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
""", genericArity: 1);

// Assert
VerifyRenderModeAttribute(component, $$"""
file sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute
{
private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl => Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
;
public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl;
}
""");
}

[Fact]
public void RenderMode_GenericComponent_CSharp10_RazorLang9()
{
// Arrange & Act
var compilationResult = CompileToCSharp(DefaultFileName, cshtmlContent: """
@typeparam T

@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
""", configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_9_0 });

CompileToAssembly(compilationResult,
// (13,19): error CS0305: Using the generic type 'TestComponent<T>' requires 1 type arguments
// [global::Test.TestComponent.__PrivateComponentRenderModeAttribute]
Diagnostic(ErrorCode.ERR_BadArity, "TestComponent").WithArguments("Test.TestComponent<T>", "type", "1").WithLocation(13, 19));
}

[Fact]
public void RenderMode_GenericComponent_CSharp10()
{
var csharpParseOptions = CSharpParseOptions.WithLanguageVersion(CodeAnalysis.CSharp.LanguageVersion.CSharp10);

// Arrange & Act
var compilationResult = CompileToCSharp(DefaultFileName, csharpParseOptions: csharpParseOptions, cshtmlContent: """
@typeparam T

@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
""");

CompileToAssembly(compilationResult,
// (13,19): error CS0305: Using the generic type 'TestComponent<T>' requires 1 type arguments
// [global::Test.TestComponent.__PrivateComponentRenderModeAttribute]
Diagnostic(ErrorCode.ERR_BadArity, "TestComponent").WithArguments("Test.TestComponent<T>", "type", "1").WithLocation(13, 19),
// (31,70): error CS8936: Feature 'generic attributes' is not available in C# 10.0. Please use language version 11.0 or greater.
// private sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion10, "global::Microsoft.AspNetCore.Components.RenderModeAttribute").WithArguments("generic attributes", "11.0").WithLocation(31, 70));
}

[Fact]
public void RenderMode_With_Fully_Qualified_Type()
{
Expand Down Expand Up @@ -51,6 +110,27 @@ private sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.A
""");
}

[Fact]
public void RenderMode_With_Static_Usings_GenericComponent()
{
// Arrange & Act
var component = CompileToComponent("""
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@typeparam T
@rendermode InteractiveServer
""", genericArity: 1);

// Assert
VerifyRenderModeAttribute(component, """
file sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute
{
private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl => InteractiveServer
;
public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl;
}
""");
}

[Fact]
public void RenderMode_Missing_Value()
{
Expand Down Expand Up @@ -369,12 +449,70 @@ private sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.A
""");
}

[Fact]
public void RenderMode_With_FunctionCall_Generic()
{
// Arrange & Act
var component = CompileToComponent("""
@typeparam T
@rendermode @(TestComponent<object>.GetRenderMode())

@code
{
public static Microsoft.AspNetCore.Components.IComponentRenderMode GetRenderMode() => Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer;
}
""", genericArity: 1);

// Assert
VerifyRenderModeAttribute(component, $$"""
file sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute
{
private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl =>
#nullable restore
#line (2,15)-(2,52) "{{DefaultDocumentPath}}"
TestComponent<object>.GetRenderMode()

#line default
#line hidden
#nullable disable

;
public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl;
}
""");
}

[Fact]
public void RenderMode_With_FunctionCall_Generic_BadRef()
{
// Arrange & Act
var compilationResult = CompileToCSharp("""
@typeparam T
@rendermode @(TestComponent.GetRenderMode())

@code
{
public static Microsoft.AspNetCore.Components.IComponentRenderMode GetRenderMode() => Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer;
}
""");

// Assert
CompileToAssembly(compilationResult,
// x:\dir\subdir\Test\TestComponent.cshtml(2,15): error CS0305: Using the generic type 'TestComponent<T>' requires 1 type arguments
// TestComponent.GetRenderMode()
Diagnostic(ErrorCode.ERR_BadArity, "TestComponent").WithArguments("Test.TestComponent<T>", "type", "1").WithLocation(2, 15));
}

private static void VerifyRenderModeAttribute(INamedTypeSymbol component, string expected)
{
var attribute = Assert.Single(component.GetAttributes());
AssertEx.Equal("__PrivateComponentRenderModeAttribute", attribute.AttributeClass?.Name);
Assert.NotNull(attribute.AttributeClass);
var attributeClass = attribute.AttributeClass; ;
AssertEx.Equal("__PrivateComponentRenderModeAttribute", attributeClass.Name);

var attributeType = component.ContainingAssembly.GetTypeByMetadataName("Test.TestComponent+__PrivateComponentRenderModeAttribute");
var attributeType = attributeClass.IsFileLocal
? component.ContainingAssembly.GetTypeByMetadataName($"Test.{attributeClass.MetadataName}")
: component.ContainingAssembly.GetTypeByMetadataName("Test.TestComponent+__PrivateComponentRenderModeAttribute");
Assert.NotNull(attributeType);

expected = expected.NormalizeLineEndings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ public void TryParse90()
Assert.Same(RazorLanguageVersion.Version_9_0, version);
}

[Fact]
public void TryParse100()
{
// Arrange
var value = "10.0";

// Act
var result = RazorLanguageVersion.TryParse(value, out var version);

// Assert
Assert.True(result);
Assert.Same(RazorLanguageVersion.Version_10_0, version);
}

[Fact]
public void TryParse110()
{
// Arrange
var value = "11.0";

// Act
var result = RazorLanguageVersion.TryParse(value, out var version);

// Assert
Assert.True(result);
Assert.Same(RazorLanguageVersion.Version_11_0, version);
}

[Fact]
public void TryParseLatest()
{
Expand Down Expand Up @@ -195,10 +223,10 @@ public void TryParseExperimental()
}

[Fact]
public void LatestPointsToNewestVersion()
public void PreviewPointsToNewestVersion()
{
// Arrange
var v = RazorLanguageVersion.Parse("latest");
var v = RazorLanguageVersion.Parse("preview");
var versions = typeof(RazorLanguageVersion).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(f => f.Name.StartsWith("Version_", StringComparison.Ordinal))
.Select(f => f.GetValue(obj: null))
Expand All @@ -208,7 +236,7 @@ public void LatestPointsToNewestVersion()
Assert.NotEmpty(versions);
foreach (var version in versions)
{
Assert.True(version.CompareTo(v) <= 0, $"RazorLanguageVersion {version} has a higher version than RazorLanguageVersion.Latest");
Assert.True(version.CompareTo(v) <= 0, $"RazorLanguageVersion {version} has a higher version than RazorLanguageVersion.Preview");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#line default
#line hidden
[__PrivateComponentRenderModeAttribute]
#nullable restore
public partial class TestComponent<
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
T

#line default
#line hidden
#nullable disable
> : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
((global::System.Action)(() => {
}
))();
((global::System.Action)(() => {
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
global::System.Object __typeHelper = nameof(Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer);

#line default
#line hidden
#nullable disable
}
))();
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
}
file sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl =>
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer

#line default
#line hidden
#nullable disable
;
public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [20] ) - global::System
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [25] ) - global::System.Linq
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
CSharpCode -
IntermediateToken - - CSharp - [__PrivateComponentRenderModeAttribute]
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - - T
DesignTimeDirective -
DirectiveToken - (11:0,11 [1] x:\dir\subdir\Test\TestComponent.cshtml) - T
DirectiveToken - (26:1,12 [64] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
ClassDeclaration - - file sealed - __PrivateComponentRenderModeAttribute - global::Microsoft.AspNetCore.Components.RenderModeAttribute -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
CSharpCode -
IntermediateToken - - CSharp - private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl =>
CSharpCode - (26:1,12 [64] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
IntermediateToken - - CSharp - ;
CSharpCode -
IntermediateToken - - CSharp - public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Source Location: (11:0,11 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|T|
Generated Location: (507:17,0 [1] )
|T|

Source Location: (26:1,12 [64] x:\dir\subdir\Test\TestComponent.cshtml)
|Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer|
Generated Location: (971:33,44 [64] )
|Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer|

Loading