Skip to content

Commit 6e080ff

Browse files
Copilottarekgh
andcommitted
Add test to verify FormattableString namespace fix
Co-authored-by: tarekgh <10833894+tarekgh@users.noreply.github.com>
1 parent 997393f commit 6e080ff

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Reflection;
8+
using System.Threading;
79
using System.Threading.Tasks;
10+
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.CSharp;
812
using SourceGenerators.Tests;
913
using Xunit;
1014

@@ -251,6 +255,52 @@ public void GenericTypeParameterAttributesAreRetained()
251255
Assert.NotNull(type.GenericTypeParameters[1].GetCustomAttribute<TestClasses.BarAttribute>());
252256
}
253257

258+
[Fact]
259+
public async Task TestFormattableStringInvariantCodeGeneration()
260+
{
261+
// This test verifies that the fix for CS0234 error works correctly.
262+
// It tests a scenario with multiple parameters that would potentially trigger the FormattableString.Invariant code path
263+
// and ensures no compilation errors occur due to incorrect namespace references.
264+
string testSourceCode = @"
265+
using Microsoft.Extensions.Logging;
266+
267+
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
268+
{
269+
internal static partial class TestFormattableStringInvariant
270+
{
271+
[LoggerMessage(EventId = 10, Level = LogLevel.Information, Message = ""Processing user {userId} with status {status} at {timestamp}"")]
272+
public static partial void LogUserProcessing(ILogger logger, int userId, string status, System.DateTime timestamp);
273+
}
274+
}";
275+
276+
var (diagnostics, results) = await RoslynTestUtils.RunGenerator(
277+
new LoggerMessageGenerator(),
278+
new[] { typeof(ILogger).Assembly, typeof(LoggerMessageAttribute).Assembly },
279+
new[] { testSourceCode }).ConfigureAwait(false);
280+
281+
// Verify no CS0234 errors (the type or namespace name 'FormattableString' does not exist in the namespace 'System.Diagnostics.CodeAnalysis')
282+
Assert.Empty(diagnostics.Where(d => d.Id == "CS0234"));
283+
284+
// Should have no diagnostics at all
285+
Assert.Empty(diagnostics);
286+
Assert.Single(results);
287+
288+
string generatedCode = results[0].SourceText.ToString();
289+
290+
// Verify the generated code doesn't contain the incorrect namespace reference
291+
Assert.DoesNotContain("System.Diagnostics.CodeAnalysis.FormattableString", generatedCode);
292+
293+
// If FormattableString.Invariant is used, it should be in the correct namespace
294+
if (generatedCode.Contains("FormattableString.Invariant"))
295+
{
296+
Assert.Contains("global::System.FormattableString.Invariant", generatedCode);
297+
}
298+
299+
// Verify that the code was generated successfully
300+
Assert.True(generatedCode.Length > 0);
301+
Assert.Contains("LogUserProcessing", generatedCode);
302+
}
303+
254304
private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode)
255305
{
256306
string baseline = LineEndingsHelper.Normalize(await File.ReadAllTextAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false));

0 commit comments

Comments
 (0)