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

Move baselines out of source generator tests #8823

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
using DiffPlex;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
Expand Down Expand Up @@ -406,56 +408,15 @@ internal static class Extensions

// UTF-8 with BOM
private static readonly Encoding _baselineEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);

public static GeneratorRunResult VerifyPageOutput(this GeneratorRunResult result, params string[] expectedOutput)
{
if (expectedOutput.Length == 1 && string.IsNullOrWhiteSpace(expectedOutput[0]))
{
Assert.True(false, GenerateExpectedPageOutput(result));
}
else
{
Assert.Equal(expectedOutput.Length, result.GeneratedSources.Length);
for (int i = 0; i < result.GeneratedSources.Length; i++)
{
var text = TrimChecksum(result.GeneratedSources[i].SourceText.ToString());
Assert.Equal(text, TrimChecksum(expectedOutput[i]), ignoreWhiteSpaceDifferences: true);
}
}

return result;
}

public static GeneratorRunResult VerifyHostOutput(this GeneratorRunResult result, params (string hintName, string text)[] expectedOutputs)
{
if (expectedOutputs.Length == 1 && string.IsNullOrWhiteSpace(expectedOutputs[0].text))
{
Assert.True(false, GenerateExpectedHostOutput(result));
}
else
{
var hostOutputs = result.GetHostOutputs();
Assert.Equal(expectedOutputs.Length, hostOutputs.Length);
for (int i = 0; i < hostOutputs.Length; i++)
{
var expectedOutput = expectedOutputs[i];
var actualOutput = hostOutputs[i];

Assert.Equal(expectedOutput.hintName, actualOutput.Key);
Assert.Equal(expectedOutput.text, actualOutput.Value, ignoreWhiteSpaceDifferences: true);
}
}

return result;
}

private static string CreateBaselineDirectory(string testPath, string testName)
private static string CreateBaselineDirectory(string testPath, string testName, string? subdir = null)
{
var baselineDirectory = Path.Join(
_testProjectRoot,
"TestFiles",
Path.GetFileNameWithoutExtension(testPath)!,
testName);
testName,
subdir);
Directory.CreateDirectory(baselineDirectory);
return baselineDirectory;
}
Expand All @@ -468,12 +429,12 @@ public static GeneratorRunResult VerifyOutputsMatchBaseline(this GeneratorRunRes

foreach (var source in result.GeneratedSources)
{
var baselinePath = Path.Join(baselineDirectory, source.HintName);
var sourceText = source.SourceText.ToString();
GenerateOutputBaseline(baselinePath, sourceText);
var baselineText = File.ReadAllText(baselinePath);
AssertEx.EqualOrDiff(TrimChecksum(baselineText), TrimChecksum(sourceText));
Assert.True(touchedFiles.Add(baselinePath));
VerifyOutput(baselineDirectory, touchedFiles, source);
}

foreach (var source in result.GetHostOutputs())
{
VerifyHostOutput(baselineDirectory, touchedFiles, source);
}

DeleteUnusedBaselines(baselineDirectory, touchedFiles);
Expand Down Expand Up @@ -515,56 +476,87 @@ private static void DeleteUnusedBaselines(string baselineDirectory, HashSet<stri
}
}

private static string GenerateExpectedPageOutput(GeneratorRunResult result)
public static GeneratorRunResult VerifyOutputsMatch(this GeneratorRunResult actual, GeneratorRunResult expected)
{
StringBuilder sb = new StringBuilder("Generated Page Output:").AppendLine().AppendLine();
for (int i = 0; i < result.GeneratedSources.Length; i++)
Assert.Equal(actual.GeneratedSources.Length, expected.GeneratedSources.Length);
for (int i = 0; i < actual.GeneratedSources.Length; i++)
{
if (i > 0)
{
sb.AppendLine(",");
}
sb.Append("@\"").Append(result.GeneratedSources[i].SourceText.ToString().Replace("\"", "\"\"")).Append('"');
AssertEx.EqualOrDiff(expected.GeneratedSources[i].SourceText.ToString(), actual.GeneratedSources[i].SourceText.ToString(), $"Index {i}");
}
return sb.ToString();
}

private static string GenerateExpectedHostOutput(GeneratorRunResult result)
{
StringBuilder sb = new StringBuilder("Generated Host Output:").AppendLine().AppendLine();
var hostOutputs = result.GetHostOutputs();
for (int i = 0; i < hostOutputs.Length; i++)
{
if (i > 0)
{
sb.AppendLine(",");
}
sb.Append("(@\"").Append(hostOutputs[i].Key.Replace("\"", "\"\"")).Append("\", ");
sb.Append("@\"").Append(hostOutputs[i].Value.Replace("\"", "\"\"")).Append("\")");
}
return sb.ToString();
return actual;
}

