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().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().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; } } }