Skip to content

Commit 4c82975

Browse files
committed
docs: mstest support for ThrowsException/Async
1 parent 2e0640f commit 4c82975

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

docs/MsTestAnalyzer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ Action action = ThrowException;
633633
// old assertion:
634634
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
635635
Exception Message: Operation is not valid due to the current state of the object.
636-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|73_0() in D:\Repos\fluentassertions\fluentassertions.analyzers\src\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs\MsTestAnalyzerTests.cs:line 883
636+
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|73_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 883
637637
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException[T](Action action, String message, Object[] parameters) */
638638

639639
// new assertion:
@@ -663,7 +663,7 @@ Func<Task> action = ThrowExceptionAsync;
663663
// old assertion:
664664
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
665665
Exception Message: Operation is not valid due to the current state of the object.
666-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|76_0() in D:\Repos\fluentassertions\fluentassertions.analyzers\src\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs\MsTestAnalyzerTests.cs:line 919
666+
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|76_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 919
667667
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync[T](Func`1 action, String message, Object[] parameters) */
668668

669669
// new assertion:

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator/DocsGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public async Task Execute()
129129
var bodyLines = testWithFailureNewAssertion.Body.ToFullString().Split(Environment.NewLine)[2..^2];
130130
var paddingToRemove = bodyLines[0].IndexOf(bodyLines[0].TrimStart());
131131

132-
var oldAssertions = testWithFailureOldAssertions.Select((x, i) => x.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(oldAssertionComment[i].Span) > 0).ToString().TrimStart() + " /* fail message: " + exceptionMessageLinesOldAssertions[i] + " */");
132+
var oldAssertions = testWithFailureOldAssertions.Select((x, i) => x.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(oldAssertionComment[i].Span) > 0).ToString().TrimStart() + " /* fail message: " + FluentAssertionAnalyzerDocsUtils.ReplaceStackTrace(exceptionMessageLinesOldAssertions[i]) + " */");
133133
var newAssertion = testWithFailureNewAssertion.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(newAssertionComment.Span) > 0).ToString().TrimStart() + " /* fail message: " + exceptionMessageLinesNewAssertion + " */";
134134

135135
var arrange = bodyLines.TakeWhile(x => !string.IsNullOrEmpty(x))
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
34
using Microsoft.Build.Locator;
45
using Microsoft.CodeAnalysis;
@@ -8,14 +9,30 @@ namespace FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator;
89

910
public static class FluentAssertionAnalyzerDocsUtils
1011
{
12+
private static readonly string _fluentAssertionsAnalyzersDocs = "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs";
13+
private static readonly string _fluentAssertionsAnalyzersDocsDirectory = Path.Combine("..", _fluentAssertionsAnalyzersDocs);
14+
private static readonly string _fluentAssertionsAnalyzersProjectPath = Path.Combine(_fluentAssertionsAnalyzersDocsDirectory, _fluentAssertionsAnalyzersDocs + ".csproj");
15+
private static readonly char _unixDirectorySeparator = '/';
16+
1117
public static async Task<Compilation> GetFluentAssertionAnalyzerDocsCompilation()
1218
{
1319
MSBuildLocator.RegisterDefaults();
1420

1521
using var workspace = MSBuildWorkspace.Create();
1622

17-
var project = await workspace.OpenProjectAsync(Path.Combine("..", "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs", "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.csproj"));
23+
var project = await workspace.OpenProjectAsync(_fluentAssertionsAnalyzersProjectPath);
1824

1925
return await project.GetCompilationAsync();
2026
}
27+
28+
public static string ReplaceStackTrace(string messageIncludingStacktrace)
29+
{
30+
var currentFullPath = Path.GetFullPath(_fluentAssertionsAnalyzersDocsDirectory) + Path.DirectorySeparatorChar;
31+
var repoRootIndex = currentFullPath.LastIndexOf(Path.DirectorySeparatorChar + "fluentassertions.analyzers" + Path.DirectorySeparatorChar, StringComparison.Ordinal);
32+
var unixFullPath = currentFullPath
33+
.Replace(currentFullPath.Substring(0, repoRootIndex), "/Users/runner/work")
34+
.Replace(Path.DirectorySeparatorChar, _unixDirectorySeparator);
35+
36+
return messageIncludingStacktrace.Replace(currentFullPath, unixFullPath);
37+
}
2138
}

0 commit comments

Comments
 (0)