Better compiler assertions
This is leveraging Microsoft.CodeAnalysis.Testing to do the assertions.
MODIFY
src/Chapter06.Tests/Chapter06.Tests.csproj
src/Chapter06.Tests/Chapter06.Tests.csproj
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Update="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Update="MSTest.TestFramework" Version="2.2.10" />
+ <PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
+ <PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.1" />
</ItemGroup>
</Project>

MODIFY
src/Chapter06.Tests/Listing06.28.Tests.cs
src/Chapter06.Tests/Listing06.28.Tests.cs
@@ -7,12 +7,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28.Tests
public class ProgramTests
{
[TestMethod]
- [Ignore]
public async Task UnassignedVariableThrowsError()
{
- await CompilerAssert.Compile2Async(
+ await CompilerAssert2.Compile2Async(
new string[] { "Listing06.28.DefaultConstructorNoLongerAvailable.cs", "Listing06.26.DefiningAConstructor.cs" },
- new string[]{ "CS0246" });
+ new string[]{ "CS7036" });
}
}
}
\ No newline at end of file

MODIFY
src/Chapter06/Listing06.28.DefaultConstructorNoLongerAvailable.cs
src/Chapter06/Listing06.28.DefaultConstructorNoLongerAvailable.cs
@@ -12,7 +12,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28
{
Employee employee;
- asdfasdfasdfdsaf
+ //asdfasdfasdfdsaf
//#if INCLUDE
#region HIGHLIGHT
// ERROR: No overload because method 'Employee'

MODIFY
src/Shared/CompilerAssert.cs
src/Shared/CompilerAssert.cs
@@ -10,9 +10,7 @@ using Microsoft.CodeAnalysis.Text;
namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
{
-
-
- readonly public struct CompileError
+ public readonly struct CompileError
{
public static Dictionary<string, string?> CompilerErrorMessages { get; } = new Dictionary<string, string?>()
{
@@ -82,15 +80,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
CompileError lhs, CompileError rhs) =>
!lhs.Equals(rhs);
}
- static public class CompilerAssert
+ public static class CompilerAssert
{
-
- private static readonly CSharpCompilationOptions DefaultCompilationOptions =
- new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
- .WithOverflowChecks(true).WithOptimizationLevel(OptimizationLevel.Release);
- //.WithUsings(DefaultNamespaces);
-
-
public static async Task Compile2Async(
string fileName,
string[] errorIds,
@@ -176,14 +167,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
(string result, string item) =>
result += $"\n{item}");
- return await ExpectErrorsAsync(sourceCode, diagnostics);
+ return ExpectErrorsAsync(sourceCode, diagnostics);
}
- async static public Task<CompileError[]> ExpectErrorsAsync(
+ static public CompileError[] ExpectErrorsAsync(
string sourceCode, params CompileError[] diagnostics)
{
ArgumentNullException.ThrowIfNull(diagnostics);
- CompileError[] actualCompileErrors = await CompileAsync(sourceCode);
+ CompileError[] actualCompileErrors = CompileAsync(sourceCode);
if (diagnostics.Length > 0)
{
@@ -221,7 +212,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
return actualCompileErrors;
}
- public static async Task<CompileError[]> CompileAsync(
+ public static CompileError[] CompileAsync(
string sourceCode)
{
CompileError[] actualCompileErrors = Array.Empty<CompileError>();

Better compiler assertions

This is leveraging Microsoft.CodeAnalysis.Testing to do the assertions.