Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithArguments call validation #1186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ public DiagnosticResult WithOptions(DiagnosticOptions options)

public DiagnosticResult WithArguments(params object[] arguments)
{
if (MessageFormat != null)
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
{
int placeholderCount = CountPlaceholders(MessageFormat.ToString());

if (arguments.Length != placeholderCount)
{
var message = $"Incorrect number of arguments provided. The message expects {placeholderCount} argument(s), but received {arguments.Length}.";
throw new ArgumentException(message);
}
}

return new DiagnosticResult(
spans: _spans,
suppressMessage: _suppressMessage,
Expand Down Expand Up @@ -479,5 +490,11 @@ public override string ToString()

return builder.ToString();
}

private int CountPlaceholders(string messageFormat)
{
var regex = new System.Text.RegularExpressions.Regex(@"\{[0-9]+(:[^}]*)?\}");
return regex.Matches(messageFormat).Count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -550,5 +550,92 @@ public async Task TestTopLevelStatements()
}.RunAsync();
}
#endif

[Fact]
[WorkItem(516, "https://github.com/dotnet/roslyn-sdk/issues/516")]
public void WithArguments_TooManyArguments_ShouldThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0}' calls '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var exception = Assert.Throws<ArgumentException>(() => result.WithArguments("arg1", "arg2", "arg3"));
Assert.Equal("Incorrect number of arguments provided. The message expects 2 argument(s), but received 3.", exception.Message);
}

[Fact]
public void WithArguments_TooFewArguments_ShouldThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0}' calls '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var exception = Assert.Throws<ArgumentException>(() => result.WithArguments("arg1"));
Assert.Equal("Incorrect number of arguments provided. The message expects 2 argument(s), but received 1.", exception.Message);
}

[Fact]
public void WithArguments_NoPlaceholdersButWithArguments_ShouldThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "This message has no placeholders.", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var exception = Assert.Throws<ArgumentException>(() => result.WithArguments("arg1"));
Assert.Equal("Incorrect number of arguments provided. The message expects 0 argument(s), but received 1.", exception.Message);
}

[Fact]
public void WithArguments_NoArgumentsAndNoPlaceholders_ShouldNotThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "This message has no placeholders.", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var formattedResult = result.WithArguments();

Assert.Empty(formattedResult.MessageArguments!);
Assert.Equal("This message has no placeholders.", formattedResult.Message);
}

[Fact]
public void WithArguments_PlaceholdersWithoutArguments_ShouldThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0}' calls '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var exception = Assert.Throws<ArgumentException>(() => result.WithArguments());
Assert.Equal("Incorrect number of arguments provided. The message expects 2 argument(s), but received 0.", exception.Message);
}

[Fact]
public void WithArguments_ComplexPlaceholders_ShouldFormatCorrectly()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0:MMMM dd, yyyy}' and '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var formattedResult = result.WithArguments(DateTime.Now, "arg2");

Assert.Contains("arg2", formattedResult.Message);
}

[Fact]
public void WithArguments_DifferentDataTypes_ShouldFormatCorrectly()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0}' and '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var formattedResult = result.WithArguments(123, DateTime.Now);

Assert.Contains("123", formattedResult.Message);
}

[Fact]
public void WithArguments_CorrectNumberOfArguments_ShouldNotThrowException()
{
var descriptor = new DiagnosticDescriptor("TestId", "TestTitle", "'{0}' calls '{1}'", "Category", DiagnosticSeverity.Warning, true);
var result = new DiagnosticResult(descriptor);

var formattedResult = result.WithArguments("arg1", "arg2");

Assert.Equal(new object[] { "arg1", "arg2" }, formattedResult.MessageArguments);
Assert.Equal("'arg1' calls 'arg2'", formattedResult.Message);
}
}
}