ApiApprovalTests.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 PublicApiGenerator;
  5. using System;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using Verify;
  10. using VerifyXunit;
  11. using Xunit;
  12. using Xunit.Abstractions;
  13. namespace ReactiveTests.Tests.Api
  14. {
  15. public class ApiApprovalTests :
  16. VerifyBase
  17. {
  18. private VerifySettings verifySettings;
  19. public ApiApprovalTests(ITestOutputHelper output) :
  20. base(output)
  21. {
  22. verifySettings = new VerifySettings();
  23. verifySettings.UseExtension("cs");
  24. }
  25. [Fact]
  26. public Task Core()
  27. {
  28. var publicApi = GeneratePublicApi(typeof(System.Reactive.Unit).Assembly);
  29. return Verify(publicApi, verifySettings);
  30. }
  31. [Fact]
  32. public Task Aliases()
  33. {
  34. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  35. return Verify(publicApi, verifySettings);
  36. }
  37. [Fact]
  38. public Task Testing()
  39. {
  40. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  41. return Verify(publicApi, verifySettings);
  42. }
  43. private string GeneratePublicApi(Assembly assembly)
  44. {
  45. var namespacePrefixWhitelist = new[] { "System", "Microsoft" };
  46. return Filter(ApiGenerator.GeneratePublicApi(assembly, whitelistedNamespacePrefixes: namespacePrefixWhitelist));
  47. }
  48. private static string Filter(string text)
  49. {
  50. return string.Join(Environment.NewLine, text.Split(new[]
  51. {
  52. Environment.NewLine
  53. }, StringSplitOptions.RemoveEmptyEntries)
  54. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  55. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  56. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  57. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  58. .Where(l => !string.IsNullOrWhiteSpace(l))
  59. );
  60. }
  61. }
  62. }