summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Loader/LoaderExtensions.cs
blob: 9c0b91faddc3bc65f3a8fe5350790241feda3c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using Xamarin.Forms.CustomAttributes;

namespace Xamarin.Forms.Loader
{
	internal static class LoaderExtensions
	{
		public static bool InheritsFromException(this Type type)
		{
			if (type.IsSubclassOf(typeof (Exception)))
				return true;
			return false;
		}

		public static bool InheritsFromAttribute(this Type type)
		{
			if (type.IsSubclassOf(typeof (Attribute)))
				return true;
			return false;
		}

		public static bool IsDelegate(this Type type)
		{
			// ignore delegates
			if (type.BaseType == typeof (MulticastDelegate))
				return true;
			return false;
		}

		public static bool IsTestFixture(this Type type)
		{
			var attributes = type.GetCustomAttributes();
			if (attributes.OfType<TestFixtureAttribute>().Any())
				return true;
			return false;
		}

		public static bool HasTestAttribute(this MemberInfo memberInfo)
		{
			var attributes = memberInfo.GetCustomAttributes();
			return attributes.Any(attr => attr is UiTestAttribute);
		}

		public static bool IsUnitTested(Type type, MemberInfo memberInfo)
		{
			if (ExemptMembers.UnitTestedTypes.ContainsKey(type))
			{
				if (ExemptMembers.UnitTestedTypes[type].Contains(memberInfo.Name))
					return true;
			}
			return false;
		}

		public static bool HasTestExemptAttribute(this MemberInfo memberInfo)
		{
			var attributes = memberInfo.GetCustomAttributes();
			return attributes.Any(attr => attr is UiTestExemptAttribute);
		}

		public static bool IsSetupOrTearDown(this MemberInfo memberInfo)
		{
			var attributes = memberInfo.GetCustomAttributes();
			return
				attributes.Any(
					attr =>
						attr is SetUpAttribute || attr is TearDownAttribute || attr is TestFixtureSetUpAttribute ||
						attr is TestFixtureTearDownAttribute);
		}

		public static bool IsEqualityOverride(this MemberInfo memberInfo)
		{
			return
				memberInfo.Name == "Equals" ||
				memberInfo.Name == "GetHashCode" ||
				memberInfo.Name == "GetType";
		}

		public static bool IsToStringOverride(this MemberInfo memberInfo)
		{
			return memberInfo.Name == "ToString";
		}

		public static bool HasCategoryAttribute(this Type type)
		{
			var attributes = type.GetCustomAttributes();
			if (attributes.OfType<CategoryAttribute>().Any())
				return true;
			//throw new MissingCategoryOnTestFixtureException (type.Name);
			return false;
		}

		public static bool IsPublic(this MemberInfo memberInfo)
		{
			var type = memberInfo as Type;
			if (type != null)
				return type.IsPublic;

			var method = memberInfo as MethodInfo;
			if (method != null)
				return method.IsPublic;

			var property = memberInfo as PropertyInfo;
			if (property != null)
				return true; // binding flags determine public

			var evt = memberInfo as EventInfo;
			if (evt != null)
				return true; // binding flags determine public

			throw new LoaderException();
		}

		public static bool IsCompilerGenerated(this MemberInfo memberInfo)
		{
			// ignore autogenerated get_ and set_ methods
			var method = memberInfo as MethodInfo;
			if (method != null && method.IsSpecialName)
				return true;

			return false;
		}
	}
}