TestsAllViews.cs 5.6 KB

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