Skip to content

Commit ea678d7

Browse files
authored
MSTest Asserts (#75)
* first mstest analyzer * added Assert.IsFalse * cleanup * progress * added helper for mstest * more analyzers * wip * fix conflicts * add support for Assert.ThrowsException & Assert.ThrowsExceptionAsync * save * added support for more AssertEqual cases * support AssertAreNotEqual * completed mstest Assert scenarios * cleanup rule messages * add CollectionAssertAllItemsAreInstancesOfType * CollectionAssert fixes * cleanup usings * cleanups * fix parent class for mstest Assert * add CollectionAssert AreEqual / AreNotEqual * fix parent class for CollectionAssert * fix parent class for StringAssert * add CollectionAssert AreEquivalent / AreNotEquivalent * add CollectionAssertAllItemsAreNotNull * add CollectionAssert.AllItemsAreUnique * add CollectionAssert.Contains * add CollectionAssert.DoesNotContain * fix tests * add CollectionAssert.IsSubsetOf / CollectionAssert.IsNotSubsetOf * add StringAssert methods * fix tests
1 parent 1f7428e commit ea678d7

File tree

54 files changed

+2268
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2268
-32
lines changed

src/FluentAssertions.Analyzers.Tests/DiagnosticVerifier.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ static DiagnosticVerifier()
3434
typeof(Compilation), // Microsoft.CodeAnalysis
3535
typeof(AssertionScope), // FluentAssertions.Core
3636
typeof(AssertionExtensions), // FluentAssertions
37+
typeof(Microsoft.VisualStudio.TestTools.UnitTesting.Assert) // MsTest
3738
}.Select(type => type.GetTypeInfo().Assembly.Location)
39+
.Append(GetSystemAssemblyPathByName("System.Globalization.dll"))
40+
.Append(GetSystemAssemblyPathByName("System.Text.RegularExpressions.dll"))
3841
.Append(GetSystemAssemblyPathByName("System.Runtime.Extensions.dll"))
3942
.Append(GetSystemAssemblyPathByName("System.Data.Common.dll"))
4043
.Append(GetSystemAssemblyPathByName("System.Threading.Tasks.dll"))
@@ -641,7 +644,7 @@ private static string FormatDiagnostics(DiagnosticAnalyzer[] analyzers, params D
641644

642645
private static DiagnosticAnalyzer[] CreateAllAnalyzers()
643646
{
644-
var assembly = typeof(Constants).Assembly;
647+
var assembly = typeof(Constants).Assembly;
645648
var analyzersTypes = assembly.GetTypes()
646649
.Where(type => !type.IsAbstract && typeof(DiagnosticAnalyzer).IsAssignableFrom(type));
647650
var analyzers = analyzersTypes.Select(type => (DiagnosticAnalyzer)Activator.CreateInstance(type));

src/FluentAssertions.Analyzers.Tests/GenerateCode.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ public static string GenericIListExpressionBodyAssertion(string assertion) => Ge
175175
.AppendLine("}")
176176
.ToString();
177177

178+
public static string MsTestAssertion(string methodArguments, string assertion) => new StringBuilder()
179+
.AppendLine("using System;")
180+
.AppendLine("using FluentAssertions;")
181+
.AppendLine("using FluentAssertions.Extensions;")
182+
.AppendLine("using Microsoft.VisualStudio.TestTools.UnitTesting;")
183+
.AppendLine("using System.Threading.Tasks;")
184+
.AppendLine("namespace TestNamespace")
185+
.AppendLine("{")
186+
.AppendLine(" class TestClass")
187+
.AppendLine(" {")
188+
.AppendLine($" void TestMethod({methodArguments})")
189+
.AppendLine(" {")
190+
.AppendLine($" {assertion}")
191+
.AppendLine(" }")
192+
.AppendLine(" }")
193+
.AppendMainMethod()
194+
.AppendLine("}")
195+
.ToString();
196+
178197
private static StringBuilder AppendMainMethod(this StringBuilder builder) => builder
179198
.AppendLine(" class Program")
180199
.AppendLine(" {")

src/FluentAssertions.Analyzers.Tests/Tips/MsTestTests.cs

Lines changed: 545 additions & 0 deletions
Large diffs are not rendered by default.

src/FluentAssertions.Analyzers/Constants.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ public static class Exceptions
8383
public const string ExceptionShouldThrowWithMessage = nameof(ExceptionShouldThrowWithMessage);
8484
public const string ExceptionShouldThrowWithInnerException = nameof(ExceptionShouldThrowWithInnerException);
8585
}
86+
87+
public static class MsTest
88+
{
89+
public const string AssertIsTrue = nameof(AssertIsTrue);
90+
public const string AssertIsFalse = nameof(AssertIsFalse);
91+
public const string AssertIsNotNull = nameof(AssertIsNotNull);
92+
public const string AssertIsNull = nameof(AssertIsNull);
93+
public const string AssertIsInstanceOfType = nameof(AssertIsInstanceOfType);
94+
public const string AssertIsNotInstanceOfType = nameof(AssertIsNotInstanceOfType);
95+
public const string AssertAreEqual = nameof(AssertAreEqual);
96+
public const string AssertAreNotEqual = nameof(AssertAreNotEqual);
97+
public const string AssertAreSame = nameof(AssertAreSame);
98+
public const string AssertAreNotSame = nameof(AssertAreNotSame);
99+
public const string AssertThrowsException = nameof(AssertThrowsException);
100+
public const string AssertThrowsExceptionAsync = nameof(AssertThrowsExceptionAsync);
101+
public const string StringAssertContains = nameof(StringAssertContains);
102+
public const string StringAssertStartsWith = nameof(StringAssertStartsWith);
103+
public const string StringAssertEndsWith = nameof(StringAssertEndsWith);
104+
public const string StringAssertMatches = nameof(StringAssertMatches);
105+
public const string StringAssertDoesNotMatch = nameof(StringAssertDoesNotMatch);
106+
public const string CollectionAssertAllItemsAreInstancesOfType = nameof(CollectionAssertAllItemsAreInstancesOfType);
107+
public const string CollectionAssertAreEqual = nameof(CollectionAssertAreEqual);
108+
public const string CollectionAssertAreNotEqual = nameof(CollectionAssertAreNotEqual);
109+
public const string CollectionAssertAreEquivalent = nameof(CollectionAssertAreEquivalent);
110+
public const string CollectionAssertAreNotEquivalent = nameof(CollectionAssertAreNotEquivalent);
111+
public const string CollectionAssertAllItemsAreNotNull = nameof(CollectionAssertAllItemsAreNotNull);
112+
public const string CollectionAssertAllItemsAreUnique = nameof(CollectionAssertAllItemsAreUnique);
113+
public const string CollectionAssertContains = nameof(CollectionAssertContains);
114+
public const string CollectionAssertDoesNotContain = nameof(CollectionAssertDoesNotContain);
115+
public const string CollectionAssertIsSubsetOf = nameof(CollectionAssertIsSubsetOf);
116+
public const string CollectionAssertIsNotSubsetOf = nameof(CollectionAssertIsNotSubsetOf);
117+
}
86118
}
87119

