TestsAllViews.cs 5.6 KB

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