public static GeneratorRunResult VerifyOutputsMatch(this GeneratorRunResult actual, GeneratorRunResult expected, params (int index, string replacement)[] diffs)
public static GeneratorRunResult VerifyOutputsMatch(this GeneratorRunResult actual, GeneratorRunResult expected,
int n, int[]? diffs = null, int[]? hostDiffs = null, bool expectedHost = true,
[CallerFilePath] string testPath = "", [CallerMemberName] string testName = "")
{
Assert.Equal(actual.GeneratedSources.Length, expected.GeneratedSources.Length);
var baselineDirectory = CreateBaselineDirectory(testPath, testName, subdir: n.ToString());
var touchedFiles = new HashSet<string>();

Assert.Equal(expected.GeneratedSources.Length, actual.GeneratedSources.Length);
for (int i = 0; i < actual.GeneratedSources.Length; i++)
{
var diff = diffs.FirstOrDefault(p => p.index == i).replacement;
if (diff is null)
if (diffs?.Contains(i) != true)
{
var actualText = actual.GeneratedSources[i].SourceText.ToString();
Assert.True(expected.GeneratedSources[i].SourceText.ToString() == actualText, $"No diff supplied. But index {i} was:\r\n\r\n{actualText.Replace("\"", "\"\"")}");
AssertEx.EqualOrDiff(expected.GeneratedSources[i].SourceText.ToString(), actual.GeneratedSources[i].SourceText.ToString(), $"Index {i}");
}
else
{
AssertEx.EqualOrDiff(TrimChecksum(diff), TrimChecksum(actual.GeneratedSources[i].SourceText.ToString()));
Assert.True(expected.GeneratedSources[i].SourceText.ToString() != actual.GeneratedSources[i].SourceText.ToString(),
$"Sources at {i} expected to be different, but they are not");
VerifyOutput(baselineDirectory, touchedFiles, actual.GeneratedSources[i]);
}
}

var actualHostOutputs = actual.GetHostOutputs();
if (!expectedHost)
{
Assert.Empty(actualHostOutputs);
}
else
{
var expectedHostOutputs = expected.GetHostOutputs();
Assert.Equal(expectedHostOutputs.Length, actualHostOutputs.Length);
for (int i = 0; i < actualHostOutputs.Length; i++)
{
if (hostDiffs?.Contains(i) != true)
{
AssertEx.EqualOrDiff(expectedHostOutputs[i].Value, actualHostOutputs[i].Value, $"Host index {i}");
}
else
{
Assert.True(expectedHostOutputs[i].Value != actualHostOutputs[i].Value,
$"Host outputs at {i} expected to be different, but they are not");
VerifyHostOutput(baselineDirectory, touchedFiles, actualHostOutputs[i]);
}
}
}

DeleteUnusedBaselines(baselineDirectory, touchedFiles);

return actual;
}

private static void VerifyOutput(string baselineDirectory, HashSet<string> touchedFiles, GeneratedSourceResult source)
{
var baselinePath = Path.Join(baselineDirectory, source.HintName);
var sourceText = source.SourceText.ToString();
GenerateOutputBaseline(baselinePath, sourceText);
var baselineText = File.ReadAllText(baselinePath);
AssertEx.EqualOrDiff(TrimChecksum(baselineText), TrimChecksum(sourceText));
Assert.True(touchedFiles.Add(baselinePath));
}

private static void VerifyHostOutput(string baselineDirectory, HashSet<string> touchedFiles, (string Key, string Value) source)
{
var baselinePath = Path.Join(baselineDirectory, "__host__" + source.Key);
GenerateOutputBaseline(baselinePath, source.Value);
var baselineText = File.ReadAllText(baselinePath);
AssertEx.EqualOrDiff(baselineText, source.Value);
Assert.True(touchedFiles.Add(baselinePath));
}

private static string TrimChecksum(string text)
{
var trimmed = text.Trim('\r', '\n') // start and end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Counter.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0de17e526cd536d59072aa0e924e99111b16b97a"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Counter : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Counter</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Index.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b5db227a6aa2228c777b0771108b184b1fc5df3"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Index : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Hello world</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Counter.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0de17e526cd536d59072aa0e924e99111b16b97a"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Counter : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Counter</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Index.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b5db227a6aa2228c777b0771108b184b1fc5df3"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Index : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Hello world</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma checksum "Pages/Counter.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1ea7ff43d3a2eecc5502dd3771378e334a8fb068"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Counter : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h2>Counter</h2>\r\n");
__builder.OpenElement(1, "h3");
__builder.AddContent(2, "Current count: ");
#nullable restore
#line (3,21)-(3,26) 24 "Pages/Counter.razor"
__builder.AddContent(3, count);

#line default
#line hidden
#nullable disable
__builder.CloseElement();
__builder.AddMarkupContent(4, "\r\n");
__builder.AddMarkupContent(5, "<button @onclick=\"Click\">Click me</button>");
}
#pragma warning restore 1998
#nullable restore
#line 7 "Pages/Counter.razor"

private int count;

public void Click() => count++;

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Counter.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0de17e526cd536d59072aa0e924e99111b16b97a"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Counter : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Counter</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma checksum "Pages/Index.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b5db227a6aa2228c777b0771108b184b1fc5df3"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Pages
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class Index : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Hello world</h1>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Loading