Skip to content

Commit 1b5b814

Browse files
committed
Support migrating from MSTest v4
1 parent 56953cc commit 1b5b814

File tree

11 files changed

+2216
-1027
lines changed

11 files changed

+2216
-1027
lines changed

src/AwesomeAssertions.Analyzers.TestUtils/PackageReference.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ private PackageReference(string name, string version, string path)
1818
_allDependencies.Add(this);
1919
}
2020

21-
public static PackageReference AwesomeAssertions_latest { get; } = new("AwesomeAssertions", "9.0.0", "lib/netstandard2.0/");
22-
public static PackageReference MSTestTestFramework_3_1_1 { get; } = new("MSTest.TestFramework", "3.1.1", "lib/netstandard2.0/");
21+
public static PackageReference AwesomeAssertions_latest { get; } = new("AwesomeAssertions", "9.3.0", "lib/netstandard2.0/");
22+
public static PackageReference MSTestTestFramework_3_11_0 { get; } = new("MSTest.TestFramework", "3.11.0", "lib/netstandard2.0/");
23+
public static PackageReference MSTestTestFramework_4_0_1 { get; } = new("MSTest.TestFramework", "4.0.1", "lib/netstandard2.0/");
2324
public static PackageReference XunitAssert_2_5_1 { get; } = new("xunit.assert", "2.5.1", "lib/netstandard1.1/");
2425
public static PackageReference Xunit3Assert_3_0_0 { get; } = new("xunit.v3.assert", "3.0.0", "lib/netstandard2.0/");
2526
public static PackageReference Nunit_3_14_0 { get; } = new("NUnit", "3.14.0", "lib/netstandard2.0/");

src/AwesomeAssertions.Analyzers.Tests/AwesomeAssertions.Analyzers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="coverlet.msbuild" >
10+
<PackageReference Include="coverlet.msbuild">
1111
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1212
<PrivateAssets>all</PrivateAssets>
1313
</PackageReference>

src/AwesomeAssertions.Analyzers.Tests/TestAttributes.cs

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,64 @@ public class NotImplementedAttribute : TestCategoryBaseAttribute
1111
{
1212
public string Reason { get; set; }
1313

14-
public override IList<string> TestCategories => new[] { "NotImplemented" };
14+
public override IList<string> TestCategories { get; } = ["NotImplemented"];
1515
}
1616

1717
[AttributeUsage(AttributeTargets.Method)]
1818
public class ImplementedAttribute : TestCategoryBaseAttribute
1919
{
2020
public string Reason { get; set; }
2121

22-
public override IList<string> TestCategories => new[] { "Completed" };
22+
public override IList<string> TestCategories { get; } = ["Completed"];
2323
}
2424

2525
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
26-
public class AssertionDiagnosticAttribute : Attribute, ITestDataSource
26+
public abstract class AssertionDiagnosticBaseAttribute(string assertion, params string[] additionalParameters) : Attribute, ITestDataSource
2727
{
28-
public string Assertion { get; }
28+
public string Assertion { get; } = assertion;
2929

30-
public string[] AdditionalParameters { get; }
30+
public string[] AdditionalParameters { get; } = additionalParameters ?? [];
3131

32-
public AssertionDiagnosticAttribute(string assertion, params string[] additionalParameters)
33-
{
34-
Assertion = assertion;
35-
AdditionalParameters = additionalParameters ?? Array.Empty<string>();
36-
}
32+
private protected abstract IEnumerable<string> GetTestCases();
3733

3834
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
3935
{
40-
foreach (var assertion in TestCasesInputUtils.GetTestCases(Assertion))
36+
foreach (var assertion in GetTestCases())
4137
{
4238
yield return new object[] { assertion }.Concat(AdditionalParameters).ToArray();
4339
}
4440
}
4541

46-
public string GetDisplayName(MethodInfo methodInfo, object[] data) => $"{methodInfo.Name}(\"{string.Join("\", \"", data)}\")";
42+
public string GetDisplayName(MethodInfo methodInfo, object[] data)
43+
=> $"{methodInfo.Name}(\"{string.Join("\", \"", data)}\")";
4744
}
4845

