Skip to content

Commit

Permalink
Add nameof test
Browse files Browse the repository at this point in the history
- Adds a test the demonstrates what happens if you declare a function called nameof in a component
  • Loading branch information
chsienki committed Jul 9, 2024
1 parent f60e10b commit 94d7580
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,51 @@ public class MyComponent : ComponentBase
CompileToAssembly(generated);
}

[IntegrationTestFact]
public void BindToComponent_SpecifiesValue_WithMatchingProperties_WithNameof()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;
namespace Test
{
public class MyComponent : ComponentBase
{
[Parameter]
public int Value { get; set; }
[Parameter]
public Action<int> ValueChanged { get; set; }
}
}"));

// Act
var generated = CompileToCSharp(@"
<MyComponent @bind-Value=""ParentValue"" />
@code {
public int ParentValue { get; set; } = 42;
public string nameof(string s) => string.Empty;
}");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated, [
// (21,55): error CS0120: An object reference is required for the non-static field, method, or property 'MyComponent.Value'
// __builder.AddComponentParameter(1, nameof(global::Test.MyComponent.
Diagnostic(ErrorCode.ERR_ObjectRequired, @"global::Test.MyComponent.
#nullable restore
#line (1,20)-(1,25) ""x:\dir\subdir\Test\TestComponent.cshtml""
Value").WithArguments("Test.MyComponent.Value").WithLocation(21, 55),
// (38,55): error CS0120: An object reference is required for the non-static field, method, or property 'MyComponent.ValueChanged'
// __builder.AddComponentParameter(2, nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<System.Int32>)(__value => ParentValue = __value));
Diagnostic(ErrorCode.ERR_ObjectRequired, "global::Test.MyComponent.ValueChanged").WithArguments("Test.MyComponent.ValueChanged").WithLocation(38, 55)
]);
}

[IntegrationTestFact]
public void BindToComponent_WithStringAttribute_DoesNotUseStringSyntax()
{
Expand Down
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
#nullable restore
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#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)
{
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
ParentValue

#line default
#line hidden
#nullable disable
);
__o = new global::System.Action<System.Int32>(
__value => ParentValue = __value);
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#pragma warning disable BL0005
((global::Test.MyComponent)default).
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Value

#line default
#line hidden
#nullable disable
= default;
#pragma warning restore BL0005
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.MyComponent);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"

public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
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
Component - (0:0,0 [41] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentValue
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - __value => ParentValue = __value
HtmlContent - (41:0,41 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (41:0,41 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public int ParentValue { get; set; } = 42;\n\n public string nameof(string s) => string.Empty;\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Source Location: (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|ParentValue|
Generated Location: (1135:29,26 [11] )
|ParentValue|

Source Location: (19:0,19 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|Value|
Generated Location: (1668:44,19 [5] )
|Value|

Source Location: (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml)
|
public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;
|
Generated Location: (2081:62,7 [105] )
|
public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <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
#nullable restore
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Test.MyComponent>(0);
__builder.AddComponentParameter(1, nameof(global::Test.MyComponent.
#nullable restore
#line (1,20)-(1,25) "x:\dir\subdir\Test\TestComponent.cshtml"
Value

#line default
#line hidden
#nullable disable
), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line (1,27)-(1,38) "x:\dir\subdir\Test\TestComponent.cshtml"
ParentValue

#line default
#line hidden
#nullable disable
));
__builder.AddComponentParameter(2, nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<System.Int32>)(__value => ParentValue = __value));
__builder.CloseComponent();
}
#pragma warning restore 1998
#nullable restore
#line (2,8)-(6,1) "x:\dir\subdir\Test\TestComponent.cshtml"

public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;

#line default
#line hidden
#nullable disable

}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [22] ) - global::System
UsingDirective - (26:2,1 [42] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [27] ) - global::System.Linq
UsingDirective - (97:4,1 [38] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [47] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [41] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentValue
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - __value => ParentValue = __value
CSharpCode - (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public int ParentValue { get; set; } = 42;\n\n public string nameof(string s) => string.Empty;\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Source Location: (19:0,19 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|Value|
Generated Location: (884:23,0 [5] )
|Value|

Source Location: (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|ParentValue|
Generated Location: (1144:31,0 [11] )
|ParentValue|

Source Location: (50:1,7 [105] x:\dir\subdir\Test\TestComponent.cshtml)
|
public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;
|
Generated Location: (1563:43,0 [105] )
|
public int ParentValue { get; set; } = 42;

public string nameof(string s) => string.Empty;
|

0 comments on commit 94d7580

Please sign in to comment.