|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.IO; |
| 6 | +using System.Linq; |
6 | 7 | using System.Reflection; |
| 8 | +using System.Threading; |
7 | 9 | using System.Threading.Tasks; |
| 10 | +using Microsoft.CodeAnalysis; |
| 11 | +using Microsoft.CodeAnalysis.CSharp; |
8 | 12 | using SourceGenerators.Tests; |
9 | 13 | using Xunit; |
10 | 14 |
|
@@ -251,6 +255,52 @@ public void GenericTypeParameterAttributesAreRetained() |
251 | 255 | Assert.NotNull(type.GenericTypeParameters[1].GetCustomAttribute<TestClasses.BarAttribute>()); |
252 | 256 | } |
253 | 257 |
|
| 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 | + |
254 | 304 | private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode) |
255 | 305 | { |
256 | 306 | string baseline = LineEndingsHelper.Normalize(await File.ReadAllTextAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false)); |
|
0 commit comments