|
3 | 3 |
|
4 | 4 | using System.ClientModel.Primitives; |
5 | 5 | using System.Collections.Generic; |
6 | | -using System.Diagnostics.CodeAnalysis; |
7 | 6 | using System.IO; |
8 | 7 | using System.Linq; |
9 | 8 | using System.Threading.Tasks; |
10 | 9 | using Microsoft.CodeAnalysis; |
11 | | -using Microsoft.CodeAnalysis.CSharp; |
12 | 10 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
13 | 11 | using Microsoft.TypeSpec.Generator.Tests.Common; |
14 | 12 | using NUnit.Framework; |
@@ -291,100 +289,6 @@ public async Task DoesNotRemoveValidAttributes() |
291 | 289 | Assert.AreEqual(Helpers.GetExpectedFromFile().TrimEnd(), output, "The output should match the expected content."); |
292 | 290 | } |
293 | 291 |
|
294 | | - [Test] |
295 | | - public async Task RemovesExperimentalAttributeWhenInternalizing() |
296 | | - { |
297 | | - MockHelpers.LoadMockGenerator(); |
298 | | - var workspace = new AdhocWorkspace(); |
299 | | - var projectInfo = ProjectInfo.Create( |
300 | | - ProjectId.CreateNewId(), |
301 | | - VersionStamp.Create(), |
302 | | - name: "TestProj", |
303 | | - assemblyName: "TestProj", |
304 | | - language: LanguageNames.CSharp) |
305 | | - .WithMetadataReferences(new[] |
306 | | - { |
307 | | - MetadataReference.CreateFromFile(typeof(object).Assembly.Location), |
308 | | - MetadataReference.CreateFromFile(typeof(ExperimentalAttribute).Assembly.Location) |
309 | | - }); |
310 | | - |
311 | | - var project = workspace.AddProject(projectInfo); |
312 | | - var folder = Helpers.GetAssetFileOrDirectoryPath(false); |
313 | | - const string rootFileName = "ExperimentalInternalizeRoot.cs"; |
314 | | - string[] modelFileNames = |
315 | | - [ |
316 | | - "ReferencedModel.cs", |
317 | | - "UnreferencedModel.cs", |
318 | | - "UnreferencedWithOtherAttribute.cs", |
319 | | - "UnreferencedWithCombinedAttributes.cs" |
320 | | - ]; |
321 | | - foreach (var fileName in modelFileNames) |
322 | | - { |
323 | | - project = project.AddDocument( |
324 | | - fileName, |
325 | | - File.ReadAllText(Path.Join(folder, fileName))).Project; |
326 | | - } |
327 | | - project = project.AddDocument( |
328 | | - rootFileName, |
329 | | - File.ReadAllText(Path.Join(folder, rootFileName))).Project; |
330 | | - var postProcessor = new TestPostProcessor(rootFileName); |
331 | | - |
332 | | - var resultProject = await postProcessor.InternalizeAsync(project); |
333 | | - |
334 | | - var referencedModel = await GetSingleClassAsync(resultProject, "ReferencedModel.cs", "ReferencedModel"); |
335 | | - var unreferencedModel = await GetSingleClassAsync(resultProject, "UnreferencedModel.cs", "UnreferencedModel"); |
336 | | - var unreferencedWithOther = await GetSingleClassAsync(resultProject, "UnreferencedWithOtherAttribute.cs", "UnreferencedWithOtherAttribute"); |
337 | | - var unreferencedWithCombined = await GetSingleClassAsync(resultProject, "UnreferencedWithCombinedAttributes.cs", "UnreferencedWithCombinedAttributes"); |
338 | | - |
339 | | - // The referenced model stays public and keeps its [Experimental] attribute. |
340 | | - Assert.IsTrue(referencedModel.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword))); |
341 | | - Assert.IsTrue(HasExperimentalAttribute(referencedModel), "Referenced (public) model should keep [Experimental]."); |
342 | | - |
343 | | - // The unreferenced model is internalized and loses its [Experimental] attribute. |
344 | | - Assert.IsTrue(unreferencedModel.Modifiers.Any(m => m.IsKind(SyntaxKind.InternalKeyword))); |
345 | | - Assert.IsFalse(unreferencedModel.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword))); |
346 | | - Assert.IsFalse(HasExperimentalAttribute(unreferencedModel), "Internalized model should not keep [Experimental]."); |
347 | | - |
348 | | - // The documentation comment on the internalized type is preserved. |
349 | | - Assert.IsTrue( |
350 | | - unreferencedModel.GetLeadingTrivia().ToFullString().Contains("not referenced"), |
351 | | - "Doc comment of the internalized type should be preserved."); |
352 | | - |
353 | | - // An internalized type with another attribute in a separate list keeps the other attribute. |
354 | | - Assert.IsTrue(unreferencedWithOther.Modifiers.Any(m => m.IsKind(SyntaxKind.InternalKeyword))); |
355 | | - Assert.IsFalse(HasExperimentalAttribute(unreferencedWithOther), "Internalized model should not keep [Experimental]."); |
356 | | - Assert.IsTrue(HasAttribute(unreferencedWithOther, "Serializable"), "Other attributes should be preserved."); |
357 | | - Assert.IsTrue( |
358 | | - unreferencedWithOther.GetLeadingTrivia().ToFullString().Contains("must be preserved"), |
359 | | - "Doc comment should be preserved when only one of several attribute lists is removed."); |
360 | | - |
361 | | - // An internalized type with the experimental attribute combined in a single list keeps the others. |
362 | | - Assert.IsTrue(unreferencedWithCombined.Modifiers.Any(m => m.IsKind(SyntaxKind.InternalKeyword))); |
363 | | - Assert.IsFalse(HasExperimentalAttribute(unreferencedWithCombined), "Internalized model should not keep [Experimental]."); |
364 | | - Assert.IsTrue(HasAttribute(unreferencedWithCombined, "Serializable"), "Other attributes in the same list should be preserved."); |
365 | | - } |
366 | | - |
367 | | - private static async Task<ClassDeclarationSyntax> GetSingleClassAsync(Project project, string fileName, string className) |
368 | | - { |
369 | | - var doc = project.Documents.Single(d => d.Name == fileName); |
370 | | - var root = await doc.GetSyntaxRootAsync(); |
371 | | - return ((CompilationUnitSyntax)root!) |
372 | | - .DescendantNodes() |
373 | | - .OfType<ClassDeclarationSyntax>() |
374 | | - .Single(t => t.Identifier.Text == className); |
375 | | - } |
376 | | - |
377 | | - private static bool HasAttribute(BaseTypeDeclarationSyntax type, string attributeName) |
378 | | - => type.AttributeLists |
379 | | - .SelectMany(list => list.Attributes) |
380 | | - .Any(attr => attr.Name.ToString() == attributeName); |
381 | | - |
382 | | - |
383 | | - private static bool HasExperimentalAttribute(BaseTypeDeclarationSyntax type) |
384 | | - => type.AttributeLists |
385 | | - .SelectMany(list => list.Attributes) |
386 | | - .Any(attr => attr.Name.ToString() is "Experimental" or "ExperimentalAttribute"); |
387 | | - |
388 | 292 | private class TestPostProcessor : PostProcessor |
389 | 293 | { |
390 | 294 | private readonly string _rootFile; |
|
0 commit comments