AssemblyAPITests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using NUnit.Framework;
  7. namespace SharpGLTF
  8. {
  9. [TestFixture]
  10. [Category("API Validation")]
  11. public class AssemblyAPITests
  12. {
  13. public class TestClass
  14. {
  15. public int PointerFunc(int x, out int y, ref int z) { y = 0; return x + 7; }
  16. public void ParamsFunc(int x, params string[] values) { }
  17. public void MultiArgsFunc(int x, int y=1, int z = 2) { }
  18. public int[][,][,,] MultiArray;
  19. public const string Alpha = "Alpha";
  20. public static readonly string Beta = "Beta";
  21. public delegate void MyDelegate(int x);
  22. public MyDelegate DelegateImpl;
  23. public enum Hello
  24. {
  25. World = 7
  26. }
  27. public event EventHandler EventX;
  28. public struct SomeStructure
  29. {
  30. public int X;
  31. public int Y { get; }
  32. public int Z { get; set; }
  33. public System.Numerics.Vector3 Vector;
  34. }
  35. }
  36. [Test(Description = "proof of concept to dump the whole public API of an assembly")]
  37. public void DumpTestAPI()
  38. {
  39. var type = typeof(TestClass);
  40. var API = DumpAssemblyAPI.GetTypeSignature(type.GetTypeInfo()).OrderBy(item => item).ToArray();
  41. AttachmentInfo.From("TestAPI.txt").WriteTextLines(API);
  42. foreach (var l in API)
  43. {
  44. TestContext.WriteLine(l);
  45. }
  46. }
  47. [Test(Description ="Checks if we have introduced a breaking change between the current and previous API")]
  48. public void DumpCoreAPI()
  49. {
  50. var assembly = typeof(Schema2.ModelRoot).Assembly;
  51. var API = DumpAssemblyAPI
  52. .GetAssemblySignature(assembly)
  53. .OrderBy(item => item)
  54. .ToArray();
  55. AttachmentInfo
  56. .From($"API.Core.{Schema2.Asset.AssemblyInformationalVersion}.txt")
  57. .WriteTextLines(API);
  58. _CheckBackwardsCompatibility("API.Core.1.0.0-alpha0011.txt", API);
  59. }
  60. [Test(Description = "Checks if we have introduced a breaking change between the current and previous API")]
  61. public void DumpToolkitAPI()
  62. {
  63. var assembly = typeof(Schema2.Toolkit).Assembly;
  64. var API = DumpAssemblyAPI
  65. .GetAssemblySignature(assembly)
  66. .OrderBy(item => item)
  67. .ToArray();
  68. AttachmentInfo
  69. .From($"API.Toolkit.{Schema2.Asset.AssemblyInformationalVersion}.txt")
  70. .WriteTextLines(API);
  71. _CheckBackwardsCompatibility("API.Toolkit.1.0.0-alpha0011.txt", API);
  72. }
  73. private static void _CheckBackwardsCompatibility(string referenceAPIFile, string[] newLines)
  74. {
  75. referenceAPIFile = ResourceInfo.From(referenceAPIFile).FilePath;
  76. var refLines = System.IO.File.ReadAllLines(referenceAPIFile);
  77. bool backwardsCompatible = true;
  78. foreach (var l in refLines)
  79. {
  80. if (!newLines.Contains(l))
  81. {
  82. TestContext.WriteLine($"Missing: {l}");
  83. backwardsCompatible = false;
  84. }
  85. }
  86. Warn.If(!backwardsCompatible, "Current API is not backwards compatible");
  87. }
  88. }
  89. }