49-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
50-
public class IgnoreAssertionDiagnosticAttribute : Attribute
46+
public sealed class AssertionDiagnosticAttribute(string assertion, params string[] additionalParameters)
47+
: AssertionDiagnosticBaseAttribute(assertion, additionalParameters)
5148
{
52-
public string Assertion { get; }
49+
private protected override IEnumerable<string> GetTestCases()
50+
=> TestCasesInputUtils.GetTestCases(Assertion, MessageFormat.Simple | MessageFormat.Because | MessageFormat.FormattedBecause);
51+
}
5352

54-
public IgnoreAssertionDiagnosticAttribute(string assertion) => Assertion = assertion;
53+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
54+
public class IgnoreAssertionDiagnosticAttribute(string assertion) : Attribute
55+
{
56+
public string Assertion { get; } = assertion;
5557
}
5658

5759
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
58-
public class AssertionCodeFixAttribute : Attribute, ITestDataSource
60+
public abstract class AssertionCodeFixBaseAttribute(string oldAssertion, string newAssertion, params string[] additionalParameters)
61+
: Attribute, ITestDataSource
5962
{
60-
public string OldAssertion { get; }
61-
public string NewAssertion { get; }
62-
public string[] AdditionalParameters { get; }
63+
public string OldAssertion { get; } = oldAssertion;
64+
public string NewAssertion { get; } = newAssertion;
65+
public string[] AdditionalParameters { get; } = additionalParameters ?? [];
6366

64-
public AssertionCodeFixAttribute(string oldAssertion, string newAssertion, params string[] additionalParameters)
65-
{
66-
OldAssertion = oldAssertion;
67-
NewAssertion = newAssertion;
68-
AdditionalParameters = additionalParameters ?? Array.Empty<string>();
69-
}
67+
private protected abstract IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases();
7068

7169
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
7270
{
73-
foreach (var (oldAssertion, newAssertion) in TestCasesInputUtils.GetTestCases(OldAssertion, NewAssertion))
71+
foreach (var (oldAssertion, newAssertion) in GetTestCases())
7472
{
7573
yield return new object[] { oldAssertion, newAssertion }.Concat(AdditionalParameters).ToArray();
7674
}
@@ -87,6 +85,13 @@ public string GetDisplayName(MethodInfo methodInfo, object[] data)
8785
}
8886
}
8987

