TestsAllViews.cs 5.6 KB

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