TestsAllViews.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #nullable enable
  2. using System.Drawing;
  3. using System.Reflection;
  4. using Terminal.Gui;
  5. namespace UnitTests;
  6. /// <summary>
  7. /// Base class for tests that need to test all views.
  8. /// </summary>
  9. public class TestsAllViews
  10. {
  11. /// <summary>
  12. /// Gets all view types.
  13. /// </summary>
  14. public static IEnumerable<object []> AllViewTypes =>
  15. typeof (View).Assembly
  16. .GetTypes ()
  17. .Where (
  18. type => type is { IsClass: true, IsAbstract: false, IsPublic: true }
  19. && (type.IsSubclassOf (typeof (View)) || type == typeof (View)))
  20. .Select (type => new object [] { type });
  21. /// <summary>
  22. /// Creates an instance of a view if it is not a generic type.
  23. /// </summary>
  24. /// <param name="type"></param>
  25. /// <returns></returns>
  26. public static View? CreateInstanceIfNotGeneric (Type type)
  27. {
  28. if (type.IsGenericType)
  29. {
  30. // Return null for generic types
  31. return null;
  32. }
  33. return Activator.CreateInstance (type) as View;
  34. }
  35. /// <summary>
  36. /// Gets a list of all view classes.
  37. /// </summary>
  38. /// <returns></returns>
  39. public static List<Type> GetAllViewClasses ()
  40. {
  41. return typeof (View).Assembly.GetTypes ()
  42. .Where (
  43. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  44. && myType.IsSubclassOf (typeof (View))
  45. )
  46. .ToList ();
  47. }
  48. /// <summary>
  49. /// Creates a view from a type.
  50. /// </summary>
  51. /// <param name="type">The type.</param>
  52. /// <param name="ctor">The constructor to call.</param>
  53. /// <returns></returns>
  54. public static View? CreateViewFromType (Type type, ConstructorInfo ctor)
  55. {
  56. View? viewType = null;
  57. if (type is { IsGenericType: true, IsTypeDefinition: true })
  58. {
  59. List<Type> typeArguments = new ();
  60. // use <object> or the original type if applicable
  61. foreach (Type arg in type.GetGenericArguments ())
  62. {
  63. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  64. {
  65. typeArguments.Add (arg);
  66. }
  67. else
  68. {
  69. typeArguments.Add (typeof (object));
  70. }
  71. }
  72. type = type.MakeGenericType (typeArguments.ToArray ());
  73. // Ensure the type does not contain any generic parameters
  74. if (type.ContainsGenericParameters)
  75. {
  76. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  77. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  78. return null;
  79. }
  80. Assert.IsType (type, (View)Activator.CreateInstance (type)!);
  81. }
  82. else
  83. {
  84. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  85. Type paramType;
  86. List<object> pTypes = new ();
  87. if (type.IsGenericType)
  88. {
  89. foreach (Type args in type.GetGenericArguments ())
  90. {
  91. paramType = args.GetType ();
  92. if (args.Name == "T")
  93. {
  94. pTypes.Add (typeof (object));
  95. }
  96. else
  97. {
  98. AddArguments (paramType, pTypes);
  99. }
  100. }
  101. }
  102. foreach (ParameterInfo p in paramsInfo)
  103. {
  104. paramType = p.ParameterType;
  105. if (p.HasDefaultValue)
  106. {
  107. pTypes.Add (p.DefaultValue!);
  108. }
  109. else
  110. {
  111. AddArguments (paramType, pTypes);
  112. }
  113. }
  114. if (type is { IsGenericType: true, IsTypeDefinition: false })
  115. {
  116. viewType = Activator.CreateInstance (type) as View;
  117. }
  118. else
  119. {
  120. viewType = (View)ctor.Invoke (pTypes.ToArray ());
  121. }
  122. Assert.IsType (type, viewType);
  123. }
  124. return viewType;
  125. }
  126. private static void AddArguments (Type paramType, List<object> pTypes)
  127. {
  128. if (paramType == typeof (Rectangle))
  129. {
  130. pTypes.Add (Rectangle.Empty);
  131. }
  132. else if (paramType == typeof (string))
  133. {
  134. pTypes.Add (string.Empty);
  135. }
  136. else if (paramType == typeof (int))
  137. {
  138. pTypes.Add (0);
  139. }
  140. else if (paramType == typeof (bool))
  141. {
  142. pTypes.Add (true);
  143. }
  144. else if (paramType.Name == "IList")
  145. {
  146. pTypes.Add (new List<object> ());
  147. }
  148. else if (paramType.Name == "View")
  149. {
  150. var top = new Toplevel ();
  151. var view = new View ();
  152. top.Add (view);
  153. pTypes.Add (view);
  154. }
  155. else if (paramType.Name == "View[]")
  156. {
  157. pTypes.Add (new View [] { });
  158. }
  159. else if (paramType.Name == "Stream")
  160. {
  161. pTypes.Add (new MemoryStream ());
  162. }
  163. else if (paramType.Name == "String")
  164. {
  165. pTypes.Add (string.Empty);
  166. }
  167. else if (paramType.Name == "TreeView`1[T]")
  168. {
  169. pTypes.Add (string.Empty);
  170. }
  171. else
  172. {
  173. pTypes.Add (null!);
  174. }
  175. }
  176. }