Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify handling of the @ character in razor code blocks #10232

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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: []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1439,7 +1439,7 @@ public void AddAttribute_ImplicitStringConversion_Bind()
using Microsoft.AspNetCore.Components;

namespace Test;

public class MyClass<T>
{
public static implicit operator string(MyClass<T> c) => throw null!;
Expand All @@ -1452,16 +1452,16 @@ public class MyComponent<T> : ComponentBase

[Parameter]
public EventCallback<MyClass<T>> 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!;
}
Expand Down Expand Up @@ -1493,7 +1493,7 @@ public void AddAttribute_ImplicitStringConversion_CustomEvent()
using Microsoft.AspNetCore.Components;

namespace Test;

public class MyClass<T>
{
public static implicit operator string(MyClass<T> c) => throw null!;
Expand All @@ -1506,16 +1506,16 @@ public class MyComponent<T> : 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!;
}
Expand Down Expand Up @@ -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!;
Expand Down Expand Up @@ -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!;
Expand Down Expand Up @@ -1615,7 +1615,7 @@ public void AddAttribute_ImplicitBooleanConversion()
using Microsoft.AspNetCore.Components;

namespace Test;

public class MyClass<T>
{
public static implicit operator bool(MyClass<T> c) => throw null!;
Expand All @@ -1625,7 +1625,7 @@ public class MyComponent<T> : ComponentBase
{
[Parameter]
public MyClass<T> MyParameter { get; set; }

[Parameter]
public bool BoolParameter { get; set; }
}
Expand All @@ -1651,7 +1651,7 @@ public void AddComponentParameter()
@typeparam T

<TestComponent Param="42" />

@code {
[Parameter]
public T Param { get; set; }
Expand Down Expand Up @@ -4715,7 +4715,7 @@ @using static Test2.SomeComponent
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated, throwOnFailure: false);
}

[IntegrationTestFact]
public void Component_WithMultipleUsingDirectives()
{
Expand Down Expand Up @@ -7032,7 +7032,7 @@ public class Grid<T> : ComponentBase
public partial class GridColumn<T> : ComponentBase
{
}

public class X { }
"""));

Expand Down Expand Up @@ -8153,7 +8153,7 @@ public void GenericComponent_TypeParameterOrdering()
// Arrange
AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components;

namespace Test;

public interface IInterfaceConstraint<T> { }
Expand Down Expand Up @@ -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
Expand All @@ -10674,7 +10674,7 @@ class MyRenderMode : Microsoft.AspNetCore.Components.IComponentRenderMode
{
public string Extra {get;set;}
}
}
}
""", throwOnFailure: true);

// Assert
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -609,7 +603,7 @@ public async Task GetSemanticTokens_Razor_MultiLineCommentWithBlankLines(bool pr
@* kdl

skd

333fred marked this conversation as resolved.
Show resolved Hide resolved
sdfasdfasdf
slf*@
""";
Expand Down Expand Up @@ -1087,7 +1081,7 @@ private void AssertSemanticTokensMatchesBaseline(SourceText sourceText, int[]? a

var actualFileContents = GetFileRepresentationOfTokens(sourceText, actualSemanticTokens);

if (GenerateBaselines)
if (GenerateBaselines.ShouldGenerate)
{
GenerateSemanticBaseline(actualFileContents, baselineFileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ protected ToolingParserTestBase(ITestOutputHelper testOutput)
/// </summary>
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; }
Expand Down Expand Up @@ -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);
Expand Down
333fred marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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
333fred marked this conversation as resolved.
Show resolved Hide resolved
{
#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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/// <summary>
Expand Down Expand Up @@ -98,12 +93,6 @@ protected IntegrationTestBase(TestProject.Layer layer, bool? generateBaselines =
/// </summary>
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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -695,13 +684,6 @@ private static string NormalizeNewLines(string content, string lineEnding)
return Regex.Replace(content, "(?<!\r)\n", lineEnding, RegexOptions.None, TimeSpan.FromSeconds(10));
}

// This is to prevent you from accidentally checking in with GenerateBaselines = true
[Fact]
public void GenerateBaselinesMustBeFalse()
{
Assert.False(GenerateBaselines, "GenerateBaselines should be set back to false before you check in!");
}

private class ConfigureCodeRenderingPhase : RazorEnginePhaseBase
{
public ConfigureCodeRenderingPhase(string lineEnding)
Expand Down
Loading