ViewTestHelpers.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Reflection;
  2. namespace UnitTests;
  3. /// <summary>
  4. /// Helpers for View tests.
  5. /// </summary>
  6. public class ViewTestHelpers
  7. {
  8. public static View CreateViewFromType (Type type, ConstructorInfo ctor)
  9. {
  10. View viewType = null;
  11. if (type.IsGenericType && type.IsTypeDefinition)
  12. {
  13. List<Type> gTypes = new ();
  14. foreach (Type args in type.GetGenericArguments ())
  15. {
  16. gTypes.Add (typeof (object));
  17. }
  18. type = type.MakeGenericType (gTypes.ToArray ());
  19. Assert.IsType (type, (View)Activator.CreateInstance (type));
  20. }
  21. else
  22. {
  23. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  24. Type paramType;
  25. List<object> pTypes = new ();
  26. if (type.IsGenericType)
  27. {
  28. foreach (Type args in type.GetGenericArguments ())
  29. {
  30. paramType = args.GetType ();
  31. if (args.Name == "T")
  32. {
  33. pTypes.Add (typeof (object));
  34. }
  35. else
  36. {
  37. AddArguments (paramType, pTypes);
  38. }
  39. }
  40. }
  41. foreach (ParameterInfo p in paramsInfo)
  42. {
  43. paramType = p.ParameterType;
  44. if (p.HasDefaultValue)
  45. {
  46. pTypes.Add (p.DefaultValue);
  47. }
  48. else
  49. {
  50. AddArguments (paramType, pTypes);
  51. }
  52. }
  53. if (type.IsGenericType && !type.IsTypeDefinition)
  54. {
  55. viewType = (View)Activator.CreateInstance (type);
  56. Assert.IsType (type, viewType);
  57. }
  58. else
  59. {
  60. viewType = (View)ctor.Invoke (pTypes.ToArray ());
  61. Assert.IsType (type, viewType);
  62. }
  63. }
  64. return viewType;
  65. }
  66. public static List<Type> GetAllViewClasses ()
  67. {
  68. return typeof (View).Assembly.GetTypes ()
  69. .Where (
  70. myType => myType.IsClass
  71. && !myType.IsAbstract
  72. && myType.IsPublic
  73. && myType.IsSubclassOf (typeof (View))
  74. )
  75. .ToList ();
  76. }
  77. private static void AddArguments (Type paramType, List<object> pTypes)
  78. {
  79. if (paramType == typeof (Rectangle))
  80. {
  81. pTypes.Add (Rectangle.Empty);
  82. }
  83. else if (paramType == typeof (string))
  84. {
  85. pTypes.Add (string.Empty);
  86. }
  87. else if (paramType == typeof (int))
  88. {
  89. pTypes.Add (0);
  90. }
  91. else if (paramType == typeof (bool))
  92. {
  93. pTypes.Add (true);
  94. }
  95. else if (paramType.Name == "IList")
  96. {
  97. pTypes.Add (new List<object> ());
  98. }
  99. else if (paramType.Name == "View")
  100. {
  101. var top = new Toplevel ();
  102. var view = new View ();
  103. top.Add (view);
  104. pTypes.Add (view);
  105. }
  106. else if (paramType.Name == "View[]")
  107. {
  108. pTypes.Add (new View [] { });
  109. }
  110. else if (paramType.Name == "Stream")
  111. {
  112. pTypes.Add (new MemoryStream ());
  113. }
  114. else if (paramType.Name == "String")
  115. {
  116. pTypes.Add (string.Empty);
  117. }
  118. else if (paramType.Name == "TreeView`1[T]")
  119. {
  120. pTypes.Add (string.Empty);
  121. }
  122. else
  123. {
  124. pTypes.Add (null);
  125. }
  126. }
  127. }