Skip to content

Commit

Permalink
Make ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated obsolete
Browse files Browse the repository at this point in the history
FromMarkup shorter and clearer hopefully.
  • Loading branch information
JohanLarsson committed Dec 10, 2021
1 parent 46be495 commit 92c1450
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/CodeFix.Success.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssertException>(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code));
Assert.AreEqual(expected, exception.Message);
Expand Down Expand Up @@ -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<AssertException>(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code));
Assert.AreEqual(expected, exception.Message);
Expand Down Expand Up @@ -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<AssertException>(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code));
Expand Down
14 changes: 7 additions & 7 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Success.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Success.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions Gu.Roslyn.Asserts/ExpectedDiagnostic.Obsolete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Gu.Roslyn.Asserts
{
using System.Collections.Generic;

public partial class ExpectedDiagnostic
{
/// <summary>
/// Create a new instance of <see cref="ExpectedDiagnostic"/>.
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="code">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="code"/> without error indicator.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string code, out string cleanedSources) => FromMarkup(diagnosticId, code, out cleanedSources);

/// <summary>
/// Create a new instance of <see cref="ExpectedDiagnostic"/>.
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="message">The expected message.</param>
/// <param name="code">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="code"/> without error indicator.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagnosticId, string? message, string code, out string cleanedSources) => FromMarkup(diagnosticId, code, out cleanedSources);

/// <summary>
/// Create a new instance of <see cref="ExpectedDiagnostic"/>.
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="message">The expected message.</param>
/// <param name="codeWithErrorsIndicated">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="codeWithErrorsIndicated"/> without errors indicated.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
public static IReadOnlyList<ExpectedDiagnostic> CreateManyFromCodeWithErrorsIndicated(string diagnosticId, string message, string codeWithErrorsIndicated, out string cleanedSources) => ManyFromMarkup(diagnosticId, message, codeWithErrorsIndicated, out cleanedSources);
}
}
52 changes: 26 additions & 26 deletions Gu.Roslyn.Asserts/ExpectedDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/// Info about an expected diagnostic.
/// </summary>
[DebuggerDisplay("{Id} {Message} {Span}")]
public class ExpectedDiagnostic
public partial class ExpectedDiagnostic
{
private static readonly FileLinePositionSpan NoPosition = new("MISSING", default, default);

Expand Down Expand Up @@ -196,57 +196,57 @@ public static ExpectedDiagnostic Create(string diagnosticId, string message, str
/// Create a new instance of <see cref="ExpectedDiagnostic"/>.
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="code">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="code"/> without error indicator.</param>
/// <param name="markup">The code with diagnostic position indicated.</param>
/// <param name="code"><paramref name="markup"/> without position indicated.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
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);
}

/// <summary>
/// Create a new instance of <see cref="ExpectedDiagnostic"/>.
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="message">The expected message.</param>
/// <param name="code">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="code"/> without error indicator.</param>
/// <param name="markup">The code with diagnostic position indicated.</param>
/// <param name="code"><paramref name="code"/> without position indicator.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
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));
}
Expand All @@ -256,10 +256,10 @@ public static ExpectedDiagnostic CreateFromCodeWithErrorsIndicated(string diagno
/// </summary>
/// <param name="diagnosticId">The expected diagnostic id.</param>
/// <param name="message">The expected message.</param>
/// <param name="codeWithErrorsIndicated">The code with error position indicated..</param>
/// <param name="cleanedSources"><paramref name="codeWithErrorsIndicated"/> without errors indicated.</param>
/// <param name="markup">The code with diagnostic position indicated.</param>
/// <param name="code"><paramref name="markup"/> without position indicated.</param>
/// <returns>A new instance of <see cref="ExpectedDiagnostic"/>.</returns>
public static IReadOnlyList<ExpectedDiagnostic> CreateManyFromCodeWithErrorsIndicated(string diagnosticId, string message, string codeWithErrorsIndicated, out string cleanedSources)
public static IReadOnlyList<ExpectedDiagnostic> ManyFromMarkup(string diagnosticId, string message, string markup, out string code)
{
if (diagnosticId is null)
{
Expand All @@ -271,19 +271,19 @@ public static IReadOnlyList<ExpectedDiagnostic> 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();
}
Expand Down

0 comments on commit 92c1450

Please sign in to comment.