ApiApprovalTests.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using PublicApiGenerator;
  11. using VerifyXunit;
  12. namespace Tests.System.Interactive.ApiApprovals.Api;
  13. public class ApiApprovalTests : VerifyBase
  14. {
  15. static ApiApprovalTests()
  16. {
  17. VerifierSettings.OnVerifyMismatch((filePair, message, autoVerify) => DiffPlexReporter.Report(filePair.ReceivedPath, filePair.VerifiedPath, message));
  18. }
  19. public ApiApprovalTests()
  20. : base()
  21. {
  22. }
  23. [Fact]
  24. public Task SystemInteractive()
  25. {
  26. var publicApi = GeneratePublicApi(typeof(EnumerableEx).Assembly);
  27. return Verify(publicApi, "cs");
  28. }
  29. [Fact]
  30. public Task SystemInteractiveProviders()
  31. {
  32. var publicApi = GeneratePublicApi(typeof(QueryableEx).Assembly);
  33. return Verify(publicApi, "cs");
  34. }
  35. [Fact]
  36. public Task SystemInteractiveAsync()
  37. {
  38. var publicApi = GeneratePublicApi(typeof(AsyncEnumerableEx).Assembly);
  39. return Verify(publicApi, "cs");
  40. }
  41. [Fact]
  42. public Task SystemInteractiveAsyncProviders()
  43. {
  44. var publicApi = GeneratePublicApi(typeof(AsyncQueryableEx).Assembly);
  45. return Verify(publicApi, "cs");
  46. }
  47. [Fact]
  48. public Task SystemLinqAsync()
  49. {
  50. var publicApi = GeneratePublicApi(typeof(AsyncEnumerable).Assembly);
  51. return Verify(publicApi, "cs");
  52. }
  53. [Fact]
  54. public Task SystemLinqAsyncQueryable()
  55. {
  56. var publicApi = GeneratePublicApi(typeof(AsyncQueryable).Assembly);
  57. return Verify(publicApi, "cs");
  58. }
  59. private string GeneratePublicApi(Assembly assembly)
  60. {
  61. ApiGeneratorOptions options = new()
  62. {
  63. AllowNamespacePrefixes = ["System", "Microsoft"]
  64. };
  65. return Filter(ApiGenerator.GeneratePublicApi(assembly, options));
  66. }
  67. private static string Filter(string text)
  68. {
  69. return string.Join(Environment.NewLine, text.Split(new[]
  70. {
  71. Environment.NewLine
  72. }, StringSplitOptions.RemoveEmptyEntries)
  73. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  74. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  75. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  76. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  77. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"RepositoryUrl\""))
  78. .Where(l => !string.IsNullOrWhiteSpace(l))
  79. );
  80. }
  81. }