88+
public class AssertionCodeFixAttribute(string oldAssertion, string newAssertion, params string[] additionalParameters)
89+
: AssertionCodeFixBaseAttribute(oldAssertion, newAssertion, additionalParameters)
90+
{
91+
private protected override IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases()
92+
=> TestCasesInputUtils.GetTestCases(OldAssertion, NewAssertion);
93+
}
94+
9095
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
9196
public class IgnoreAssertionCodeFixAttribute : Attribute
9297
{
@@ -128,34 +133,63 @@ public IgnoreAssertionMethodCodeFixAttribute(string methodArguments, string oldA
128133
=> (MethodArguments, OldAssertion, NewAssertion) = (methodArguments, oldAssertion, newAssertion);
129134
}
130135

131-
public static class TestCasesInputUtils
136+
[Flags]
137+
internal enum MessageFormat
132138
{
133-
private static readonly string Empty = string.Empty;
134-
private static readonly string Because = "\"because it's possible\"";
135-
private static readonly string FormattedBecause = "\"because message with {0} placeholders {1} at {2}\", 3, \"is awesome\", DateTime.Now.Add(2.Seconds())";
136-
public static IEnumerable<string> GetTestCases(string assertion)
139+
Simple = 1,
140+
Because = 2,
141+
FormattedBecause = 4,
142+
InterpolatedBecause = 8,
143+
Default = Simple | Because | FormattedBecause,
144+
}
145+
146+
internal static class TestCasesInputUtils
147+
{
148+
private const string Empty = "";
149+
private const string Because = "\"because it's possible\"";
150+
private const string FormattedBecause = "\"because message with {0} placeholders {1} at {2}\", 3, \"is awesome\", DateTime.Now.Add(2.Seconds())";
151+
private const string InterpolatedBecause = "$\"because message with {3} placeholders {DateTime.Now}\"";
152+
153+
public static IEnumerable<string> GetTestCases(string assertion, MessageFormat messageFormat = MessageFormat.Default)
137154
{
138155
if (!assertion.Contains("{0}"))
139156
{
140157
yield return assertion;
141158
yield break;
142159
}
143160

144-
yield return SafeFormat(assertion, Empty);
145-
yield return SafeFormat(assertion, Because);
146-
yield return SafeFormat(assertion, FormattedBecause);
161+
if (messageFormat.HasFlag(MessageFormat.Simple))
162+
yield return SafeFormat(assertion, Empty);
163+
164+
if (messageFormat.HasFlag(MessageFormat.Because))
165+
yield return SafeFormat(assertion, Because);
166+
167+
if (messageFormat.HasFlag(MessageFormat.FormattedBecause))
168+
yield return SafeFormat(assertion, FormattedBecause);
169+
170+
if (messageFormat.HasFlag(MessageFormat.InterpolatedBecause))
171+
yield return SafeFormat(assertion, InterpolatedBecause);
147172
}
148-
public static IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases(string oldAssertion, string newAssertion)
173+
174+
public static IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases(string oldAssertion, string newAssertion, MessageFormat messageFormat = MessageFormat.Default)
149175
{
150176
if (!oldAssertion.Contains("{0}") && !newAssertion.Contains("{0}"))
151177
{
152178
yield return (oldAssertion, newAssertion);
153179
yield break;
154180
}
155181

156-
yield return (SafeFormat(oldAssertion, Empty), SafeFormat(newAssertion, Empty));
157-
yield return (SafeFormat(oldAssertion, Because), SafeFormat(newAssertion, Because));
158-
yield return (SafeFormat(oldAssertion, FormattedBecause), SafeFormat(newAssertion, FormattedBecause));
182+
if (messageFormat.HasFlag(MessageFormat.Simple))
183+
yield return (SafeFormat(oldAssertion, Empty), SafeFormat(newAssertion, Empty));
184+
185+
if (messageFormat.HasFlag(MessageFormat.Because))
186+
yield return (SafeFormat(oldAssertion, Because), SafeFormat(newAssertion, Because));
187+
188+
if (messageFormat.HasFlag(MessageFormat.FormattedBecause))
189+
yield return (SafeFormat(oldAssertion, FormattedBecause), SafeFormat(newAssertion, FormattedBecause));
190+
191+
if (messageFormat.HasFlag(MessageFormat.InterpolatedBecause))
192+
yield return (SafeFormat(oldAssertion, InterpolatedBecause), SafeFormat(newAssertion, InterpolatedBecause));
159193
}
160194

161195
/// <summary>

src/AwesomeAssertions.Analyzers.Tests/Tips/AwesomeAssertionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ public void ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInAnyScope_
7676
[TestMethod]
7777
[Implemented]
7878
public void ShouldAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsNotInScope_ForMsTest()
79-
=> ShouldAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsNotInScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_1_1);
79+
=> ShouldAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsNotInScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_11_0);
8080

8181
[TestMethod]
8282
[Implemented]
8383
public void ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInGlobalScope_ForMsTest()
84-
=> ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInGlobalScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_1_1);
84+
=> ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInGlobalScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_11_0);
8585

8686
[TestMethod]
8787
[Implemented]
8888
public void ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInAnyScope_ForMsTest()
89-
=> ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInAnyScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_1_1);
89+
=> ShouldNotAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsInAnyScope<MsTestCodeFixProvider>("IsTrue", "using Microsoft.VisualStudio.TestTools.UnitTesting;", PackageReference.MSTestTestFramework_3_11_0);
9090

9191
private void ShouldAddAwesomeAssertionsUsing_WhenAwesomeAssertionIsNotInScope<TCodeFixProvider>(string assertTrue, string usingDirective, PackageReference testingLibraryReference) where TCodeFixProvider : CodeFixProvider, new()
9292
{

0 commit comments

Comments
 (0)