From 92c1450d04ea73ff52079d32915059f6293eb401 Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Fri, 10 Dec 2021 08:35:51 +0100 Subject: [PATCH] Make ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated obsolete FromMarkup shorter and clearer hopefully. --- .../RoslynAssertTests/CodeFix.Success.cs | 6 +-- .../RoslynAssertTests/Diagnostics.Fail.cs | 6 +-- .../RoslynAssertTests/Diagnostics.Success.cs | 14 ++--- .../RoslynAssertTests/FixAll.Success.cs | 12 ++--- .../ExpectedDiagnostic.Obsolete.cs | 36 +++++++++++++ Gu.Roslyn.Asserts/ExpectedDiagnostic.cs | 52 +++++++++---------- 6 files changed, 81 insertions(+), 45 deletions(-) create mode 100644 Gu.Roslyn.Asserts/ExpectedDiagnostic.Obsolete.cs diff --git a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/CodeFix.Success.cs b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/CodeFix.Success.cs index bed28cd8..2412a00b 100644 --- a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/CodeFix.Success.cs +++ b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/CodeFix.Success.cs @@ -261,7 +261,7 @@ class C public int M() => f; } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated(FieldNameMustNotBeginWithUnderscore.DiagnosticId, before, out before); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup(FieldNameMustNotBeginWithUnderscore.DiagnosticId, before, out before); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var fix = new DoNotUseUnderscoreFix(); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, before, after, "Rename to: 'f'"); @@ -629,7 +629,7 @@ public class C1 RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, new[] { after, before2 }); - expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before2, before1 }, after); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before2, before1 }, new[] { after, before2 }); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, after); @@ -674,7 +674,7 @@ public class C1 RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, new[] { after, before2 }); - expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before2, before1 }, after); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before2, before1 }, new[] { after, before2 }); RoslynAssert.CodeFix(fix, expectedDiagnostic, new[] { before1, before2 }, after); diff --git a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs index d6ae06b9..42d353b9 100644 --- a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs +++ b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs @@ -237,7 +237,7 @@ class C " SA1309 Field '_f' must not begin with an underscore\r\n" + " at line 5 and character 29 in file C.cs | private readonly int ↓_f = 1;\r\n"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var exception = Assert.Throws(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code)); Assert.AreEqual(expected, exception.Message); @@ -380,7 +380,7 @@ class C " SA1309 Field '_f' must not begin with an underscore\r\n" + " at line 5 and character 29 in file C.cs | private readonly int ↓_f = 1;\r\n"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Field '_f' must not begin with an underscore", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", "Field '_f' must not begin with an underscore", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var exception = Assert.Throws(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code)); Assert.AreEqual(expected, exception.Message); @@ -410,7 +410,7 @@ class C " SA1309 Field '_f' must not begin with an underscore\r\n" + " at line 5 and character 29 in file C.cs | private readonly int ↓_f = 1;\r\n"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Wrong message", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", "Wrong message", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var exception = Assert.Throws(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code)); diff --git a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Success.cs b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Success.cs index fb5ad485..83501700 100644 --- a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Success.cs +++ b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Success.cs @@ -76,12 +76,12 @@ class C public int M() => _f; } }"; - var expectedDiagnostic1 = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated( + var expectedDiagnostic1 = ExpectedDiagnostic.FromMarkup( FieldNameMustNotBeginWithUnderscoreReportsTwo.DiagnosticId1, code, out _); - var expectedDiagnostic2 = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated( + var expectedDiagnostic2 = ExpectedDiagnostic.FromMarkup( FieldNameMustNotBeginWithUnderscoreReportsTwo.DiagnosticId2, code, out code); @@ -165,7 +165,7 @@ class C } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code); RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code); @@ -185,7 +185,7 @@ class C } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code); RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code); @@ -205,7 +205,7 @@ class C } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Field '_f' must not begin with an underscore", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", "Field '_f' must not begin with an underscore", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code); @@ -247,7 +247,7 @@ class C } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Field '_f' must not begin with an underscore", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309", "Field '_f' must not begin with an underscore", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code); } @@ -266,7 +266,7 @@ class C } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309b", "Field '_f' must not begin with an underscore", code, out code); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("SA1309b", "Field '_f' must not begin with an underscore", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscoreDifferentDiagnosticsForPublic(); RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code); RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code); diff --git a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Success.cs b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Success.cs index 73bb78d0..5b1edadd 100644 --- a/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Success.cs +++ b/Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Success.cs @@ -444,7 +444,7 @@ public class C1 { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before1, before2 }, new[] { after, before2 }); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before2, before1 }, new[] { before2, after }); @@ -481,7 +481,7 @@ public class C1 { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before2, before1 }, after); @@ -519,7 +519,7 @@ public class CCore { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before2, before1 }, after); @@ -556,7 +556,7 @@ public class C1 { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before1, out before1); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before1, out before1); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before2, before1 }, after); @@ -593,7 +593,7 @@ public class C2 : N.Core.C1 { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before2, out before2); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before2, out before2); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before1, before2 }, after); RoslynAssert.FixAll(fix, expectedDiagnostic, new[] { before2, before1 }, after); @@ -622,7 +622,7 @@ public class C { } }"; - var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("CS0067", before, out before); + var expectedDiagnostic = ExpectedDiagnostic.FromMarkup("CS0067", before, out before); var fix = new RemoveUnusedFix(); RoslynAssert.FixAll(fix, expectedDiagnostic, before, after); } diff --git a/Gu.Roslyn.Asserts/ExpectedDiagnostic.Obsolete.cs b/Gu.Roslyn.Asserts/ExpectedDiagnostic.Obsolete.cs new file mode 100644 index 00000000..8dfd1698 --- /dev/null +++ b/Gu.Roslyn.Asserts/ExpectedDiagnostic.Obsolete.cs @@ -0,0 +1,36 @@ +namespace Gu.Roslyn.Asserts +{ + using System.Collections.Generic; + + public partial class ExpectedDiagnostic + { + /// + /// Create a new instance of . + /// + /// The expected diagnostic id. + /// The code with error position indicated.. + /// without error indicator. + /// A new instance of . + public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string code, out string cleanedSources) => FromMarkup(diagnosticId, code, out cleanedSources); + + /// + /// Create a new instance of . + /// + /// The expected diagnostic id. + /// The expected message. + /// The code with error position indicated.. + /// without error indicator. + /// A new instance of . + public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string? message, string code, out string cleanedSources) => FromMarkup(diagnosticId, code, out cleanedSources); + + /// + /// Create a new instance of . + /// + /// The expected diagnostic id. + /// The expected message. + /// The code with error position indicated.. + /// without errors indicated. + /// A new instance of . + public static IReadOnlyList CreateManyFromCodeWithErrorsIndicated(string diagnosticId, string message, string codeWithErrorsIndicated, out string cleanedSources) => ManyFromMarkup(diagnosticId, message, codeWithErrorsIndicated, out cleanedSources); + } +} diff --git a/Gu.Roslyn.Asserts/ExpectedDiagnostic.cs b/Gu.Roslyn.Asserts/ExpectedDiagnostic.cs index 00d51997..39435603 100644 --- a/Gu.Roslyn.Asserts/ExpectedDiagnostic.cs +++ b/Gu.Roslyn.Asserts/ExpectedDiagnostic.cs @@ -14,7 +14,7 @@ /// Info about an expected diagnostic. /// [DebuggerDisplay("{Id} {Message} {Span}")] - public class ExpectedDiagnostic + public partial class ExpectedDiagnostic { private static readonly FileLinePositionSpan NoPosition = new("MISSING", default, default); @@ -196,22 +196,22 @@ public static ExpectedDiagnostic Create(string diagnosticId, string message, str /// Create a new instance of . /// /// The expected diagnostic id. - /// The code with error position indicated.. - /// without error indicator. + /// The code with diagnostic position indicated. + /// without position indicated. /// A new instance of . - public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string code, out string cleanedSources) + public static ExpectedDiagnostic FromMarkup(string diagnosticId, string markup, out string code) { if (diagnosticId is null) { throw new ArgumentNullException(nameof(diagnosticId)); } - if (code is null) + if (markup is null) { - throw new ArgumentNullException(nameof(code)); + throw new ArgumentNullException(nameof(markup)); } - return CreateFromCodeWithErrorsIndicated(diagnosticId, null, code, out cleanedSources); + return FromMarkup(diagnosticId, null, markup, out code); } /// @@ -219,34 +219,34 @@ public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagno /// /// The expected diagnostic id. /// The expected message. - /// The code with error position indicated.. - /// without error indicator. + /// The code with diagnostic position indicated. + /// without position indicator. /// A new instance of . - public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string? message, string code, out string cleanedSources) + public static ExpectedDiagnostic FromMarkup(string diagnosticId, string? message, string markup, out string code) { if (diagnosticId is null) { throw new ArgumentNullException(nameof(diagnosticId)); } - if (code is null) + if (markup is null) { - throw new ArgumentNullException(nameof(code)); + throw new ArgumentNullException(nameof(markup)); } - var positions = CodeReader.FindLinePositions(code).ToArray(); + var positions = CodeReader.FindLinePositions(markup).ToArray(); if (positions.Length == 0) { - throw new ArgumentException("Expected one error position indicated, was zero.", nameof(code)); + throw new ArgumentException("Expected one error position indicated, was zero.", nameof(markup)); } if (positions.Length > 1) { - throw new ArgumentException($"Expected one error position indicated, was {positions.Length}.", nameof(code)); + throw new ArgumentException($"Expected one error position indicated, was {positions.Length}.", nameof(markup)); } - cleanedSources = code.Replace("↓", string.Empty); - var fileName = CodeReader.FileName(code); + code = markup.Replace("↓", string.Empty); + var fileName = CodeReader.FileName(markup); var position = positions[0]; return new ExpectedDiagnostic(diagnosticId, message, new FileLinePositionSpan(fileName, position, position)); } @@ -256,10 +256,10 @@ public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagno /// /// The expected diagnostic id. /// The expected message. - /// The code with error position indicated.. - /// without errors indicated. + /// The code with diagnostic position indicated. + /// without position indicated. /// A new instance of . - public static IReadOnlyList CreateManyFromCodeWithErrorsIndicated(string diagnosticId, string message, string codeWithErrorsIndicated, out string cleanedSources) + public static IReadOnlyList ManyFromMarkup(string diagnosticId, string message, string markup, out string code) { if (diagnosticId is null) { @@ -271,19 +271,19 @@ public static IReadOnlyList CreateManyFromCodeWithErrorsIndi throw new ArgumentNullException(nameof(message)); } - if (codeWithErrorsIndicated is null) + if (markup is null) { - throw new ArgumentNullException(nameof(codeWithErrorsIndicated)); + throw new ArgumentNullException(nameof(markup)); } - var positions = CodeReader.FindLinePositions(codeWithErrorsIndicated).ToArray(); + var positions = CodeReader.FindLinePositions(markup).ToArray(); if (positions.Length == 0) { - throw new ArgumentException("Expected one error position indicated, was zero.", nameof(codeWithErrorsIndicated)); + throw new ArgumentException("Expected one error position indicated, was zero.", nameof(markup)); } - cleanedSources = codeWithErrorsIndicated.Replace("↓", string.Empty); - var fileName = CodeReader.FileName(codeWithErrorsIndicated); + code = markup.Replace("↓", string.Empty); + var fileName = CodeReader.FileName(markup); return positions.Select(p => new ExpectedDiagnostic(diagnosticId, message, new FileLinePositionSpan(fileName, p, p))) .ToArray(); }