From ebee3cbc1554ec28ece457abacb99445a7bd4a23 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 4 Apr 2024 10:56:59 -0700 Subject: [PATCH 1/7] Unify GENERATE_BASELINE handling to a single location for all tests --- .../CodeGenerationIntegrationTest.cs | 2 +- .../CodeGenerationIntegrationTest.cs | 2 +- .../InstrumentationPassIntegrationTest.cs | 2 +- .../CodeGenerationIntegrationTest.cs | 2 +- .../CodeGenerationIntegrationTest.cs | 2 +- .../ComponentCodeGenerationTestBase.cs | 44 +++++++++---------- .../ExtensibleDirectiveTest.cs | 2 +- .../Semantic/SemanticTokensTest.cs | 10 +---- .../Language/Legacy/ToolingParserTestBase.cs | 8 +--- .../GenerateBaselines.cs | 20 +++++++++ .../IntegrationTests/IntegrationTestBase.cs | 30 +++---------- .../RazorBaselineIntegrationTestBase.cs | 23 ++-------- .../Language/Legacy/ParserTestBase.cs | 8 +--- 13 files changed, 61 insertions(+), 94 deletions(-) create mode 100644 src/Shared/Microsoft.AspNetCore.Razor.Test.Common/GenerateBaselines.cs diff --git a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs index 298cb900381..045fc2ee3af 100644 --- a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -16,7 +16,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase private static readonly CSharpCompilation DefaultBaseCompilation = MvcShim.BaseCompilation.WithAssemblyName("AppCode"); public CodeGenerationIntegrationTest() - : base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X") + : base(layer: TestProject.Layer.Compiler, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X") { Configuration = new(RazorLanguageVersion.Version_1_1, "MVC-1.1", Extensions: []); } diff --git a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs index 0a73859b82a..bc3aa19c9c1 100644 --- a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -18,7 +18,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase private static readonly CSharpCompilation DefaultBaseCompilation = MvcShim.BaseCompilation.WithAssemblyName("AppCode"); public CodeGenerationIntegrationTest() - : base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X") + : base(layer: TestProject.Layer.Compiler, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X") { Configuration = new(RazorLanguageVersion.Version_2_0, "MVC-2.1", Extensions: []); } diff --git a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/InstrumentationPassIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/InstrumentationPassIntegrationTest.cs index 612436e2661..5d5111e1d30 100644 --- a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/InstrumentationPassIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/InstrumentationPassIntegrationTest.cs @@ -19,7 +19,7 @@ public class InstrumentationPassIntegrationTest : IntegrationTestBase private static readonly CSharpCompilation DefaultBaseCompilation = MvcShim.BaseCompilation.WithAssemblyName("AppCode"); public InstrumentationPassIntegrationTest() - : base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X") + : base(layer: TestProject.Layer.Compiler, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X") { Configuration = new(RazorLanguageVersion.Version_2_0, "MVC-2.1", Extensions: []); } diff --git a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs index 7adf529b0f4..af08fa81b31 100644 --- a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -18,7 +18,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase private static readonly CSharpCompilation DefaultBaseCompilation = MvcShim.BaseCompilation.WithAssemblyName("AppCode"); public CodeGenerationIntegrationTest() - : base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions") + : base(layer: TestProject.Layer.Compiler, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions") { Configuration = new(RazorLanguageVersion.Latest, "MVC-3.0", Extensions: []); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs index 7e13fc1b3b1..10a38aed0b9 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests; public class CodeGenerationIntegrationTest(bool designTime = false) - : IntegrationTestBase(layer: TestProject.Layer.Compiler, generateBaselines: null) + : IntegrationTestBase(layer: TestProject.Layer.Compiler) { [IntegrationTestFact] public void SingleLineControlFlowStatements() => RunTest(); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 68b9c85fa53..4a655d12a46 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests; public class ComponentCodeGenerationTestBase(bool designTime = false) - : RazorBaselineIntegrationTestBase(layer: TestProject.Layer.Compiler, generateBaselines: null) + : RazorBaselineIntegrationTestBase(layer: TestProject.Layer.Compiler) { private RazorConfiguration _configuration; @@ -1439,7 +1439,7 @@ public void AddAttribute_ImplicitStringConversion_Bind() using Microsoft.AspNetCore.Components; namespace Test; - + public class MyClass { public static implicit operator string(MyClass c) => throw null!; @@ -1452,16 +1452,16 @@ public class MyComponent : ComponentBase [Parameter] public EventCallback> MyParameterChanged { get; set; } - + [Parameter] public bool BoolParameter { get; set; } - + [Parameter] public string StringParameter { get; set; } = null!; - + [Parameter] public System.Delegate DelegateParameter { get; set; } = null!; - + [Parameter] public object ObjectParameter { get; set; } = null!; } @@ -1493,7 +1493,7 @@ public void AddAttribute_ImplicitStringConversion_CustomEvent() using Microsoft.AspNetCore.Components; namespace Test; - + public class MyClass { public static implicit operator string(MyClass c) => throw null!; @@ -1506,16 +1506,16 @@ public class MyComponent : ComponentBase [Parameter] public EventCallback MyEvent { get; set; } - + [Parameter] public bool BoolParameter { get; set; } - + [Parameter] public string StringParameter { get; set; } = null!; - + [Parameter] public System.Delegate DelegateParameter { get; set; } = null!; - + [Parameter] public object ObjectParameter { get; set; } = null!; } @@ -1548,7 +1548,7 @@ public void AddAttribute_ImplicitStringConversion_BindUnknown() using Microsoft.AspNetCore.Components; namespace Test; - + public class MyClass { public static implicit operator string(MyClass c) => throw null!; @@ -1581,7 +1581,7 @@ public void AddAttribute_ImplicitStringConversion_BindUnknown_Assignment() using Microsoft.AspNetCore.Components; namespace Test; - + public class MyClass { public static implicit operator string(MyClass c) => throw null!; @@ -1615,7 +1615,7 @@ public void AddAttribute_ImplicitBooleanConversion() using Microsoft.AspNetCore.Components; namespace Test; - + public class MyClass { public static implicit operator bool(MyClass c) => throw null!; @@ -1625,7 +1625,7 @@ public class MyComponent : ComponentBase { [Parameter] public MyClass MyParameter { get; set; } - + [Parameter] public bool BoolParameter { get; set; } } @@ -1651,7 +1651,7 @@ public void AddComponentParameter() @typeparam T - + @code { [Parameter] public T Param { get; set; } @@ -4715,7 +4715,7 @@ @using static Test2.SomeComponent AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated, throwOnFailure: false); } - + [IntegrationTestFact] public void Component_WithMultipleUsingDirectives() { @@ -7032,7 +7032,7 @@ public class Grid : ComponentBase public partial class GridColumn : ComponentBase { } - + public class X { } """)); @@ -8153,7 +8153,7 @@ public void GenericComponent_TypeParameterOrdering() // Arrange AdditionalSyntaxTrees.Add(Parse(""" using Microsoft.AspNetCore.Components; - + namespace Test; public interface IInterfaceConstraint { } @@ -10654,7 +10654,7 @@ @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server public void RenderMode_Attribute_With_SimpleIdentifier() { var generated = CompileToCSharp($""" - <{ComponentName} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> + <{ComponentName} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> """, throwOnFailure: true); // Assert @@ -10674,7 +10674,7 @@ class MyRenderMode : Microsoft.AspNetCore.Components.IComponentRenderMode { public string Extra {get;set;} } - } + } """, throwOnFailure: true); // Assert @@ -10694,7 +10694,7 @@ public void RenderMode_Attribute_With_Existing_Attributes() [Parameter]public string P1 {get; set;} [Parameter]public string P2 {get; set;} - } + } """, throwOnFailure: true); // Assert diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ExtensibleDirectiveTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ExtensibleDirectiveTest.cs index 02349af252b..fe8dfa77e7f 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ExtensibleDirectiveTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ExtensibleDirectiveTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests; public class ExtensibleDirectiveTest : IntegrationTestBase { public ExtensibleDirectiveTest() - : base(layer: TestProject.Layer.Compiler, generateBaselines: null) + : base(layer: TestProject.Layer.Compiler) { } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs index bef1fa86c72..3b58fe8d8fa 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs @@ -58,12 +58,6 @@ public partial class SemanticTokensTest(ITestOutputHelper testOutput) : TagHelpe private static Regex MyRegex() => new Regex("\r\n|\r|\n"); #endif -#if GENERATE_BASELINES - private bool GenerateBaselines { get; set; } = true; -#else - private bool GenerateBaselines { get; set; } = false; -#endif - [Theory] [CombinatorialData] public async Task GetSemanticTokens_CSharp_RazorIfNotReady(bool precise) @@ -609,7 +603,7 @@ public async Task GetSemanticTokens_Razor_MultiLineCommentWithBlankLines(bool pr @* kdl skd - + sdfasdfasdf slf*@ """; @@ -1087,7 +1081,7 @@ private void AssertSemanticTokensMatchesBaseline(SourceText sourceText, int[]? a var actualFileContents = GetFileRepresentationOfTokens(sourceText, actualSemanticTokens); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { GenerateSemanticBaseline(actualFileContents, baselineFileName); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/Legacy/ToolingParserTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/Legacy/ToolingParserTestBase.cs index a200d0eb007..64c89e2de7a 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/Legacy/ToolingParserTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/Legacy/ToolingParserTestBase.cs @@ -38,12 +38,6 @@ protected ToolingParserTestBase(ITestOutputHelper testOutput) /// protected bool FixupSpans { get; set; } -#if GENERATE_BASELINES - protected bool GenerateBaselines { get; set; } = true; -#else - protected bool GenerateBaselines { get; set; } = false; -#endif - protected string TestProjectRoot { get; } protected int BaselineTestCount { get; set; } @@ -74,7 +68,7 @@ internal virtual void AssertSyntaxTreeNodeMatchesBaseline(RazorSyntaxTree syntax var baselineTagHelperSpansFileName = Path.ChangeExtension(fileName, ".tspans.txt"); BaselineTestCount++; - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { // Write syntax tree baseline var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/GenerateBaselines.cs b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/GenerateBaselines.cs new file mode 100644 index 00000000000..62d5828a7ba --- /dev/null +++ b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/GenerateBaselines.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +internal class GenerateBaselines +{ +#if GENERATE_BASELINES + internal static readonly bool ShouldGenerate = true; +#else + internal static readonly bool ShouldGenerate = false; +#endif + + // This is to prevent you from accidentally checking in with GenerateBaselines = true + [Fact] + public void GenerateBaselinesMustBeFalse() + { + Assert.False(ShouldGenerate, "GenerateBaselines should be set back to false before you check in!"); + } +} diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs index e7d74be94e6..45b3309aae6 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs +++ b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs @@ -49,14 +49,9 @@ static IntegrationTestBase() new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); } - protected IntegrationTestBase(TestProject.Layer layer, bool? generateBaselines = null, string? projectDirectoryHint = null) + protected IntegrationTestBase(TestProject.Layer layer, string? projectDirectoryHint = null) { TestProjectRoot = projectDirectoryHint == null ? TestProject.GetProjectDirectory(GetType(), layer) : TestProject.GetProjectDirectory(projectDirectoryHint, layer); - - if (generateBaselines.HasValue) - { - GenerateBaselines = generateBaselines.Value; - } } /// @@ -98,12 +93,6 @@ protected IntegrationTestBase(TestProject.Layer layer, bool? generateBaselines = /// protected virtual string LineEnding { get; } = "\r\n"; -#if GENERATE_BASELINES - protected bool GenerateBaselines { get; } = true; -#else - protected bool GenerateBaselines { get; } = false; -#endif - protected string TestProjectRoot { get; } public virtual string GetTestFileName(string? testName) @@ -338,7 +327,7 @@ protected void AssertDocumentNodeMatchesBaseline(DocumentIntermediateNode docume { var baselineFileName = Path.ChangeExtension(GetTestFileName(testName), ".ir.txt"); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); File.WriteAllText(baselineFullPath, IntermediateNodeSerializer.Serialize(document), _baselineEncoding); @@ -359,7 +348,7 @@ internal void AssertHtmlDocumentMatchesBaseline(RazorHtmlDocument htmlDocument, { var baselineFileName = Path.ChangeExtension(GetTestFileName(testName), ".codegen.html"); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); File.WriteAllText(baselineFullPath, htmlDocument.GeneratedCode, _baselineEncoding); @@ -385,7 +374,7 @@ protected void AssertCSharpDocumentMatchesBaseline(RazorCSharpDocument cSharpDoc var baselineFileName = Path.ChangeExtension(fileName, ".codegen.cs"); var baselineDiagnosticsFileName = Path.ChangeExtension(fileName, ".diagnostics.txt"); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); File.WriteAllText(baselineFullPath, cSharpDocument.GeneratedCode, _baselineEncoding); @@ -435,7 +424,7 @@ protected void AssertSourceMappingsMatchBaseline(RazorCodeDocument codeDocument, var baselineFileName = Path.ChangeExtension(GetTestFileName(testName), ".mappings.txt"); var serializedMappings = SourceMappingsSerializer.Serialize(csharpDocument, codeDocument.Source); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); File.WriteAllText(baselineFullPath, serializedMappings, _baselineEncoding); @@ -533,7 +522,7 @@ protected void AssertHtmlSourceMappingsMatchBaseline(RazorCodeDocument codeDocum var baselineFileName = Path.ChangeExtension(GetTestFileName(testName), ".html.mappings.txt"); var serializedMappings = SourceMappingsSerializer.Serialize(htmlDocument, codeDocument.Source); - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); File.WriteAllText(baselineFullPath, serializedMappings, _baselineEncoding); @@ -695,13 +684,6 @@ private static string NormalizeNewLines(string content, string lineEnding) return Regex.Replace(content, "(? protected bool FixupSpans { get; set; } -#if GENERATE_BASELINES - protected bool GenerateBaselines { get; set; } = true; -#else - protected bool GenerateBaselines { get; set; } = false; -#endif - protected string TestProjectRoot { get; } // Used by the test framework to set the 'base' name for test files. @@ -86,7 +80,7 @@ internal virtual void AssertSyntaxTreeNodeMatchesBaseline(RazorSyntaxTree syntax var baselineTagHelperSpansFileName = Path.ChangeExtension(fileName, ".tspans.txt"); BaselineTestCount++; - if (GenerateBaselines) + if (GenerateBaselines.ShouldGenerate) { // Write syntax tree baseline var baselineFullPath = Path.Combine(TestProjectRoot, baselineFileName); From 242e871a5689c76db016e7a5cb61605f96bc4180 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Fri, 5 Apr 2024 16:21:00 -0700 Subject: [PATCH 2/7] Unify test diagnostic handling. We will now always verify the diagnostics reported by the C# compiler. At some point, we should consider doing the same for Razor diagnostics, but that can be done as a future step. --- .../ComponentBindIntegrationTest.cs | 12 +- .../ComponentChildContentIntegrationTest.cs | 23 +- .../ComponentCodeGenerationTestBase.cs | 614 +++++++++++------- .../ComponentDeclarationIntegrationTest.cs | 14 +- ...ComponentDiagnosticRazorIntegrationTest.cs | 24 +- .../ComponentDiscoveryIntegrationTest.cs | 10 +- .../ComponentFilePathIntegrationTest.cs | 4 +- .../ComponentGenericTypeIntegrationTest.cs | 4 +- .../ComponentImportsIntegrationTest.cs | 36 +- ...nentRenderModeAttributeIntegrationTests.cs | 12 +- ...nentRenderModeDirectiveIntegrationTests.cs | 44 +- .../ComponentTemplateIntegrationTest.cs | 18 +- .../IntegrationTests/ComponentTypingTest.cs | 37 +- .../TestComponent.codegen.cs | 0 .../TestComponent.ir.txt | 0 .../TestComponent.mappings.txt | 0 .../TestComponent.codegen.cs | 94 +++ .../TestComponent.ir.txt | 66 ++ .../TestComponent.mappings.txt | 61 ++ .../TestComponent.codegen.cs | 0 .../TestComponent.ir.txt | 0 .../TestComponent.mappings.txt | 0 .../TestComponent.codegen.cs | 108 +++ .../TestComponent.ir.txt | 49 ++ .../TestComponent.mappings.txt | 61 ++ .../ErrorCode.cs | 9 + .../RazorIntegrationTestBase.cs | 63 +- 27 files changed, 968 insertions(+), 395 deletions(-) rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.codegen.cs (100%) rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.ir.txt (100%) rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.mappings.txt (100%) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.codegen.cs create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.ir.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.mappings.txt rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.codegen.cs (100%) rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.ir.txt (100%) rename src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/{CanProduceLinePragmasForComponentWithRenderFragment => CanProduceLinePragmasForComponentWithRenderFragment_01}/TestComponent.mappings.txt (100%) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.codegen.cs create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.ir.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/CanProduceLinePragmasForComponentWithRenderFragment_02/TestComponent.mappings.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentBindIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentBindIntegrationTest.cs index cd63ebec355..9e7d2f1f4b0 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentBindIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentBindIntegrationTest.cs @@ -3,9 +3,7 @@ #nullable disable -using System; using System.Globalization; -using Microsoft.AspNetCore.Components; using Xunit; namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests; @@ -41,7 +39,7 @@ public static class BindAttributes }"); // Assert - var diagnostic = Assert.Single(result.Diagnostics); + var diagnostic = Assert.Single(result.RazorDiagnostics); Assert.Equal("RZ9989", diagnostic.Id); Assert.Equal(""" The attribute '@bind-value' was matched by multiple bind attributes. Duplicates: @@ -62,7 +60,7 @@ public void BindFallback_InvalidSyntax_TooManyParts() }"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9991", diagnostic.Id); } @@ -77,7 +75,7 @@ public void BindFallback_InvalidSyntax_TrailingDash() }"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9991", diagnostic.Id); } @@ -91,11 +89,11 @@ @using Microsoft.AspNetCore.Components.Web @functions { public string page { get; set; } = ""text""; -}", throwOnFailure: false); +}"); // Assert Assert.Collection( - generated.Diagnostics, + generated.RazorDiagnostics, d => Assert.Equal("RZ2005", d.Id), d => Assert.Equal("RZ1011", d.Id)); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentChildContentIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentChildContentIntegrationTest.cs index 80727f454df..99660d4ebac 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentChildContentIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentChildContentIntegrationTest.cs @@ -4,7 +4,6 @@ #nullable disable using System.Globalization; -using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Razor.Language.Components; using Microsoft.CodeAnalysis.CSharp; using Xunit; @@ -70,7 +69,7 @@ Some Content "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentSetByAttributeAndBody.Id, diagnostic.Id); } @@ -90,7 +89,7 @@ Some Content "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentSetByAttributeAndBody.Id, diagnostic.Id); } @@ -109,7 +108,7 @@ public void ChildContent_ExplicitChildContent_UnrecogizedContent_ProducesDiagnos "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentMixedWithExplicitChildContent.Id, diagnostic.Id); Assert.Equal(""" Unrecognized child content inside component 'RenderChildContent'. The component 'RenderChildContent' accepts child content through the following top-level items: 'ChildContent'. @@ -135,7 +134,7 @@ public void ChildContent_ExplicitChildContent_UnrecogizedElement_ProducesDiagnos // Assert Assert.Collection( - generated.Diagnostics, + generated.RazorDiagnostics, d => Assert.Equal("RZ10012", d.Id), d => Assert.Equal("RZ9996", d.Id)); } @@ -156,7 +155,7 @@ public void ChildContent_ExplicitChildContent_StartsWithCharThatIsOtherLetterCat // Assert Assert.Collection( - generated.Diagnostics, + generated.RazorDiagnostics, d => Assert.Equal("RZ10012", d.Id), d => Assert.Equal("RZ9996", d.Id)); } @@ -177,7 +176,7 @@ public void ChildContent_ExplicitChildContent_StartsWithCharThatIsOtherLetterCat // Assert Assert.Collection( - generated.Diagnostics, + generated.RazorDiagnostics, d => Assert.Equal("RZ9996", d.Id)); } @@ -195,7 +194,7 @@ public void ChildContent_ExplicitChildContent_UnrecogizedAttribute_ProducesDiagn "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentHasInvalidAttribute.Id, diagnostic.Id); } @@ -213,7 +212,7 @@ public void ChildContent_ExplicitChildContent_InvalidParameterName_ProducesDiagn "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentHasInvalidParameter.Id, diagnostic.Id); } @@ -235,7 +234,7 @@ public void ChildContent_ExplicitChildContent_RepeatedParameterName_GeneratesDia "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentRepeatedParameterName.Id, diagnostic.Id); Assert.Equal(""" The child content element 'ChildContent' of component 'RenderChildContentString' uses the same parameter name ('context') as enclosing child content element 'ChildContent' of component 'RenderChildContentString'. Specify the parameter name like: ' to resolve the ambiguity @@ -255,7 +254,7 @@ public void ChildContent_ContextParameterNameOnComponent_Invalid_ProducesDiagnos "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentHasInvalidParameterOnComponent.Id, diagnostic.Id); Assert.Equal( "Invalid parameter name. The parameter name attribute 'Context' on component 'RenderChildContentString' can only include literal text.", @@ -276,7 +275,7 @@ public void ChildContent_ExplicitChildContent_ContainsDirectiveAttribute_Produce "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.ChildContentHasInvalidAttribute.Id, diagnostic.Id); Assert.Equal( "Unrecognized attribute '@key' on child content element 'ChildContent'.", diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 4a655d12a46..861a38cbe47 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -521,7 +521,7 @@ @typeparam TItem CompileToAssembly(generated); AdditionalSyntaxTrees.Add(Parse(generated.CodeDocument.GetCSharpDocument().GeneratedCode)); - var useGenerated = CompileToCSharp("UseTestComponent.cshtml", @" + var useGenerated = CompileToCSharp("UseTestComponent.cshtml", cshtmlContent: @" @using Test

@context[0].description

@@ -599,7 +599,7 @@ @typeparam TItem2 CompileToAssembly(generated); AdditionalSyntaxTrees.Add(Parse(generated.CodeDocument.GetCSharpDocument().GeneratedCode)); - var useGenerated = CompileToCSharp("UseTestComponent.cshtml", @" + var useGenerated = CompileToCSharp("UseTestComponent.cshtml", cshtmlContent: @" @using Test

@context

@@ -742,7 +742,7 @@ public class Tag : ITag CompileToAssembly(generated); AdditionalSyntaxTrees.Add(Parse(generated.CodeDocument.GetCSharpDocument().GeneratedCode)); - var useGenerated = CompileToCSharp("UseTestComponent.cshtml", @" + var useGenerated = CompileToCSharp("UseTestComponent.cshtml", cshtmlContent: @" @using Test

@context

@@ -819,7 +819,7 @@ public class Tag : ITag CompileToAssembly(generated); AdditionalSyntaxTrees.Add(Parse(generated.CodeDocument.GetCSharpDocument().GeneratedCode)); - var useGenerated = CompileToCSharp("UseTestComponent.cshtml", @" + var useGenerated = CompileToCSharp("UseTestComponent.cshtml", cshtmlContent: @" @using Test

@context

@@ -913,11 +913,10 @@ public class CoolnessMeter : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var assembly = CompileToAssembly(generated, throwOnFailure: false); - // This has some errors - Assert.Collection( - assembly.Diagnostics.OrderBy(d => d.Id), - d => Assert.Equal("CS1503", d.Id)); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,28): error CS1503: Argument 1: cannot convert from 'string' to 'int' + // "very-cool" + Diagnostic(ErrorCode.ERR_BadArgType, @"""very-cool""").WithArguments("1", "string", "int").WithLocation(1, 28)); } [IntegrationTestFact] @@ -1109,9 +1108,9 @@ public class ComponentWithEditorRequiredParameters : ComponentBase "); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated); - var diagnostics = Assert.Single(generated.Diagnostics); + var diagnostics = Assert.Single(generated.RazorDiagnostics); Assert.Equal(RazorDiagnosticSeverity.Warning, diagnostics.Severity); Assert.Equal("RZ2012", diagnostics.Id); } @@ -1139,7 +1138,7 @@ public class ComponentWithEditorRequiredParameters : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -1165,7 +1164,7 @@ public class ComponentWithEditorRequiredParameters : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/7395")] @@ -1196,7 +1195,7 @@ public class ComponentWithEditorRequiredParameters : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -1220,9 +1219,9 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase "); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated); - var diagnostics = Assert.Single(generated.Diagnostics); + var diagnostics = Assert.Single(generated.RazorDiagnostics); Assert.Equal(RazorDiagnosticSeverity.Warning, diagnostics.Severity); Assert.Equal("RZ2012", diagnostics.Id); } @@ -1253,7 +1252,7 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -1280,7 +1279,7 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -1311,7 +1310,7 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -1340,9 +1339,9 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase "); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated); - var diagnostics = Assert.Single(generated.Diagnostics); + var diagnostics = Assert.Single(generated.RazorDiagnostics); Assert.Equal(RazorDiagnosticSeverity.Warning, diagnostics.Severity); Assert.Equal("RZ2012", diagnostics.Id); } @@ -1376,7 +1375,7 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); CompileToAssembly(generated); - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")] @@ -1788,12 +1787,13 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var assembly = CompileToAssembly(generated, throwOnFailure: false); - // This has some errors - Assert.Collection( - assembly.Diagnostics.OrderBy(d => d.Id), - d => Assert.Equal("CS0029", d.Id), - d => Assert.Equal("CS1503", d.Id)); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,27): error CS1503: Argument 1: cannot convert from 'string' to 'int' + // ParentValue + Diagnostic(ErrorCode.ERR_BadArgType, "ParentValue").WithArguments("1", "string", "int").WithLocation(1, 27), + // (30,127): error CS0029: Cannot implicitly convert type 'int' to 'string' + // __builder.AddComponentParameter(2, "ValueChanged", (global::System.Action)(__value => ParentValue = __value)); + Diagnostic(ErrorCode.ERR_NoImplicitConv, "__value").WithArguments("int", "string").WithLocation(DesignTime ? 37 : 30, DesignTime ? 38 : 127)); } [IntegrationTestFact] @@ -1860,10 +1860,18 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var assembly = CompileToAssembly(generated, throwOnFailure: false); + var assembly = CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,27): error CS1503: Argument 1: cannot convert from 'string' to 'int' + // ParentValue + Diagnostic(ErrorCode.ERR_BadArgType, "ParentValue").WithArguments("1", "string", "int").WithLocation(1, 27), + // (37,13): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'Microsoft.AspNetCore.Components.EventCallback' + // global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue))); + Diagnostic(ErrorCode.ERR_BadArgType, "global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => ParentValue = __value, ParentValue)").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "Microsoft.AspNetCore.Components.EventCallback").WithLocation(DesignTime ? 37 : 30, DesignTime ? 13 : 320) + ); + // This has some errors Assert.Collection( - assembly.Diagnostics.OrderBy(d => d.Id), + assembly.CSharpDiagnostics.OrderBy(d => d.Id), d => Assert.Equal("CS1503", d.Id), d => Assert.Equal("CS1503", d.Id)); } @@ -2060,12 +2068,16 @@ public class MyComponent : ComponentBase public int ParentValue { get; set; } = 42; }"); - var assembly = CompileToAssembly(generated, throwOnFailure: false); - // This has some errors - Assert.Collection( - assembly.Diagnostics.OrderBy(d => d.Id), - d => Assert.Equal("CS0029", d.Id), - d => Assert.Equal("CS1662", d.Id)); + var row = DesignTime ? 38 : 31; + var column = DesignTime ? 179 : 227; + + CompileToAssembly(generated, + // (31,227): error CS0029: Cannot implicitly convert type 'int' to 'string' + // __builder.AddComponentParameter(3, "ValueExpression", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck>>(() => ParentValue)); + Diagnostic(ErrorCode.ERR_NoImplicitConv, "ParentValue").WithArguments("int", "string").WithLocation(row, column), + // (31,227): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type + // __builder.AddComponentParameter(3, "ValueExpression", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck>>(() => ParentValue)); + Diagnostic(ErrorCode.ERR_CantConvAnonMethReturns, "ParentValue").WithArguments("lambda expression").WithLocation(row, column)); } [IntegrationTestFact] @@ -2477,11 +2489,10 @@ public class MyComponent : ComponentBase // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,61): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(1, 61)); } [IntegrationTestFact] @@ -2516,11 +2527,10 @@ public class CustomValue // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,61): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(1, 61)); } [IntegrationTestFact] @@ -2555,12 +2565,10 @@ public class CustomValue // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); - + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,82): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(1, 82)); } [IntegrationTestFact] @@ -2593,11 +2601,10 @@ @typeparam TParam // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(2,61): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(2, 61)); } [IntegrationTestFact] @@ -2630,11 +2637,10 @@ @typeparam TParam // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(2,77): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("2", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(2, 77)); } [IntegrationTestFact] @@ -3035,11 +3041,10 @@ public class MyComponent : ComponentBase // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - var error = Assert.Single(result.Diagnostics); - // Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to '...' (System.Action/System.Func) - Assert.Equal("CS1503", error.Id); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,63): error CS1503: Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'System.Action' + // UpdateValue + Diagnostic(ErrorCode.ERR_BadArgType, "UpdateValue").WithArguments("1", "Microsoft.AspNetCore.Components.EventCallback", "System.Action").WithLocation(1, 63)); } [IntegrationTestFact] @@ -3420,11 +3425,10 @@ public class MyComponent : ComponentBase // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var assembly = CompileToAssembly(generated, throwOnFailure: false); - // This has some errors - Assert.Collection( - assembly.Diagnostics.OrderBy(d => d.Id), - d => Assert.Equal("CS8030", d.Id)); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(1,94): error CS8030: Anonymous function converted to a void returning delegate cannot return a value + // (value => { ParentValue = value; return Task.CompletedTask; }) + Diagnostic(ErrorCode.ERR_RetNoObjectRequiredLambda, "return").WithLocation(1, 94)); } [IntegrationTestFact] @@ -3845,10 +3849,10 @@ public static class BindAttributes public string ParentValue { get; set; } = ""hi""; public void UpdateValue(string value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10015", diagnostic.Id)); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -3877,10 +3881,10 @@ public static class BindAttributes public string ParentValue { get; set; } = ""hi""; public void UpdateValue(string value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10016", diagnostic.Id)); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -3909,10 +3913,10 @@ public static class BindAttributes public string ParentValue { get; set; } = ""hi""; public void UpdateValue(string value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10018", diagnostic.Id), diagnostic => Assert.Equal("RZ10015", diagnostic.Id)); @@ -3953,10 +3957,10 @@ public class MyComponent : ComponentBase public int ParentValue { get; set; } = 42; public void UpdateValue(int value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10020", diagnostic.Id)); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -3986,10 +3990,10 @@ public static class BindAttributes public void UpdateValue(string value) => ParentValue = value; public void AfterUpdate() { } -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10019", diagnostic.Id)); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -4018,10 +4022,10 @@ public static class BindAttributes public string ParentValue { get; set; } = ""hi""; public void UpdateValue(string value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10016", diagnostic.Id)); } @@ -4047,10 +4051,10 @@ public static class BindAttributes public string ParentValue { get; set; } = ""hi""; public void UpdateValue(string value) => ParentValue = value; -}", throwOnFailure: false); +}"); // Assert - Assert.Collection(generated.Diagnostics, + Assert.Collection(generated.RazorDiagnostics, diagnostic => Assert.Equal("RZ10017", diagnostic.Id)); } @@ -4661,11 +4665,11 @@ @using Test3 // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); + var result = CompileToAssembly(generated); if (DesignTime) { - Assert.Collection(generated.Diagnostics, d => + Assert.Collection(generated.RazorDiagnostics, d => { Assert.Equal("RZ9985", d.Id); Assert.Equal(RazorDiagnosticSeverity.Error, d.Severity); @@ -4708,12 +4712,12 @@ public class SomeComponent : ComponentBase @using static Test2.SomeComponent @using Foo = Test3 - ", throwOnFailure: false); + "); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -4894,10 +4898,10 @@ @preservewhitespace someVariable @code { bool someVariable = false; } -", throwOnFailure: false); +"); // Assert - Assert.Collection(generated.Diagnostics, d => { Assert.Equal("RZ1038", d.Id); }); + Assert.Collection(generated.RazorDiagnostics, d => { Assert.Equal("RZ1038", d.Id); }); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/7169")] @@ -4925,21 +4929,10 @@ @inherits BaseComponent // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var compiled = CompileToAssembly(generated, throwOnFailure: false); - if (DesignTime) - { - compiled.Diagnostics.Verify( - // x:\dir\subdir\Test\TestComponent.cshtml(4,7): warning CS8602: Dereference of a possibly null reference. - // __o = _field.ToString(); - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "_field").WithLocation(4, 7)); - } - else - { - compiled.Diagnostics.Verify( - // x:\dir\subdir\Test\TestComponent.cshtml(4,3): warning CS8602: Dereference of a possibly null reference. - // __builder.AddContent(1, _field.ToString()); - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "_field").WithLocation(4, 3)); - } + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(4,7): warning CS8602: Dereference of a possibly null reference. + // __o = _field.ToString(); + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "_field").WithLocation(4, DesignTime ? 7 : 3)); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/7169")] @@ -4963,30 +4956,29 @@ @inherits BaseComponent @(_field.ToString()) """, nullableEnable: false, - throwOnFailure: false); + expectedCSharpDiagnostics: + // (18,62): warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source. + // public partial class TestComponent : BaseComponent + Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(18, 62)); // Assert - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var compiled = CompileToAssembly(generated, throwOnFailure: false); - if (DesignTime) - { - compiled.Diagnostics.Verify( + CompileToAssembly(generated, DesignTime + ? [ // x:\dir\subdir\Test\TestComponent.cshtml(1,21): warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source. // BaseComponent __typeHelper = default!; Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(1, 21), // (14,62): warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source. // public partial class TestComponent : BaseComponent - Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(14, 62)); - } - else - { - compiled.Diagnostics.Verify( + Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(14, 62) + ] + : [ // (14,62): warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source. // public partial class TestComponent : BaseComponent - Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(14, 62)); - } + Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(14, 62) + ]); } #endregion @@ -5464,11 +5456,10 @@ private void Increment(ChangeEventArgs e) { AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - // Cannot convert from method group to Action - this isn't a great error message, but it's - // what the compiler gives us. - Assert.Collection(result.Diagnostics, d => { Assert.Equal("CS1503", d.Id); }); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(2,24): error CS1503: Argument 2: cannot convert from 'method group' to 'Microsoft.AspNetCore.Components.EventCallback' + // Increment + Diagnostic(ErrorCode.ERR_BadArgType, "Increment").WithArguments("2", "method group", "Microsoft.AspNetCore.Components.EventCallback").WithLocation(2, 24)); } [IntegrationTestFact] @@ -5504,8 +5495,7 @@ public class MyType // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument, verifyLinePragmas: DesignTime); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(4,17): warning CS0169: The field 'TestComponent.counter' is never used // private int counter; Diagnostic(ErrorCode.WRN_UnreferencedField, "counter").WithArguments("Test.TestComponent.counter").WithLocation(4, 17)); @@ -5547,8 +5537,7 @@ public class MyType // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument, verifyLinePragmas: DesignTime); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + var result = CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(4,17): warning CS0169: The field 'TestComponent.counter' is never used // private int counter; Diagnostic(ErrorCode.WRN_UnreferencedField, "counter").WithArguments("Test.TestComponent.counter").WithLocation(4, 17)); @@ -6326,7 +6315,7 @@ public class Column : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentTypeInferenceUnderspecified.Id, diagnostic.Id); } @@ -6606,7 +6595,7 @@ public class Column : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentMissingTypeArgument.Id, diagnostic.Id); } @@ -6787,7 +6776,7 @@ public class Column : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentTypeInferenceUnderspecified.Id, diagnostic.Id); } @@ -6935,7 +6924,7 @@ public class Column : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentTypeInferenceUnderspecified.Id, diagnostic.Id); } @@ -9426,6 +9415,16 @@ public class MainLayout : ComponentBase, ILayoutComponent ")); // Act + DiagnosticDescription[] expectedDiagnostics = [ + // (4,31): error CS0246: The type or namespace name 'ComponentBase' could not be found (are you missing a using directive or an assembly reference?) + // public class MainLayout : ComponentBase, ILayoutComponent + Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "ComponentBase").WithArguments("ComponentBase").WithLocation(4, 31), + // (4,46): error CS0246: The type or namespace name 'ILayoutComponent' could not be found (are you missing a using directive or an assembly reference?) + // public class MainLayout : ComponentBase, ILayoutComponent + Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "ILayoutComponent").WithArguments("ILayoutComponent").WithLocation(4, 46), + // (6,16): error CS0246: The type or namespace name 'RenderFragment' could not be found (are you missing a using directive or an assembly reference?) + // public RenderFragment Body { get; set; } + Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "RenderFragment").WithArguments("RenderFragment").WithLocation(6, 16)]; var generated = CompileToCSharp("_Imports.razor", @" @using System.Text @using System.Reflection @@ -9433,12 +9432,21 @@ @using System.Reflection @layout MainLayout @Foo
Hello
-", throwOnFailure: false, fileKind: FileKinds.ComponentImport); +", fileKind: FileKinds.ComponentImport, expectedCSharpDiagnostics: expectedDiagnostics); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated, [.. expectedDiagnostics, + // x:\dir\subdir\Test\_Imports.razor(5,2): error CS0103: The name 'Foo' does not exist in the current context + // Foo + Diagnostic(ErrorCode.ERR_NameNotInContext, "Foo").WithArguments("Foo").WithLocation(5, DesignTime ? 7 : 2), + .. (DesignTime ? Array.Empty() + : [ + // (33,13): error CS0103: The name '__builder' does not exist in the current context + // __builder.AddContent(0, + Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(33, 13) + ])]); } [IntegrationTestFact] @@ -9457,7 +9465,7 @@ public class Counter : ComponentBase """)); // Act - var generated = CompileToCSharp("Index.razor", """ + var generated = CompileToCSharp("Index.razor", cshtmlContent: """ @using global::MyComponents @@ -9527,7 +9535,7 @@ public class Counter2 : ComponentBase ")); // Act - var generated = CompileToCSharp("Pages/Counter.razor", @" + var generated = CompileToCSharp("Pages/Counter.razor", cshtmlContent: @" @namespace New.Test "); @@ -9700,7 +9708,7 @@ public void DuplicateMarkupAttributes_IsAnError() AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttribute.Id, diagnostic.Id); } @@ -9720,7 +9728,7 @@ @using Microsoft.AspNetCore.Components.Web AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttributeDirective.Id, diagnostic.Id); } @@ -9739,7 +9747,7 @@ public void DuplicateMarkupAttributes_Multiple_IsAnError() AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - Assert.All(generated.Diagnostics, d => + Assert.All(generated.RazorDiagnostics, d => { Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttribute.Id, d.Id); }); @@ -9766,7 +9774,7 @@ @using Microsoft.AspNetCore.Components.Web AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttributeDirective.Id, diagnostic.Id); } @@ -9790,7 +9798,7 @@ @using Microsoft.AspNetCore.Components.Web AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttributeDirective.Id, diagnostic.Id); } @@ -9814,7 +9822,7 @@ @using Microsoft.AspNetCore.Components.Web AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateMarkupAttributeDirective.Id, diagnostic.Id); } @@ -9843,7 +9851,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameter.Id, diagnostic.Id); } @@ -9872,7 +9880,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - Assert.All(generated.Diagnostics, d => + Assert.All(generated.RazorDiagnostics, d => { Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameter.Id, d.Id); }); @@ -9903,7 +9911,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameter.Id, diagnostic.Id); } @@ -9938,7 +9946,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameterDirective.Id, diagnostic.Id); } @@ -9973,7 +9981,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameterDirective.Id, diagnostic.Id); } @@ -10008,7 +10016,7 @@ public class MyComponent : ComponentBase AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.DuplicateComponentParameterDirective.Id, diagnostic.Id); } @@ -10111,7 +10119,7 @@ public class SurveyPrompt : ComponentBase // This has some errors Assert.Collection( - generated.Diagnostics.OrderBy(d => d.Id), + generated.RazorDiagnostics.OrderBy(d => d.Id), d => Assert.Equal("RZ1034", d.Id), d => Assert.Equal("RZ1035", d.Id)); } @@ -10222,12 +10230,11 @@ public enum MyEnum // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(1,31): error CS0119: 'TestComponent.MyEnum' is a type, which is not valid in the given context // MyEnum Diagnostic(ErrorCode.ERR_BadSKunknown, "MyEnum").WithArguments("Test.TestComponent.MyEnum", "type").WithLocation(1, 31)); - Assert.NotEmpty(generated.Diagnostics); + Assert.NotEmpty(generated.RazorDiagnostics); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9346")] @@ -10262,12 +10269,11 @@ public enum MyEnum // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(1,31): error CS0119: 'TestComponent.MyEnum' is a type, which is not valid in the given context // MyEnum Diagnostic(ErrorCode.ERR_BadSKunknown, "MyEnum").WithArguments("Test.TestComponent.MyEnum", "type").WithLocation(1, 31)); - Assert.NotEmpty(generated.Diagnostics); + Assert.NotEmpty(generated.RazorDiagnostics); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9346")] @@ -10298,19 +10304,14 @@ public class MyComponent : ComponentBase // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(1,32): error CS1003: Syntax error, ',' expected // x Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments(",").WithLocation(1, 32), - DesignTime // (27,91): error CS1501: No overload for method 'TypeCheck' takes 2 arguments // __o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck( - ? Diagnostic(ErrorCode.ERR_BadArgCount, "TypeCheck").WithArguments("TypeCheck", "2").WithLocation(27, 91) - // (21,138): error CS1501: No overload for method 'TypeCheck' takes 2 arguments - // __builder.AddComponentParameter(1, "StringProperty", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck( - : Diagnostic(ErrorCode.ERR_BadArgCount, "TypeCheck").WithArguments("TypeCheck", "2").WithLocation(21, 138)); - Assert.NotEmpty(generated.Diagnostics); + Diagnostic(ErrorCode.ERR_BadArgCount, "TypeCheck").WithArguments("TypeCheck", "2").WithLocation(DesignTime ? 27 : 21, DesignTime? 91 : 138)); + Assert.NotEmpty(generated.RazorDiagnostics); } [IntegrationTestFact] @@ -10494,18 +10495,24 @@ public void ProducesStandardLinePragmaForCSharpCode() [Parameter] public int IncrementAmount { get; set; } } -", throwOnFailure: false); +"); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated, + // x:\dir\subdir\Test\TestComponent.cshtml(8,3): error CS1069: The type name 'Console' could not be found in the namespace 'System'. This type has been forwarded to assembly 'System.Console, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. + // System.Console.WriteLine(1);System.Console.WriteLine(2); + Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNSFwd, "System.Console").WithArguments("Console", "System", "System.Console, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").WithLocation(8, 3), + // x:\dir\subdir\Test\TestComponent.cshtml(8,31): error CS1069: The type name 'Console' could not be found in the namespace 'System'. This type has been forwarded to assembly 'System.Console, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. + // System.Console.WriteLine(1);System.Console.WriteLine(2); + Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNSFwd, "System.Console").WithArguments("Console", "System", "System.Console, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").WithLocation(8, 31)); } [IntegrationTestFact] - public void CanProduceLinePragmasForComponentWithRenderFragment() + public void CanProduceLinePragmasForComponentWithRenderFragment_01() { - var generated = CompileToCSharp(@" + var code = @"
@ActionText @if (!Collapsed) @@ -10526,12 +10533,179 @@ void Toggle() { Collapsed = !Collapsed; } -}", throwOnFailure: false); +}"; + + DiagnosticDescription[] expectedDiagnostics = DesignTime + ? [// (36,9): error CS0103: The name '__builder' does not exist in the current context + // __builder.OpenElement(0, "p"); + Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(36, 9), + // (37,29): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(37, 29), + // (37,30): error CS1031: Type expected + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_TypeExpected, "1").WithLocation(37, 30), + // (37,30): error CS8124: Tuple must contain at least two elements. + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_TupleTooFewElements, "1").WithLocation(37, 30), + // (37,30): error CS1026: ) expected + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_CloseParenExpected, "1").WithLocation(37, 30), + // (37,30): error CS1519: Invalid token '1' in class, record, struct, or interface member declaration + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "1").WithArguments("1").WithLocation(37, 30), + // (45,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(45, 9), + // (45,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(45, 9), + // (46,31): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(46, 31), + // (46,32): error CS8124: Tuple must contain at least two elements. + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(46, 32), + // (46,33): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(46, 33), + // (47,35): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(47, 35), + // (47,36): error CS1031: Type expected + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_TypeExpected, "2").WithLocation(47, 36), + // (47,36): error CS8124: Tuple must contain at least two elements. + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_TupleTooFewElements, "2").WithLocation(47, 36), + // (47,36): error CS1026: ) expected + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_CloseParenExpected, "2").WithLocation(47, 36), + // (47,36): error CS1519: Invalid token '2' in class, record, struct, or interface member declaration + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "2").WithArguments("2").WithLocation(47, 36)] + : [// (36,9): error CS0103: The name '__builder' does not exist in the current context + // __builder.OpenElement(0, "p"); + Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(36, 9), + // (37,29): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(37, 29), + // (37,30): error CS1031: Type expected + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_TypeExpected, "1").WithLocation(37, 30), + // (37,30): error CS8124: Tuple must contain at least two elements. + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_TupleTooFewElements, "1").WithLocation(37, 30), + // (37,30): error CS1026: ) expected + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_CloseParenExpected, "1").WithLocation(37, 30), + // (37,30): error CS1519: Invalid token '1' in class, record, struct, or interface member declaration + // __builder.AddContent(1, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "1").WithArguments("1").WithLocation(37, 30), + // (45,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(45, 9), + // (45,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(45, 9), + // (46,31): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(46, 31), + // (46,32): error CS8124: Tuple must contain at least two elements. + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(46, 32), + // (46,33): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(46, 33), + // (47,35): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(47, 35), + // (47,36): error CS1031: Type expected + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_TypeExpected, "2").WithLocation(47, 36), + // (47,36): error CS8124: Tuple must contain at least two elements. + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_TupleTooFewElements, "2").WithLocation(47, 36), + // (47,36): error CS1026: ) expected + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_CloseParenExpected, "2").WithLocation(47, 36), + // (47,36): error CS1519: Invalid token '2' in class, record, struct, or interface member declaration + // __builder.AddMarkupContent(2, "\r\n"); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "2").WithArguments("2").WithLocation(47, 36) + ]; + + var generated = CompileToCSharp(code, expectedDiagnostics); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated, DesignTime + ? [] + : [// (78,9): error CS0103: The name '__builder' does not exist in the current context + // __builder.OpenElement(10, "p"); + Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(78, 9), + // (79,29): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.AddContent(11, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(79, 29), + // (79,30): error CS1031: Type expected + // __builder.AddContent(11, + Diagnostic(ErrorCode.ERR_TypeExpected, "11").WithLocation(79, 30), + // (79,30): error CS8124: Tuple must contain at least two elements. + // __builder.AddContent(11, + Diagnostic(ErrorCode.ERR_TupleTooFewElements, "11").WithLocation(79, 30), + // (79,30): error CS1026: ) expected + // __builder.AddContent(11, + Diagnostic(ErrorCode.ERR_CloseParenExpected, "11").WithLocation(79, 30), + // (79,30): error CS1519: Invalid token '11' in class, record, struct, or interface member declaration + // __builder.AddContent(11, + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "11").WithArguments("11").WithLocation(79, 30), + // (87,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(87, 9), + // (87,9): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration + // ); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ")").WithArguments(")").WithLocation(87, 9), + // (88,31): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(88, 31), + // (88,32): error CS8124: Tuple must contain at least two elements. + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(88, 32), + // (88,33): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration + // __builder.CloseElement(); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(88, 33)]); + } + + [IntegrationTestFact] + public void CanProduceLinePragmasForComponentWithRenderFragment_02() + { + var generated = CompileToCSharp(@" +
+ @ActionText + @if (!Collapsed) + { +
+ @ChildContent +
+ } +
+@code +{ + [Parameter] + public RenderFragment ChildContent { get; set; } = (context) => @

@context

; + [Parameter] + public bool Collapsed { get; set; } + string ActionText { get => Collapsed ? ""Expand"" : ""Collapse""; } + void Toggle() + { + Collapsed = !Collapsed; + } +}"); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: false); + CompileToAssembly(generated); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9359")] @@ -10558,12 +10732,12 @@ public void RenderMode_Directive_FullyQualified() { var generated = CompileToCSharp(""" @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10571,12 +10745,12 @@ public void RenderMode_Directive_SimpleExpression() { var generated = CompileToCSharp(""" @rendermode @(Microsoft.AspNetCore.Components.Web.RenderMode.Server) - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10590,12 +10764,12 @@ @rendermode @(Microsoft.AspNetCore.Components.Web.RenderMode.Server) [Parameter] public int Count { get; set; } } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10608,12 +10782,12 @@ public void RenderMode_Directive_SimpleExpression_NotFirst() public int Count { get; set; } } @rendermode @(Microsoft.AspNetCore.Components.Web.RenderMode.Server) - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10627,12 +10801,12 @@ @rendermode @(new TestComponent.MyRenderMode("This is some text")) #pragma warning disable CS9113 public class MyRenderMode(string Text) : Microsoft.AspNetCore.Components.IComponentRenderMode { } } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10642,12 +10816,12 @@ public void RenderMode_Directive_WithNamespaces() @namespace Custom.Namespace @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10655,12 +10829,12 @@ public void RenderMode_Attribute_With_SimpleIdentifier() { var generated = CompileToCSharp($""" <{ComponentName} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10675,12 +10849,12 @@ class MyRenderMode : Microsoft.AspNetCore.Components.IComponentRenderMode public string Extra {get;set;} } } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10695,12 +10869,12 @@ public void RenderMode_Attribute_With_Existing_Attributes() [Parameter]public string P2 {get; set;} } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10709,12 +10883,12 @@ public void Duplicate_RenderMode() var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" @rendermode="Value2" /> - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10723,12 +10897,12 @@ public void RenderMode_Multiple_Components() var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> <{{ComponentName}} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10750,12 +10924,12 @@ public void RenderMode_Child_Components() [Parameter] public RenderFragment ChildContent { get; set; } } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10770,12 +10944,12 @@ public void RenderMode_With_TypeInference() { [Parameter] public TRenderMode RenderModeParam { get; set;} } - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact] @@ -10783,12 +10957,12 @@ public void RenderMode_With_Ternary() { var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="@(true ? Microsoft.AspNetCore.Components.Web.RenderMode.Server : null)" /> - """, throwOnFailure: true); + """); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9343")] @@ -10796,12 +10970,12 @@ public void RenderMode_With_Null_Nullable_Disabled() { var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="null" /> - """, throwOnFailure: true, nullableEnable: false); + """, nullableEnable: false); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9343")] @@ -10809,12 +10983,12 @@ public void RenderMode_With_Null_Nullable_Enabled() { var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="null" /> - """, throwOnFailure: true, nullableEnable: true); + """, nullableEnable: true); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - CompileToAssembly(generated, throwOnFailure: true); + CompileToAssembly(generated); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9343")] @@ -10831,14 +11005,12 @@ public class RenderModeContainer RenderModeContainer? Container => null; } <{{ComponentName}} @rendermode="@(Container.RenderMode)" /> - """, throwOnFailure: true, nullableEnable: true); + """, nullableEnable: true); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - - result.Diagnostics.Verify( + CompileToAssembly(generated, DesignTime // x:\dir\subdir\Test\TestComponent.cshtml(10,29): warning CS8602: Dereference of a possibly null reference. // Container.RenderMode @@ -10898,8 +11070,7 @@ @using Microsoft.AspNetCore.Components.Web // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(2,55): error CS1503: Argument 1: cannot convert from 'int' to 'string' // x Diagnostic(ErrorCode.ERR_BadArgType, "x").WithArguments("1", "int", "string").WithLocation(2, 55)); @@ -10920,19 +11091,13 @@ @using Microsoft.AspNetCore.Components.Web // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - if (DesignTime) - { - result.Diagnostics.Verify( + CompileToAssembly(generated, DesignTime + ? [ // x:\dir\subdir\Test\TestComponent.cshtml(2,74): error CS1503: Argument 1: cannot convert from 'int' to 'string' // x - Diagnostic(ErrorCode.ERR_BadArgType, "x").WithArguments("1", "int", "string").WithLocation(2, 74)); - } - else - { - result.Diagnostics.Verify(); - } - Assert.NotEmpty(generated.Diagnostics); + Diagnostic(ErrorCode.ERR_BadArgType, "x").WithArguments("1", "int", "string").WithLocation(2, 74) + ] + : []); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9077")] @@ -10966,8 +11131,7 @@ @using Microsoft.AspNetCore.Components.Web // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify( + CompileToAssembly(generated, // x:\dir\subdir\Test\TestComponent.cshtml(2,55): error CS0103: The name 'x' does not exist in the current context // x Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(2, 55)); @@ -10985,14 +11149,10 @@ @using Microsoft.AspNetCore.Components.Web // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); - var result = CompileToAssembly(generated, throwOnFailure: false); - result.Diagnostics.Verify(DesignTime + CompileToAssembly(generated, // (41,85): error CS7036: There is no argument given that corresponds to the required parameter 'value' of 'RuntimeHelpers.TypeCheck(T)' // global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(); - ? Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "TypeCheck").WithArguments("value", "Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(T)").WithLocation(41, 85) - // (37,105): error CS7036: There is no argument given that corresponds to the required parameter 'value' of 'RuntimeHelpers.TypeCheck(T)' - // string __formName = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(); - : Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "TypeCheck").WithArguments("value", "Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(T)").WithLocation(37, 105)); + Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "TypeCheck").WithArguments("value", "Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(T)").WithLocation(DesignTime? 41 : 37, DesignTime ? 85: 105)); } [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9077")] diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDeclarationIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDeclarationIntegrationTest.cs index 7a95be30bcf..78da8387095 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDeclarationIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDeclarationIntegrationTest.cs @@ -149,16 +149,10 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. ", generated); // Act 2 - var assembly = CompileToAssembly(generated, throwOnFailure: false); - - // Assert 2 - var diagnostic = Assert.Single(assembly.Diagnostics); - - // This error should map to line 2 of the generated file, the test - // says 1 because Roslyn's line/column data structures are 0-based. - var position = diagnostic.Location.GetMappedLineSpan(); - Assert.EndsWith(".cshtml", position.Path); - Assert.Equal(1, position.StartLinePosition.Line); + CompileToAssembly(generated, + // /dir/subdir/Test/TestComponent.cshtml(2,12): error CS0246: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?) + // public StringBuilder Builder { get; set; } + Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "StringBuilder").WithArguments("StringBuilder").WithLocation(2, 12)); } public class BaseClass : IComponent diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiagnosticRazorIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiagnosticRazorIntegrationTest.cs index 6610bd4816e..69716543e13 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiagnosticRazorIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiagnosticRazorIntegrationTest.cs @@ -22,7 +22,7 @@ public void RejectsEndTagWithNoStartTag() "Line1\nLine2\nLine3"); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ9981", item.Id); @@ -42,7 +42,7 @@ public void OldCodeBlockAttributeSyntax_ReportsError() }"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9979", diagnostic.Id); Assert.NotNull(diagnostic.GetMessage(CultureInfo.CurrentCulture)); } @@ -70,7 +70,7 @@ @tagHelperPrefix th "); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ9978", item.Id); @@ -102,7 +102,7 @@ public void DirectiveAttribute_ComplexContent_ReportsError() }"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9986", diagnostic.Id); Assert.Equal( "Component attributes do not support complex content (mixed C# and markup). Attribute: '@key', text: 'Foo @Text'", @@ -113,14 +113,14 @@ public void DirectiveAttribute_ComplexContent_ReportsError() public void Component_StartsWithLowerCase_ReportsError() { // Arrange & Act - var generated = CompileToCSharp("lowerCase.razor", @" + var generated = CompileToCSharp("lowerCase.razor", cshtmlContent: @" @functions { public string Text { get; set; } = ""text""; -}", throwOnFailure: false); +}"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10011", diagnostic.Id); Assert.Equal( "Component 'lowerCase' starts with a lowercase character. Component names cannot start with a lowercase character.", @@ -141,7 +141,7 @@ public void Component_NotFound_ReportsWarning(bool supportLocalizedComponentName }", supportLocalizedComponentNames: supportLocalizedComponentNames); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10012", diagnostic.Id); Assert.Equal(RazorDiagnosticSeverity.Warning, diagnostic.Severity); Assert.Equal( @@ -161,7 +161,7 @@ public void Component_NotFound_StartsWithOtherLetter_WhenLocalizedComponentNames }", supportLocalizedComponentNames: true); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10012", diagnostic.Id); Assert.Equal(RazorDiagnosticSeverity.Warning, diagnostic.Severity); Assert.Equal( @@ -181,7 +181,7 @@ public void Component_NotFound_StartsWithOtherLetter_WhenLocalizedComponentNames }", supportLocalizedComponentNames: false); // Assert - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [Fact] @@ -192,7 +192,7 @@ public void Element_DoesNotStartWithLowerCase_OverrideWithBang_NoWarning() "); // Assert - Assert.Empty(generated.Diagnostics); + Assert.Empty(generated.RazorDiagnostics); } [Fact] @@ -213,7 +213,7 @@ public class MyComponent : ComponentBase "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10013", diagnostic.Id); Assert.Equal( "The start tag name 'MyComponent' does not match the end tag name 'mycomponent'. Components must have matching start and end tag names (case-sensitive).", diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiscoveryIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiscoveryIntegrationTest.cs index 76998cd4edd..dac3556aad0 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiscoveryIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentDiscoveryIntegrationTest.cs @@ -76,7 +76,7 @@ public void ComponentDiscovery_CanFindComponent_DefinedinCshtml() // Arrange // Act - var result = CompileToCSharp("UniqueName.cshtml", string.Empty); + var result = CompileToCSharp("UniqueName.cshtml", cshtmlContent: string.Empty); // Assert var bindings = result.CodeDocument.GetTagHelperContext(); @@ -89,7 +89,7 @@ public void ComponentDiscovery_CanFindComponent_WithTypeParameter() // Arrange // Act - var result = CompileToCSharp("UniqueName.cshtml", @" + var result = CompileToCSharp("UniqueName.cshtml", cshtmlContent: @" @typeparam TItem @functions { [Parameter] public TItem Item { get; set; } @@ -106,7 +106,7 @@ public void ComponentDiscovery_CanFindComponent_WithTypeParameterAndSemicolon() // Arrange // Act - var result = CompileToCSharp("UniqueName.cshtml", @" + var result = CompileToCSharp("UniqueName.cshtml", cshtmlContent: @" @typeparam TItem; @functions { [Parameter] public TItem Item { get; set; } @@ -123,7 +123,7 @@ public void ComponentDiscovery_CanFindComponent_WithMultipleTypeParameters() // Arrange // Act - var result = CompileToCSharp("UniqueName.cshtml", @" + var result = CompileToCSharp("UniqueName.cshtml", cshtmlContent: @" @typeparam TItem1 @typeparam TItem2 @typeparam TItem3 @@ -142,7 +142,7 @@ public void ComponentDiscovery_CanFindComponent_WithMultipleTypeParametersAndMix // Arrange // Act - var result = CompileToCSharp("UniqueName.cshtml", @" + var result = CompileToCSharp("UniqueName.cshtml", cshtmlContent: @" @typeparam TItem1 @typeparam TItem2; @typeparam TItem3 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentFilePathIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentFilePathIntegrationTest.cs index b432bab3617..f4f815cce12 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentFilePathIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentFilePathIntegrationTest.cs @@ -23,7 +23,7 @@ public void FileNameIsInvalidClassName_SanitizesInvalidClassName() var result = CompileToAssembly("Filename with spaces.cshtml", ""); // Assert - Assert.Empty(result.Diagnostics); + Assert.Empty(result.CSharpDiagnostics); var type = Assert.Single(result.Assembly.GetTypes().Where(t => t.GetCustomAttributes(inherit: false).All(a => a.GetType().Name != "CompilerGeneratedAttribute"))); Assert.Equal(DefaultRootNamespace, type.Namespace); @@ -43,7 +43,7 @@ public void CreatesClassWithCorrectNameAndNamespace(string relativePath, string var result = CompileToAssembly(relativePath, ""); // Assert - Assert.Empty(result.Diagnostics); + Assert.Empty(result.CSharpDiagnostics); var type = Assert.Single(result.Assembly.GetTypes().Where(t => t.GetCustomAttributes(inherit: false).All(a => a.GetType().Name != "CompilerGeneratedAttribute"))); Assert.Equal(expectedNamespace, type.Namespace); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentGenericTypeIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentGenericTypeIntegrationTest.cs index ecab49e994a..17637d275bb 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentGenericTypeIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentGenericTypeIntegrationTest.cs @@ -93,7 +93,7 @@ public void GenericComponent_WithoutAnyTypeParameters_TriggersDiagnostic() "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentTypeInferenceUnderspecified.Id, diagnostic.Id); Assert.Equal(""" The type of component 'GenericContext' cannot be inferred based on the values provided. Consider specifying the type arguments directly using the following attributes: 'TItem'. @@ -112,7 +112,7 @@ public void GenericComponent_WithMissingTypeParameters_TriggersDiagnostic() "); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Same(ComponentDiagnosticFactory.GenericComponentMissingTypeArgument.Id, diagnostic.Id); Assert.Equal(""" The component 'MultipleGenericParameter' is missing required type arguments. Specify the missing types using the attributes: 'TItem2', 'TItem3'. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentImportsIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentImportsIntegrationTest.cs index 3f9f66275cb..0b52c303460 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentImportsIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentImportsIntegrationTest.cs @@ -16,33 +16,33 @@ public class ComponentImportsIntegrationTest : RazorIntegrationTestBase public void NoErrorsForUsingStatements() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @using System.Text @using System.Reflection @* This is allowed in imports *@ "); // Assert - Assert.Empty(result.Diagnostics); + Assert.Empty(result.RazorDiagnostics); } [Fact] public void NoErrorsForRazorComments() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @* This is allowed in imports *@ "); // Assert - Assert.Empty(result.Diagnostics); + Assert.Empty(result.RazorDiagnostics); } [Fact] public void NoErrorsForSupportedDirectives() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @inject FooService Foo @typeparam TItem @implements ISomeInterface @@ -50,19 +50,19 @@ @inherits SomeNamespace.SomeBaseType "); // Assert - Assert.Empty(result.Diagnostics); + Assert.Empty(result.RazorDiagnostics); } [Fact] public void ErrorsForPageDirective() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @page ""/"" "); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ9987", item.Id); @@ -74,14 +74,14 @@ public void ErrorsForPageDirective() public void ErrorsForTagHelperDirectives() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @addTagHelper *, TestAssembly @removeTagHelper *, TestAssembly @tagHelperPrefix th: "); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ9978", item.Id); @@ -106,7 +106,7 @@ public void ErrorsForTagHelperDirectives() public void ErrorsForFunctionsDirective() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @functions { public class Test { @@ -115,7 +115,7 @@ public class Test "); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ10003", item.Id); @@ -127,13 +127,13 @@ public class Test public void ErrorsForSectionDirective() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @section Foo { } "); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ10003", item.Id); @@ -145,12 +145,12 @@ @section Foo { public void ErrorsForMarkup() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @"
asdf
"); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ10003", item.Id); @@ -162,7 +162,7 @@ public void ErrorsForMarkup() public void ErrorsForCode() { // Arrange/Act - var result = CompileToCSharp("_Imports.razor", @" + var result = CompileToCSharp("_Imports.razor", cshtmlContent: @" @Foo @(Bar) @{ @@ -170,7 +170,7 @@ public void ErrorsForCode() }"); // Assert - Assert.Collection(result.Diagnostics, + Assert.Collection(result.RazorDiagnostics, item => { Assert.Equal("RZ10003", item.Id); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeAttributeIntegrationTests.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeAttributeIntegrationTests.cs index 195361db37a..fcfb1f28e4c 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeAttributeIntegrationTests.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeAttributeIntegrationTests.cs @@ -20,12 +20,12 @@ public void RenderMode_Attribute_With_Diagnostics() { var generated = CompileToCSharp($$""" <{{ComponentName}} @rendermode="@Microsoft.AspNetCore.Components.Web.RenderMode.Server)" /> - """, throwOnFailure: true); + """); // Assert //x:\dir\subdir\Test\TestComponent.cshtml(1, 29): Error RZ9986: Component attributes do not support complex content(mixed C# and markup). Attribute: '@rendermode', text: 'Microsoft.AspNetCore.Components.Web.RenderMode.Server)' - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9986", diagnostic.Id); } @@ -34,11 +34,11 @@ public void RenderMode_Attribute_On_Html_Element() { var generated = CompileToCSharp(""" - """, throwOnFailure: true); + """); // Assert //x:\dir\subdir\Test\TestComponent.cshtml(1,21): Error RZ10023: Attribute 'rendermode' is only valid when used on a component. - var diag = Assert.Single(generated.Diagnostics); + var diag = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10023", diag.Id); } @@ -49,12 +49,12 @@ public void RenderMode_Attribute_On_Component_With_Directive() @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server <{{ComponentName}} @rendermode="Microsoft.AspNetCore.Components.Web.RenderMode.Server" /> - """, throwOnFailure: true); + """); // Assert // x:\dir\subdir\Test\TestComponent.cshtml(3,29): Error RZ10024: Cannot override render mode for component 'Test.TestComponent' as it explicitly declares one. - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ10024", diagnostic.Id); } } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeDirectiveIntegrationTests.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeDirectiveIntegrationTests.cs index a9379e8abd5..ed897de6451 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeDirectiveIntegrationTests.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeDirectiveIntegrationTests.cs @@ -80,7 +80,7 @@ public void RenderMode_Missing_Value() // Assert // Error RZ1041: The 'rendermode' directive expects an identifier or explicit razor expression. - var diagnostic = Assert.Single(compilationResult.Diagnostics); + var diagnostic = Assert.Single(compilationResult.RazorDiagnostics); Assert.Equal("RZ1041", diagnostic.Id); } @@ -95,7 +95,7 @@ @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server // Assert //Error RZ2001: The 'rendermode' directive may only occur once per document. - var diagnostic = Assert.Single(compilationResult.Diagnostics); + var diagnostic = Assert.Single(compilationResult.RazorDiagnostics); Assert.Equal("RZ2001", diagnostic.Id); } @@ -106,10 +106,9 @@ public void RenderMode_With_InvalidIdentifier() @rendermode NoExist """); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: false); - assemblyResult.Diagnostics.Verify( + CompileToAssembly(compilationResult, // x:\dir\subdir\Test\TestComponent.cshtml(25,101): error CS0103: The name 'NoExist' does not exist in the current context // NoExist Diagnostic(ErrorCode.ERR_NameNotInContext, "NoExist").WithArguments("NoExist").WithLocation(25, 101) @@ -123,10 +122,9 @@ public void LanguageVersion() @rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server """, configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 }); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: false); - assemblyResult.Diagnostics.Verify( + CompileToAssembly(compilationResult, // x:\dir\subdir\Test\TestComponent.cshtml(1,2): error CS0103: The name 'rendermode' does not exist in the current context // __builder.AddContent(0, rendermode); Diagnostic(ErrorCode.ERR_NameNotInContext, "rendermode").WithArguments("rendermode").WithLocation(1, 2) @@ -145,10 +143,9 @@ @rendermode Foo } """, configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 }); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: true); - assemblyResult.Diagnostics.Verify(); + CompileToAssembly(compilationResult); } [Fact] @@ -163,10 +160,9 @@ @rendermode Foo } """, configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_8_0 }); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: false); - assemblyResult.Diagnostics.Verify( + CompileToAssembly(compilationResult, // x:\dir\subdir\Test\TestComponent.cshtml(34,101): error CS0103: The name 'Foo' does not exist in the current context // Foo Diagnostic(ErrorCode.ERR_NameNotInContext, "Foo").WithArguments("Foo").WithLocation(34, 101), @@ -186,12 +182,11 @@ @rendermode myRenderMode { Microsoft.AspNetCore.Components.IComponentRenderMode myRenderMode = new Microsoft.AspNetCore.Components.Web.ServerRenderMode(); } - """, throwOnFailure: true); + """); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: false); - assemblyResult.Diagnostics.Verify( + CompileToAssembly(compilationResult, // x:\dir\subdir\Test\TestComponent.cshtml(34, 101): error CS0120: An object reference is required for the non-static field, method, or property 'TestComponent.myRenderMode' // myRenderMode Diagnostic(ErrorCode.ERR_ObjectRequired, "myRenderMode").WithArguments("Test.TestComponent.myRenderMode").WithLocation(34, 101) @@ -207,11 +202,11 @@ @rendermode myRenderMode { static Microsoft.AspNetCore.Components.IComponentRenderMode myRenderMode = new Microsoft.AspNetCore.Components.Web.ServerRenderMode(); } - """, throwOnFailure: true); + """); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: true); + CompileToAssembly(compilationResult); } [Fact] @@ -223,12 +218,11 @@ @rendermode TestComponent.myRenderMode { internal static Microsoft.AspNetCore.Components.IComponentRenderMode myRenderMode = new Microsoft.AspNetCore.Components.Web.ServerRenderMode(); } - """, throwOnFailure: true); + """); - Assert.Empty(compilationResult.Diagnostics); + Assert.Empty(compilationResult.RazorDiagnostics); - var assemblyResult = CompileToAssembly(compilationResult, throwOnFailure: false); - assemblyResult.Diagnostics.Verify(); + CompileToAssembly(compilationResult); } [Fact] diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTemplateIntegrationTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTemplateIntegrationTest.cs index 0d179dbb115..e63c17fcdaa 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTemplateIntegrationTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTemplateIntegrationTest.cs @@ -21,10 +21,10 @@ public void Template_ImplicitExpressionInMarkupAttribute_CreatesDiagnostic() // Arrange // Act - var generated = CompileToCSharp(@"
"" />", throwOnFailure: false); + var generated = CompileToCSharp(@"
"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ1005", diagnostic.Id); } @@ -37,7 +37,7 @@ public void Template_ExplicitExpressionInMarkupAttribute_CreatesDiagnostic() var generated = CompileToCSharp(@"
)"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9994", diagnostic.Id); } @@ -59,11 +59,11 @@ public class MyComponent : ComponentBase ")); // Act - var generated = CompileToCSharp(@"
"" />", throwOnFailure: false); + var generated = CompileToCSharp(@""" />"); // Assert Assert.Collection( - generated.Diagnostics, + generated.RazorDiagnostics, d => Assert.Equal("RZ9986", d.Id), d => Assert.Equal("RZ1005", d.Id)); } @@ -86,7 +86,7 @@ public class MyComponent : ComponentBase var generated = CompileToCSharp(@")"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9994", diagnostic.Id); } @@ -99,7 +99,7 @@ public void Template_ExplicitExpressionInRef_CreatesDiagnostic() var generated = CompileToCSharp(@"
)"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9994", diagnostic.Id); } @@ -113,7 +113,7 @@ public void Template_ExplicitExpressionInBind_CreatesDiagnostic() var generated = CompileToCSharp(@")"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9994", diagnostic.Id); } @@ -126,7 +126,7 @@ public void Template_ExplicitExpressionInEventHandler_CreatesDiagnostic() var generated = CompileToCSharp(@")"" />"); // Assert - var diagnostic = Assert.Single(generated.Diagnostics); + var diagnostic = Assert.Single(generated.RazorDiagnostics); Assert.Equal("RZ9994", diagnostic.Id); } } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTypingTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTypingTest.cs index a2fca9ff8e5..5c5df9d5e1a 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTypingTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentTypingTest.cs @@ -3,10 +3,8 @@ #nullable disable -using System; -using System.Linq; +using Microsoft.CodeAnalysis; using Xunit; -using Xunit.Sdk; namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests; @@ -61,44 +59,27 @@ public class ModelState for (var i = 0; i <= text.Length; i++) { - try - { - CompileToCSharp(text.Substring(0, i), throwOnFailure: false); - } - catch (Exception ex) - { - throw new XunitException($@" -Code generation failed on iteration {i} with source text: -{text.Substring(0, i)} - -Exception: -{ex} -"); - } + CompileToCSharp(text.Substring(0, i)); } } [Fact] // Regression test for #1068 public void Regression_1068() { - // Arrange - - // Act - var generated = CompileToCSharp(@" + CompileToCSharp(@" @functions { Test.ModelState ModelState { get; set; } } -", throwOnFailure: false); - - // Assert +", + // /dir/subdir/Test/TestComponent.cshtml(3,10): error CS0234: The type or namespace name 'ModelState' does not exist in the namespace 'Test' (are you missing an assembly reference?) + // Test.ModelState ModelState { get; set; } + Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "ModelState").WithArguments("ModelState", "Test").WithLocation(3, 10)); } [Fact] public void MalformedAttributeContent() { - // Act - // Arrange AdditionalSyntaxTrees.Add(Parse(@" using System; using Microsoft.AspNetCore.Components; @@ -124,8 +105,6 @@ public class ModelState