Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions SourceGeneratorToolkit.Test/DiagnosticCatalogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@
CollectionAssert.AreEqual(ExpectedAllocationOrder, ids);
}

[TestMethod]
public void ReusingANumberAcrossSeveritiesThrows()
{
DiagnosticCatalog catalog = new("X", "c");
catalog.Warning(1, "a", "a");

ArgumentException exception = Assert.ThrowsExactly<ArgumentException>(() => catalog.Error(1, "b", "b"));

Assert.AreEqual("number", exception.ParamName);
Assert.AreEqual(1, catalog.Descriptors.Count);

Check warning on line 78 in SourceGeneratorToolkit.Test/DiagnosticCatalogTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_SourceGeneratorToolkit&issues=AaDdfc6AobAzMXtw1v2O&open=AaDdfc6AobAzMXtw1v2O&pullRequest=21
}

[TestMethod]
public void ReusingANumberWithTheSameSeverityThrows()
{
DiagnosticCatalog catalog = new("X", "c");
catalog.Warning(1, "a", "a");

Assert.ThrowsExactly<ArgumentException>(() => catalog.Warning(1, "a", "a"));
}

[TestMethod]
public void TheSameNumberIsAllowedInSeparateCatalogues()
{
DiagnosticCatalog first = new("X", "c");
DiagnosticCatalog second = new("Y", "c");

Assert.AreEqual("X001", first.Warning(1, "a", "a").Id);
Assert.AreEqual("Y001", second.Warning(1, "a", "a").Id);
}

[TestMethod]
public void DescriptorsIsEmptyBeforeAnythingIsAllocated()
{
Expand Down
18 changes: 17 additions & 1 deletion SourceGeneratorToolkit/DiagnosticCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ktsu.SourceGeneratorToolkit;
public sealed class DiagnosticCatalog(string idPrefix, string category)
{
private readonly List<DiagnosticDescriptor> descriptors = [];
private readonly HashSet<int> numbers = [];

/// <summary>Gets the category every descriptor in this catalogue is reported under.</summary>
public string Category { get; } = category;
Expand All @@ -40,6 +41,9 @@ public sealed class DiagnosticCatalog(string idPrefix, string category)
/// <param name="title">The diagnostic title.</param>
/// <param name="messageFormat">The message format string.</param>
/// <returns>The descriptor, also recorded in <see cref="Descriptors"/>.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="number"/> has already been allocated from this catalogue.
/// </exception>
public DiagnosticDescriptor Warning(int number, string title, string messageFormat) =>
Add(number, title, messageFormat, DiagnosticSeverity.Warning);

Expand All @@ -50,13 +54,25 @@ public DiagnosticDescriptor Warning(int number, string title, string messageForm
/// <param name="title">The diagnostic title.</param>
/// <param name="messageFormat">The message format string.</param>
/// <returns>The descriptor, also recorded in <see cref="Descriptors"/>.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="number"/> has already been allocated from this catalogue.
/// </exception>
public DiagnosticDescriptor Error(int number, string title, string messageFormat) =>
Add(number, title, messageFormat, DiagnosticSeverity.Error);

private DiagnosticDescriptor Add(int number, string title, string messageFormat, DiagnosticSeverity severity)
{
string id = idPrefix + number.ToString("D3", CultureInfo.InvariantCulture);

// Two descriptors under one identifier would share every .editorconfig severity override and
// #pragma suppression meant for either, so a reused number is a mistake to catch here.
if (!numbers.Add(number))
{
throw new ArgumentException($"Diagnostic number {number} is already allocated as {id}.", nameof(number));
}

DiagnosticDescriptor descriptor = new(
id: idPrefix + number.ToString("D3", CultureInfo.InvariantCulture),
id: id,
title: title,
messageFormat: messageFormat,
category: Category,
Expand Down
Loading