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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#nullable disable

using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -35,6 +37,78 @@ protected override string GetDirectoryPath(string testName)
return $"TestFiles/IntegrationTests/ComponentCodeGenerationTest/{testName}";
}

/// <summary>
/// Shadow of the base <c>CompileToAssembly</c> that, after compiling and verifying no
/// unexpected C# diagnostics, also asserts a <c>.builder.txt</c> baseline: the
/// component's render-tree-builder calls ordered by sequence number (see
/// <see cref="BuilderCallDumper"/>). Every test calling <c>CompileToAssembly(generated)</c>
/// picks up the check with no per-test change; pass
/// <paramref name="assertBuilderBaseline"/> <c>false</c> to opt a test out (for example
/// one whose generated render operations aren't deterministic across runs).
/// </summary>
/// <remarks>
/// The <c>.codegen.cs</c> / <c>.decl.codegen.cs</c> baselines pin the emitted C#
/// byte-for-byte, so they move under cosmetic reorganization -- which partial half a
/// member lands in, line-pragma layout, whitespace -- that doesn't change what the
/// component renders. The builder baseline is a sequence-ordered projection of just the
/// render operations: it stays put under that reorganization yet still trips on a real
/// change (a different element, a dropped attribute, a renumbered fragment).
/// </remarks>
protected CompileToAssemblyResult CompileToAssembly(
CompileToCSharpResult cSharpResult,
bool assertBuilderBaseline = true,
[CallerMemberName] string testName = "")
{
var result = RazorIntegrationTestBase.CompileToAssembly(cSharpResult);
if (assertBuilderBaseline)
{
AssertBuilderCallsMatchBaseline(cSharpResult, testName);
}

return result;
}

/// <summary>
/// Diagnostic-expecting overload: shadowed so call sites that pass expected
/// diagnostics resolve here. The builder-baseline check is skipped because
/// compile-error tests don't emit a meaningful render tree.
/// </summary>
protected new CompileToAssemblyResult CompileToAssembly(
CompileToCSharpResult cSharpResult,
params DiagnosticDescription[] expectedDiagnostics)
{
return RazorIntegrationTestBase.CompileToAssembly(cSharpResult, expectedDiagnostics);
}

private void AssertBuilderCallsMatchBaseline(CompileToCSharpResult cSharpResult, string testName)
{
var dump = BuilderCallDumper.Dump([cSharpResult.Code, cSharpResult.DeclCode]);

var baselineFilePath = GetBaselineFilePath(cSharpResult.CodeDocument, ".builder.txt", testName);
if (GenerateBaselines.ShouldGenerate)
{
if (dump.Length == 0)
{
// No render-tree-builder calls (e.g. a code-only component) -- nothing to pin.
return;
}

var baselineFullPath = Path.Combine(TestProjectRoot, baselineFilePath);
Directory.CreateDirectory(Path.GetDirectoryName(baselineFullPath));
File.WriteAllText(baselineFullPath, dump, new System.Text.UTF8Encoding(encoderShouldEmitUTF8Identifier: true));
return;
}

var baselineFile = TestFile.Create(baselineFilePath, GetType().Assembly);
if (!baselineFile.Exists())
{
// Component emits no builder calls, or its baseline predates this check.
return;
}

Assert.Equal(baselineFile.ReadAllText(), dump);
}

#region Basics

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddAttribute("Value", (object)( c ))
[0002] AddAttribute("ValueChanged", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => c = __value, c)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddAttribute("Value", (object)( c1 = c2 ))
[0002] AddAttribute("ValueChanged", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => c1 = c2 = __value, c1 = c2)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenComponent<global::Test.dynamic>()
[0001] AddComponentParameter(nameof(global::Test.dynamic. Value ), "Hello")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenComponent<global::Test.@int>()
[0001] AddComponentParameter(nameof(global::Test.@int. Value ), "Hello")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenComponent<global::MyComponent>()
[0001] AddComponentParameter(nameof(global::MyComponent. Value ), "Hello")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenElement("input")
[0001] AddAttribute("onclick", global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this, async (e) => await Task.Delay(10) ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenElement("input")
[0001] AddAttribute("onclick", global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this, OnClick ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenElement("input")
[0001] AddAttribute("onclick", global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this, async () => await Task.Delay(10) ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] OpenElement("input")
[0001] AddAttribute("onclick", global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this, OnClick ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[0000] AddContent(x)
[0001] AddContent(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue))))
[0003] AddComponentParameter(nameof(global::Test.MyComponent.ValueExpression), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Linq.Expressions.Expression<global::System.Func<global::System.Int32>>>(() => ParentValue))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.OnChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter("Value", ParentValue)
[0002] AddComponentParameter("OnChanged", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
[0003] AddComponentParameter(nameof(global::Test.MyComponent.ValueExpression), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Linq.Expressions.Expression<global::System.Func<global::System.Int32>>>(() => ParentValue))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.dynamic>()
[0001] AddComponentParameter(nameof(global::Test.dynamic. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.dynamic.ValueChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.@int>()
[0001] AddComponentParameter(nameof(global::Test.@int. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.@int.ValueChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::MyComponent>()
[0001] AddComponentParameter(nameof(global::MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)(__value => ParentValue = __value))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter("Value", ParentValue)
[0002] AddComponentParameter("ValueChanged", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)( __value => { ParentValue = __value; global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeSynchronousDelegate( Update ); }))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)( __value => { ParentValue = __value; global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeSynchronousDelegate( () => { } ); }))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredBindSetter(callback: __value => { ParentValue = __value; return global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate(callback: () => { } ); }, value: ParentValue), ParentValue))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredBindSetter(callback: __value => { ParentValue = __value; return global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate(callback: UpdateValue ); }, value: ParentValue), ParentValue))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[0000] OpenComponent<global::Test.OuterComponent>()
[0001] AddAttribute("ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment<global::Test.FilterContext>)((_) => (__builder2) => { __builder2.OpenComponent<global::Test.MyComponent>(2); __builder2.AddComponentParameter(3, nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue )); __builder2.AddComponentParameter(4, nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredBindSetter(callback: __value => { ParentValue = __value; return global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate(callback: OnAfter ); }, value: ParentValue), ParentValue)))); var (_, _) = (nameof(global::Test.MyComponent. Value ), 0); __builder2.CloseComponent(); } ))
[0002] OpenComponent<global::Test.MyComponent>()
[0003] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0004] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredBindSetter(callback: __value => { ParentValue = __value; return global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate(callback: OnAfter ); }, value: ParentValue), ParentValue))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Func<global::System.Int32, global::System.Threading.Tasks.Task>)(async __value => { ParentValue = __value; await global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate( Update ); }))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Func<global::System.Int32, global::System.Threading.Tasks.Task>)(async __value => { ParentValue = __value; await global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate( () => { return Task.CompletedTask; } ); }))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)( UpdateValue ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), (global::System.Action<global::System.Int32>)( value => ParentValue = value ))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[0000] OpenComponent<global::Test.MyComponent>()
[0001] AddComponentParameter(nameof(global::Test.MyComponent. Value ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>( ParentValue ))
[0002] AddComponentParameter(nameof(global::Test.MyComponent.ValueChanged), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<global::System.Int32>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.Int32>(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, UpdateValue , ParentValue))))
Loading