AllViewsTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Xunit;
  6. using System.IO;
  7. namespace Terminal.Gui.Views {
  8. public class AllViewsTests {
  9. [Fact]
  10. public void AllViews_Tests_All_Constructors ()
  11. {
  12. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  13. foreach (var type in GetAllViewClassesCollection ()) {
  14. Assert.True (Constructors_FullTest (type));
  15. }
  16. Application.Shutdown ();
  17. }
  18. public bool Constructors_FullTest (Type type)
  19. {
  20. foreach (var ctor in type.GetConstructors ()) {
  21. if (type.IsGenericType && type.IsTypeDefinition) {
  22. List<Type> gTypes = new List<Type> ();
  23. foreach (var args in type.GetGenericArguments ()) {
  24. gTypes.Add (typeof (object));
  25. }
  26. type = type.MakeGenericType (gTypes.ToArray ());
  27. Assert.IsType (type, (View)Activator.CreateInstance (type));
  28. } else {
  29. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  30. Type paramType;
  31. List<object> pTypes = new List<object> ();
  32. if (type.IsGenericType) {
  33. foreach (var args in type.GetGenericArguments ()) {
  34. paramType = args.GetType ();
  35. if (args.Name == "T") {
  36. pTypes.Add (typeof (object));
  37. } else {
  38. AddArguments (paramType, pTypes);
  39. }
  40. }
  41. }
  42. foreach (var p in paramsInfo) {
  43. paramType = p.ParameterType;
  44. if (p.HasDefaultValue) {
  45. pTypes.Add (p.DefaultValue);
  46. } else {
  47. AddArguments (paramType, pTypes);
  48. }
  49. }
  50. if (type.IsGenericType && !type.IsTypeDefinition) {
  51. Assert.IsType (type, (View)Activator.CreateInstance (type));
  52. } else {
  53. Assert.IsType (type, ctor.Invoke (pTypes.ToArray ()));
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59. private static void AddArguments (Type paramType, List<object> pTypes)
  60. {
  61. if (paramType == typeof (Rect)) {
  62. pTypes.Add (Rect.Empty);
  63. } else if (paramType == typeof (NStack.ustring)) {
  64. pTypes.Add (NStack.ustring.Empty);
  65. } else if (paramType == typeof (int)) {
  66. pTypes.Add (0);
  67. } else if (paramType == typeof (bool)) {
  68. pTypes.Add (true);
  69. } else if (paramType.Name == "IList") {
  70. pTypes.Add (new List<object> ());
  71. } else if (paramType.Name == "View") {
  72. var top = new Toplevel ();
  73. var view = new View ();
  74. top.Add (view);
  75. pTypes.Add (view);
  76. } else if (paramType.Name == "View[]") {
  77. pTypes.Add (new View [] { });
  78. } else if (paramType.Name == "Stream") {
  79. pTypes.Add (new MemoryStream ());
  80. } else if (paramType.Name == "String") {
  81. pTypes.Add (string.Empty);
  82. } else if (paramType.Name == "TreeView`1[T]") {
  83. pTypes.Add (string.Empty);
  84. } else {
  85. pTypes.Add (null);
  86. }
  87. }
  88. List<Type> GetAllViewClassesCollection ()
  89. {
  90. List<Type> types = new List<Type> ();
  91. foreach (Type type in typeof (View).Assembly.GetTypes ()
  92. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View)))) {
  93. types.Add (type);
  94. }
  95. return types;
  96. }
  97. }
  98. }