AssemblyAPITests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Structure
  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. TestContext.CurrentContext.AttachShowDirLink();
  40. var type = typeof(TestClass);
  41. var API = DumpAssemblyAPI.GetTypeSignature(type.GetTypeInfo()).OrderBy(item => item).ToArray();
  42. TestContext.CurrentContext.AttachText("TestAPI.txt", API);
  43. foreach (var l in API)
  44. {
  45. TestContext.WriteLine(l);
  46. }
  47. }
  48. [Test(Description ="Checks if we have introduced a breaking change between the current and previous API")]
  49. public void DumpCoreAPI()
  50. {
  51. TestContext.CurrentContext.AttachShowDirLink();
  52. var assembly = typeof(Schema2.ModelRoot).Assembly;
  53. var API = DumpAssemblyAPI.GetAssemblySignature(assembly).OrderBy(item => item).ToArray();
  54. TestContext.CurrentContext.AttachText($"API.Core.{Schema2.Asset.AssemblyInformationalVersion}.txt", API);
  55. _CheckBackwardsCompatibility("API.Core.1.0.0-alpha0009.txt", API);
  56. }
  57. [Test(Description = "Checks if we have introduced a breaking change between the current and previous API")]
  58. public void DumpToolkitAPI()
  59. {
  60. TestContext.CurrentContext.AttachShowDirLink();
  61. var assembly = typeof(Schema2.Schema2Toolkit).Assembly;
  62. var API = DumpAssemblyAPI.GetAssemblySignature(assembly).OrderBy(item => item).ToArray();
  63. TestContext.CurrentContext.AttachText($"API.Toolkit.{Schema2.Asset.AssemblyInformationalVersion}.txt", API);
  64. _CheckBackwardsCompatibility("API.Toolkit.1.0.0-alpha0009.txt", API);
  65. }
  66. private static void _CheckBackwardsCompatibility(string referenceAPIFile, string[] newLines)
  67. {
  68. referenceAPIFile = System.IO.Path.Combine(TestContext.CurrentContext.WorkDirectory, "Assets", referenceAPIFile);
  69. var refLines = System.IO.File.ReadAllLines(referenceAPIFile);
  70. bool backwardsCompatible = true;
  71. foreach (var l in refLines)
  72. {
  73. if (!newLines.Contains(l))
  74. {
  75. TestContext.WriteLine($"Missing: {l}");
  76. backwardsCompatible = false;
  77. }
  78. }
  79. Warn.If(!backwardsCompatible, "Current API is not backwards compatible");
  80. }
  81. }
  82. }