Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
upd - Updated unit tests to use MSTest and .NET 8.0
Browse files Browse the repository at this point in the history
---

We've not only retargeted the unit test projects to use .NET 8.0, since .NET 7.0 will be EOL on May 14th, but we've also used the new MSTest unit test framework to replace XUnit.

---

Type: upd
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 17, 2024
1 parent bafd1ec commit 85c0397
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 119 deletions.
21 changes: 10 additions & 11 deletions Figletize.Generator.Tests/Figletize.Generator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\aptivi_snk.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MSTest" Version="3.3.1" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
94 changes: 46 additions & 48 deletions Figletize.Generator.Tests/FigletizeSourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
using Xunit.Sdk;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace Figletize.Generator.Tests;

[TestClass]
public class FigletizeSourceGeneratorTests
{
private readonly ImmutableArray<MetadataReference> _references;
Expand Down Expand Up @@ -44,7 +45,7 @@ static IEnumerable<MetadataReference> GetReferences()
}
}

[Fact]
[TestMethod]
public void SimpleCase_Class()
{
string source =
Expand Down Expand Up @@ -77,7 +78,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected);
}

[Fact]
[TestMethod]
public void SimpleCase_StaticClass()
{
string source =
Expand Down Expand Up @@ -110,7 +111,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected);
}

[Fact]
[TestMethod]
public void SimpleCase_GlobalNamespace()
{
string source = """
Expand All @@ -137,7 +138,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected);
}

[Fact]
[TestMethod]
public void MultipleStrings()
{
string source =
Expand Down Expand Up @@ -182,7 +183,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected);
}

[Fact]
[TestMethod]
public void SimpleCase_TwoClasses_SameNamespace()
{
string source =
Expand Down Expand Up @@ -236,7 +237,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected1, expected2);
}

[Fact]
[TestMethod]
public void SimpleCase_TwoClasses_DifferentNamespace()
{
string source =
Expand Down Expand Up @@ -293,7 +294,7 @@ 7 77 77 77 7 7 77 77 77 77 7
ValidateOutput(source, expected1, expected2);
}

[Fact]
[TestMethod]
public void InvalidFontName()
{
string source =
Expand All @@ -309,13 +310,13 @@ internal static partial class DemoUsage

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.UnknownFontNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("A font with name 'unknown-font' was not found", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.UnknownFontNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("A font with name 'unknown-font' was not found");
}

[Fact]
[TestMethod]
public void InvalidMemberName()
{
string source =
Expand All @@ -331,13 +332,13 @@ internal static partial class DemoUsage

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.InvalidMemberNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("The string 'With Space' is not a valid member name", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.InvalidMemberNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("The string 'With Space' is not a valid member name");
}

[Fact]
[TestMethod]
public void DuplicateMemberName_SamePart()
{
string source =
Expand All @@ -354,10 +355,10 @@ internal static partial class DemoUsage

var (compilation, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("Member 'Foo' has already been declared", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Member 'Foo' has already been declared");

string expected =
$$"""
Expand All @@ -376,10 +377,10 @@ 7 77 77 7
}
""";

Assert.Equal(expected, compilation.SyntaxTrees.Last().ToString(), NewlineIgnoreComparer.Instance);
compilation.SyntaxTrees.Last().ToString().ShouldBe(expected, NewlineIgnoreComparer.Instance);
}

[Fact]
[TestMethod]
public void MemberAlreadyExists_SamePart()
{
string source =
Expand All @@ -396,13 +397,13 @@ public static void Foo() {}

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("Member 'Foo' has already been declared", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Member 'Foo' has already been declared");
}

[Fact]
[TestMethod]
public void MemberAlreadyExists_DifferentPart()
{
string source =
Expand All @@ -423,13 +424,13 @@ public static void Foo() {}

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("Member 'Foo' has already been declared", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Member 'Foo' has already been declared");
}

[Fact]
[TestMethod]
public void DuplicateMemberName_DifferentParts()
{
string source =
Expand All @@ -450,10 +451,10 @@ internal static partial class DemoUsage

var (compilation, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
Assert.Equal("Member 'Foo' has already been declared", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.DuplicateMemberNameDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Member 'Foo' has already been declared");

string expected =
$$"""
Expand All @@ -472,10 +473,10 @@ 7 77 77 7
}
""";

Assert.Equal(expected, compilation.SyntaxTrees.Last().ToString(), NewlineIgnoreComparer.Instance);
compilation.SyntaxTrees.Last().ToString().ShouldBe(expected, NewlineIgnoreComparer.Instance);
}

[Fact]
[TestMethod]
public void TypeIsNotPartial()
{
string source =
Expand All @@ -491,13 +492,13 @@ internal class DemoUsage

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.TypeIsNotPartialDiagnostic, diagnostic.Descriptor);
Assert.Equal("Type 'DemoUsage' must be partial", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.TypeIsNotPartialDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Type 'DemoUsage' must be partial");
}

[Fact]
[TestMethod]
public void NestedTypeIsNotSupported()
{
string source =
Expand All @@ -516,10 +517,10 @@ internal partial class Inner

var (_, diagnostics) = RunGenerator(source);

var diagnostic = Assert.Single(diagnostics);
var diagnostic = diagnostics.ShouldHaveSingleItem();

Assert.Same(FigletizeSourceGenerator.NestedTypeIsNotSupportedDiagnostic, diagnostic.Descriptor);
Assert.Equal("Unable to generate Figletize text for nested type 'Inner'. Generation is only supported for non-nested types.", diagnostic.GetMessage());
Assert.AreSame(FigletizeSourceGenerator.NestedTypeIsNotSupportedDiagnostic, diagnostic.Descriptor);
diagnostic.GetMessage().ShouldBe("Unable to generate Figletize text for nested type 'Inner'. Generation is only supported for non-nested types.");
}

private void ValidateOutput(string source, params string[] outputs)
Expand All @@ -528,10 +529,7 @@ private void ValidateOutput(string source, params string[] outputs)

ValidateNoErrors(diagnostics);

Assert.Equal(
new[] { source, FigletizeSourceGenerator.AttributeSource }.Concat(outputs),
compilation.SyntaxTrees.Select(tree => tree.ToString()),
NewlineIgnoreComparer.Instance);
compilation.SyntaxTrees.Select(tree => tree.ToString()).ShouldBe(new[] { source, FigletizeSourceGenerator.AttributeSource }.Concat(outputs), NewlineIgnoreComparer.Instance);
}

private (Compilation Compilation, ImmutableArray<Diagnostic> Diagnostics) RunGenerator(string source)
Expand Down Expand Up @@ -560,9 +558,9 @@ private static void ValidateNoErrors(ImmutableArray<Diagnostic> diagnostics)
{
var errors = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToList();

if (errors.Any())
if (errors.Count != 0)
{
throw new XunitException(
throw new Exception(
string.Join(
Environment.NewLine,
errors.Select(error => error.GetMessage())));
Expand Down
20 changes: 13 additions & 7 deletions Figletize.Tests/Figletize.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\aptivi_snk.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MSTest" Version="3.3.1" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Figletize\Figletize.csproj" />
</ItemGroup>
Expand Down
13 changes: 0 additions & 13 deletions Figletize.Tests/FigletizeFontParserTest.cs

This file was deleted.

Loading

0 comments on commit 85c0397

Please sign in to comment.