Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable
Expand Down Expand Up @@ -189,9 +189,10 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin
{
// Result of generating declarations
codeDocument = projectEngine.ProcessDeclarationOnly(item);
Assert.Empty(codeDocument.GetRequiredImplCSharpDocument().Diagnostics);
var implCSharpDocument = codeDocument.GetRequiredCSharpDocument(declarationDocument: false);
Assert.Empty(implCSharpDocument.Diagnostics);

var syntaxTree = Parse(codeDocument.GetRequiredImplCSharpDocument().Text, path: item.FilePath);
var syntaxTree = Parse(implCSharpDocument.Text, path: item.FilePath);
AdditionalSyntaxTrees.Add(syntaxTree);
}

Expand All @@ -202,8 +203,9 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin
{
BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees),
CodeDocument = codeDocument,
Code = codeDocument.GetRequiredImplCSharpDocument().Text.ToString(),
Diagnostics = codeDocument.GetRequiredImplCSharpDocument().Diagnostics,
Code = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Text.ToString(),
DeclCode = codeDocument.GetCSharpDocument(declarationDocument: true)?.Text.ToString(),
Diagnostics = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Diagnostics,
};

// Result of doing 'temp' compilation
Expand All @@ -218,12 +220,16 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin
{
// Result of generating definition
codeDocument = projectEngine.Process(item);
Assert.Empty(codeDocument.GetRequiredImplCSharpDocument().Diagnostics);
var implCSharpDocument = codeDocument.GetRequiredCSharpDocument(declarationDocument: false);
Assert.Empty(implCSharpDocument.Diagnostics);

// Replace the 'declaration' syntax tree
var syntaxTree = Parse(codeDocument.GetRequiredImplCSharpDocument().Text, path: item.FilePath);
AdditionalSyntaxTrees.RemoveAll(st => st.FilePath == item.FilePath);
AdditionalSyntaxTrees.Add(syntaxTree);
AdditionalSyntaxTrees.RemoveAll(st => st.FilePath == item.FilePath || st.FilePath == item.FilePath + ".decl.g.cs");
AdditionalSyntaxTrees.Add(Parse(implCSharpDocument.Text, path: item.FilePath));
if (codeDocument.GetCSharpDocument(declarationDocument: true) is { } declCSharpDocument)
{
AdditionalSyntaxTrees.Add(Parse(declCSharpDocument.Text, path: item.FilePath + ".decl.g.cs"));
}
}

// Result of real code generation for the document under test
Expand All @@ -232,8 +238,9 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin
{
BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees),
CodeDocument = codeDocument,
Code = codeDocument.GetRequiredImplCSharpDocument().Text.ToString(),
Diagnostics = codeDocument.GetRequiredImplCSharpDocument().Diagnostics,
Code = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Text.ToString(),
DeclCode = codeDocument.GetCSharpDocument(declarationDocument: true)?.Text.ToString(),
Diagnostics = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Diagnostics,
};
}
else
Expand All @@ -251,18 +258,13 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin
{
BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees),
CodeDocument = codeDocument,
Code = codeDocument.GetRequiredImplCSharpDocument().Text.ToString(),
Diagnostics = codeDocument.GetRequiredImplCSharpDocument().Diagnostics,
Code = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Text.ToString(),
DeclCode = codeDocument.GetCSharpDocument(declarationDocument: true)?.Text.ToString(),
Diagnostics = codeDocument.GetRequiredCSharpDocument(declarationDocument: false).Diagnostics,
};
}
}

protected CompileToAssemblyResult CompileToAssembly(string cshtmlRelativePath, string cshtmlContent)
{
var cSharpResult = CompileToCSharp(cshtmlRelativePath, cshtmlContent);
return CompileToAssembly(cSharpResult);
}

protected static CompileToAssemblyResult CompileToAssembly(CompileToCSharpResult cSharpResult, bool throwOnFailure = true)
{
if (cSharpResult.Diagnostics.Any() && throwOnFailure)
Expand All @@ -271,10 +273,15 @@ protected static CompileToAssemblyResult CompileToAssembly(CompileToCSharpResult
throw new InvalidOperationException($"Aborting compilation to assembly because RazorCompiler returned nonempty diagnostics: {diagnosticsLog}");
}

var syntaxTrees = new[]
var primaryPath = cSharpResult.CodeDocument.Source.FilePath ?? string.Empty;
var syntaxTrees = new List<SyntaxTree>
{
Parse(cSharpResult.Code),
Parse(cSharpResult.Code, path: primaryPath),
};
if (cSharpResult.DeclCode is { } declCode)
{
syntaxTrees.Add(Parse(declCode, path: primaryPath + ".decl.g.cs"));
}

var compilation = cSharpResult.BaseCompilation.AddSyntaxTrees(syntaxTrees);

Expand Down Expand Up @@ -318,35 +325,20 @@ protected static CSharpSyntaxTree Parse(string text, string path = null)
return Parse(SourceText.From(text, Encoding.UTF8), path);
}

protected static string FullTypeName<T>() => typeof(T).FullName.Replace('+', '.');

protected static void AssertSourceEquals(string expected, CompileToCSharpResult generated)
{
// Normalize the paths inside the expected result to match the OS paths
if (!PlatformInformation.IsWindows)
{
var windowsPath = Path.Combine(ArbitraryWindowsPath, generated.CodeDocument.Source.RelativePath).Replace('/', '\\');
expected = expected.Replace(windowsPath, generated.CodeDocument.Source.FilePath);
}

expected = expected.Trim();
Assert.Equal(expected, generated.Code.Trim(), ignoreLineEndingDifferences: true);
}

protected class CompileToCSharpResult
{
// A compilation that can be used *with* this code to compile an assembly
public Compilation BaseCompilation { get; set; }
public RazorCodeDocument CodeDocument { get; set; }
public string Code { get; set; }
public string DeclCode { get; set; }
public IEnumerable<RazorDiagnostic> Diagnostics { get; set; }
}

protected class CompileToAssemblyResult
{
public Assembly Assembly { get; set; }
public Compilation Compilation { get; set; }
public string VerboseLog { get; set; }
public IEnumerable<Diagnostic> Diagnostics { get; set; }
}

Expand Down

This file was deleted.

Loading