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 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Immutable;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Testing
Expand Down Expand Up @@ -122,13 +123,26 @@ public string? Message

if (MessageFormat != null)
{
var messageFormatString = MessageFormat.ToString();

// Ensure placeholders in the MessageFormat match the provided arguments
int placeholderCount = Regex.Matches(messageFormatString, @"\{[0-9]+(:[^}]*)?\}").Count;

// Initialize MessageArguments if null
var arguments = MessageArguments ?? EmptyArguments;

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

try
{
return string.Format(MessageFormat.ToString(), MessageArguments ?? EmptyArguments);
return string.Format(messageFormatString, arguments);
}
catch (FormatException)
{
return MessageFormat.ToString();
return messageFormatString;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,5 +550,94 @@ 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).WithArguments("arg1", "arg2", "arg3");

var exception = Assert.Throws<ArgumentException>(() => result.Message);
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).WithArguments("arg1");

var exception = Assert.Throws<ArgumentException>(() => result.Message);
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).WithArguments("arg1");

var exception = Assert.Throws<ArgumentException>(() => result.Message);
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).WithArguments();

Assert.Empty(result.MessageArguments!);
Assert.Equal("This message has no placeholders.", result.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).WithArguments();

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

[Fact]
public void WithArguments_PlaceholdersAndWithArgumentsNotCalled_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.Message);
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).WithArguments(DateTime.Now, "arg2");

Assert.Contains("arg2", result.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).WithArguments(123, DateTime.Now);

Assert.Contains("123", result.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).WithArguments("arg1", "arg2");

Assert.Equal(new object[] { "arg1", "arg2" }, result.MessageArguments);
Assert.Equal("'arg1' calls 'arg2'", result.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ public void TestToStringWithoutLocationOrMessage()
[Fact]
public void TestToStringWithMessageFormat()
{
Assert.Equal(
"error CS1002: {0} expected",
DiagnosticResult.CompilerError("CS1002").WithMessageFormat("{0} expected").ToString());
var exception = Assert.Throws<ArgumentException>(() =>
DiagnosticResult.CompilerError("CS1002")
.WithMessageFormat("{0} expected")
.ToString());
Assert.Equal("Incorrect number of arguments provided. The message expects 1 argument(s), but received 0.", exception.Message);

Assert.Equal(
"error CS1002: ; expected",
DiagnosticResult.CompilerError("CS1002").WithMessageFormat("{0} expected").WithArguments(";").ToString());
DiagnosticResult.CompilerError("CS1002")
.WithMessageFormat("{0} expected")
.WithArguments(";")
.ToString());
}

[Fact]
Expand Down