88120
public static class CodeSmell

src/FluentAssertions.Analyzers/FluentAssertions.Analyzers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<PropertyGroup>
1111
<RootNamespace>FluentAssertions.Analyzers</RootNamespace>
12+
<LangVersion>8.0</LangVersion>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldBeEmpty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ShouldHaveCount0SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
4040
{
4141
}
4242

43-
private static bool HaveCountArgumentsValidator(SeparatedSyntaxList<ArgumentSyntax> arguments)
43+
private static bool HaveCountArgumentsValidator(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel)
4444
{
4545
if (!arguments.Any()) return false;
4646

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldContainSingle.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Composition;
9-
using System.Linq;
109

1110
namespace FluentAssertions.Analyzers
1211
{

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldEqualOtherCollectionByComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public SelectShouldEqualOtherCollectionSelectSyntaxVisitor()
3434
{
3535
}
3636

37-
private static bool MathodContainingArgumentInvokingLambda(SeparatedSyntaxList<ArgumentSyntax> arguments)
37+
private static bool MathodContainingArgumentInvokingLambda(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel)
3838
{
3939
if (!arguments.Any()) return false;
4040

4141
return arguments[0].Expression is InvocationExpressionSyntax invocation
42-
&& MemberValidator.MethodContainingLambdaPredicate(invocation.ArgumentList.Arguments);
42+
&& MemberValidator.MethodContainingLambdaPredicate(invocation.ArgumentList.Arguments, semanticModel);
4343
}
4444
}
4545
}

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveElementAt.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Composition;
9-
using System.Linq;
109

1110
namespace FluentAssertions.Analyzers
1211
{

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveSameCount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ShouldHaveCountOtherCollectionCountSyntaxVisitor : FluentAssertions
3232
{
3333
}
3434

35-
private static bool HasArgumentInvokingCountMethod(SeparatedSyntaxList<ArgumentSyntax> arguments)
35+
private static bool HasArgumentInvokingCountMethod(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel)
3636
{
3737
if (!arguments.Any()) return false;
3838

0 commit comments

Comments
 (0)