예제 #1
0
        void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
        {
            string displayNameBase = factAttribute.GetNamedArgument<string>("DisplayName") ?? type.Name + "." + method.Name;
            ITypeInfo[] resolvedTypes = null;

            if (arguments != null && method.IsGenericMethodDefinition)
            {
                resolvedTypes = ResolveGenericTypes(method, arguments);
                method = method.MakeGenericMethod(resolvedTypes);
            }

            Assembly = assembly;
            Class = type;
            Method = method;
            Arguments = arguments;
            DisplayName = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
            SkipReason = factAttribute.GetNamedArgument<string>("Skip");
            Traits = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
            TestCollection = testCollection;

            foreach (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute))
                                                            .Concat(Class.GetCustomAttributes(typeof(TraitAttribute))))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
            }

            uniqueID = new Lazy<string>(GetUniqueID, true);
        }
 public static bool IsContext(this ITypeInfo type)
 {
     return(!type.IsAbstract &&
            !type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() &&
            !type.GetGenericArguments().Any() &&
            type.GetFields().Any(x => x.IsSpecification() || x.IsBehavior()));
 }
예제 #3
0
        void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
        {
            string displayNameBase = factAttribute.GetNamedArgument <string>("DisplayName") ?? type.Name + "." + method.Name;

            ITypeInfo[] resolvedTypes = null;

            if (arguments != null && method.IsGenericMethodDefinition)
            {
                resolvedTypes = ResolveGenericTypes(method, arguments);
                method        = method.MakeGenericMethod(resolvedTypes);
            }

            Assembly       = assembly;
            Class          = type;
            Method         = method;
            Arguments      = arguments;
            DisplayName    = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
            SkipReason     = factAttribute.GetNamedArgument <string>("Skip");
            Traits         = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);
            TestCollection = testCollection;

            foreach (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute))
                     .Concat(Class.GetCustomAttributes(typeof(TraitAttribute))))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
            }

            uniqueID = new Lazy <string>(GetUniqueID, true);
        }
예제 #4
0
        /// <summary>
        /// We look for attributes implementing IFixtureBuilder at one level
        /// of inheritance at a time. Attributes on base classes are not used
        /// unless there are no fixture builder attributes at all on the derived
        /// class. This is by design.
        /// </summary>
        /// <param name="typeInfo">The type being examined for attributes</param>
        /// <returns>A list of the attributes found.</returns>
        private IFixtureBuilder[] GetFixtureBuilderAttributes(ITypeInfo typeInfo)
        {
            IFixtureBuilder[] attrs = new IFixtureBuilder[0];

            while (typeInfo != null && !typeInfo.IsType(typeof(object)))
            {
                attrs = typeInfo.GetCustomAttributes <IFixtureBuilder>(false);

                if (attrs.Length > 0)
                {
                    // We want to eliminate duplicates that have no args.
                    // If there is just one, no duplication is possible.
                    if (attrs.Length == 1)
                    {
                        return(attrs);
                    }

                    // Count how many have arguments
                    int withArgs = 0;
                    foreach (var attr in attrs)
                    {
                        if (HasArguments(attr))
                        {
                            withArgs++;
                        }
                    }

                    // If all have args, just return them
                    if (withArgs == attrs.Length)
                    {
                        return(attrs);
                    }

                    // If none of them have args, return the first one
                    if (withArgs == 0)
                    {
                        return new IFixtureBuilder[] { attrs[0] }
                    }
                    ;

                    // Some of each - extract those with args
                    var result = new IFixtureBuilder[withArgs];
                    int count  = 0;
                    foreach (var attr in attrs)
                    {
                        if (HasArguments(attr))
                        {
                            result[count++] = attr;
                        }
                    }

                    return(result);
                }

                typeInfo = typeInfo.BaseType;
            }

            return(attrs);
        }
예제 #5
0
        protected virtual ITestCondition GetTestCondition <TType>(ITypeInfo type) where TType : ITestCondition
        {
            var attribute = type.GetCustomAttributes(typeof(TType)).FirstOrDefault();

            return(attribute != null
                ? (TType)Activator.CreateInstance(typeof(TType), attribute.GetConstructorArguments().ToArray())
                : null);
        }
예제 #6
0
 public static DynamoDBAttribute GetAttribute(ITypeInfo targetTypeInfo)
 {
     if (targetTypeInfo == null)
     {
         throw new ArgumentNullException("targetTypeInfo");
     }
     object[] attributes = targetTypeInfo.GetCustomAttributes(TypeFactory.GetTypeInfo(typeof(DynamoDBAttribute)), true);
     return(GetSingleDDBAttribute(attributes));
 }
        /// <inheritdoc/>
        public ITestCollection Get(ITypeInfo testClass)
        {
            var collectionAttribute = testClass.GetCustomAttributes(typeof(CollectionAttribute)).SingleOrDefault();
            if (collectionAttribute == null)
                return defaultCollection;

            var collectionName = (string)collectionAttribute.GetConstructorArguments().First();
            return testCollections.GetOrAdd(collectionName, CreateTestCollection);
        }
        private static IEnumerable <HtmlTargetElementAttribute> GetValidHtmlTargetElementAttributes(
            ITypeInfo typeInfo,
            ErrorSink errorSink)
        {
            var targetElementAttributes = typeInfo.GetCustomAttributes <HtmlTargetElementAttribute>();

            return(targetElementAttributes.Where(
                       attribute => ValidHtmlTargetElementAttributeNames(attribute, errorSink)));
        }
        protected virtual int GetOrder(ITestCollection col)
        {
            ITypeInfo type =
                col.CollectionDefinition
                ?? col.TestAssembly.Assembly.GetType(GetCollectionTypeName(col));

            return(type == null
                                ? 0
                                : ExtractOrderFromAttribute(type.GetCustomAttributes(typeof(OrderAttribute))));
        }
예제 #10
0
        protected override bool IsValidTestClass(ITypeInfo type)
        {
            // only classes marked as test cases are valid
            var isTestCase = type.GetCustomAttributes(typeof(TestCaseAttribute)).Any();

            if (!isTestCase)
            {
                DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Class {type.Name} is not a test case."));
            }
            return(isTestCase);
        }
예제 #11
0
        /// <summary>
        /// Return the first found custom attribute
        /// </summary>
        /// <param name="attributeType">type of the custom attribute</param>
        /// <returns>the first found custom attribute; or null none is found</returns>
        public static IAttributeInfo GetFirstCustomAttribute(this ITypeInfo me, Type attributeType)
        {
            IAttributeInfo retval = null;

            foreach (var a in me.GetCustomAttributes(attributeType))
            {
                retval = a;
                break;
            }

            return(retval);
        }
예제 #12
0
        /// <inheritdoc/>
        public ITestCollection Get(ITypeInfo testClass)
        {
            var collectionAttribute = testClass.GetCustomAttributes(typeof(CollectionAttribute)).SingleOrDefault();

            if (collectionAttribute == null)
            {
                return(defaultCollection);
            }

            var collectionName = (string)collectionAttribute.GetConstructorArguments().First();

            return(testCollections.GetOrAdd(collectionName, CreateTestCollection));
        }
        public static string GetSubject(this ITypeInfo type)
        {
            var attributes = type.GetCustomAttributes(FullNames.SubjectAttribute, true)
                             .ToArray();

            if (!attributes.Any())
            {
                return(type.GetContainingType()?.GetSubject() ?? string.Empty);
            }

            return(attributes.First()
                   .GetParameters()
                   .Join(" "));
        }
예제 #14
0
        public static ITypeInfo GetRunWith(ITypeInfo type)
        {
            foreach (IAttributeInfo attributeInfo in type.GetCustomAttributes(typeof(RunWithAttribute)))
            {
                RunWithAttribute attribute = attributeInfo.GetInstance<RunWithAttribute>();
                if (attribute == null || attribute.TestClassCommand == null)
                    continue;

                ITypeInfo typeInfo = Reflector.Wrap(attribute.TestClassCommand);
                if (ImplementsITestClassCommand(typeInfo))
                    return typeInfo;
            }

            return null;
        }
예제 #15
0
        public static T[] GetCustomAttributes <T>(this ITypeInfo me) where T : Attribute
        {
            var retvals = new List <T>();

            foreach (var a in me.GetCustomAttributes(typeof(T)))
            {
                var attr = a.GetInstance <T>();
                if (attr != null)
                {
                    retvals.Add(attr);
                }
            }

            return(retvals.ToArray());
        }
        /// <inheritdoc/>
        public ITestCollection Get(ITypeInfo testClass)
        {
            string collectionName;
            var    collectionAttribute = testClass.GetCustomAttributes(typeof(CollectionAttribute)).SingleOrDefault();

            if (collectionAttribute == null)
            {
                collectionName = "Test collection for " + testClass.Name;
            }
            else
            {
                collectionName = (string)collectionAttribute.GetConstructorArguments().First();
            }

            return(testCollections.GetOrAdd(collectionName, CreateCollection));
        }
예제 #17
0
        public static ITypeInfo GetRunWith(ITypeInfo type)
        {
            foreach (IAttributeInfo attributeInfo in type.GetCustomAttributes(typeof(RunWithAttribute)))
            {
                RunWithAttribute attribute = attributeInfo.GetInstance <RunWithAttribute>();
                if (attribute == null || attribute.TestClassCommand == null)
                {
                    continue;
                }

                ITypeInfo typeInfo = Reflector.Wrap(attribute.TestClassCommand);
                if (ImplementsITestClassCommand(typeInfo))
                {
                    return(typeInfo);
                }
            }

            return(null);
        }
예제 #18
0
        /// <summary>
        /// Return the first found custom attribute
        /// </summary>
        /// <param name="attributeType">type of the custome attribute</param>
        /// <returns>the first found custom attribute; or null none is found</returns>
        public static T GetFirstCustomAttribute <T>(this ITypeInfo me) where T : Attribute
        {
            IAttributeInfo attrInfo = null;

            foreach (var a in me.GetCustomAttributes(typeof(T)))
            {
                attrInfo = a;
                break;
            }

            if (attrInfo != null)
            {
                return(attrInfo.GetInstance <T>());
            }
            else
            {
                return(null);
            }
        }
예제 #19
0
        void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
        {
            string displayNameBase = factAttribute.GetNamedArgument <string>("DisplayName") ?? type.Name + "." + method.Name;

            ITypeInfo[] resolvedTypes = null;

            if (arguments != null && method.IsGenericMethodDefinition)
            {
                resolvedTypes = ResolveGenericTypes(method, arguments);
                method        = method.MakeGenericMethod(resolvedTypes);
            }

            Assembly       = assembly;
            Class          = type;
            Method         = method;
            Arguments      = arguments;
            DisplayName    = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
            SkipReason     = factAttribute.GetNamedArgument <string>("Skip");
            Traits         = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);
            TestCollection = testCollection;

            foreach (var traitAttribute in Method.GetCustomAttributes(typeof(ITraitAttribute))
                     .Concat(Class.GetCustomAttributes(typeof(ITraitAttribute))))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).First();
                var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                var discovererType = Reflector.GetType(args[1], args[0]);

                if (discovererType != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererType);

                    foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                    {
                        Traits.Add(keyValuePair.Key, keyValuePair.Value);
                    }
                }
            }

            uniqueID = new Lazy <string>(GetUniqueID, true);
        }
예제 #20
0
 public static MultiValueDictionary <string, string> SafelyGetTraits(this ITypeInfo typeInfo)
 {
     try
     {
         // GetTraits relies on our attribute parsing to get at a property
         // value. Unless that value is set in the attribute constructor
         // via a named parameter (property) or a parameter with a name
         // similar to the required property, we'll return null, and that
         // can cause exceptions. If we get an exception, fail as nicely
         // as we can, with an empty collection of traits
         var multiValueDictionary = new MultiValueDictionary <string, string>();
         foreach (var attributeInfo in typeInfo.GetCustomAttributes(typeof(TraitAttribute)))
         {
             multiValueDictionary.AddValue(attributeInfo.GetPropertyValue <string>("Name"), attributeInfo.GetPropertyValue <string>("Value"));
         }
         return(multiValueDictionary);
     }
     catch (Exception)
     {
         return(new MultiValueDictionary <string, string>());
     }
 }
        private static IEnumerable <string> GetAllowedChildren(ITypeInfo typeInfo, ErrorSink errorSink)
        {
            var restrictChildrenAttribute = typeInfo
                                            .GetCustomAttributes <RestrictChildrenAttribute>()
                                            .FirstOrDefault();

            if (restrictChildrenAttribute == null)
            {
                return(null);
            }

            var allowedChildren      = restrictChildrenAttribute.ChildTags;
            var validAllowedChildren = GetValidAllowedChildren(allowedChildren, typeInfo.FullName, errorSink);

            if (validAllowedChildren.Any())
            {
                return(validAllowedChildren);
            }
            else
            {
                // All allowed children were invalid, return null to indicate that any child is acceptable.
                return(null);
            }
        }
예제 #22
0
 private static IEnumerable <IAttributeInfo> GetCachedTraitAttributes(ITypeInfo type)
 => typeTraitAttributeCache.GetOrAdd(type.Name, () => type.GetCustomAttributes(typeof(ITraitAttribute)));
예제 #23
0
 public static DynamoDBAttribute GetAttribute(ITypeInfo targetTypeInfo)
 {
     if (targetTypeInfo == null) throw new ArgumentNullException("targetTypeInfo");
     object[] attributes = targetTypeInfo.GetCustomAttributes(TypeFactory.GetTypeInfo(typeof(DynamoDBAttribute)), true);
     return GetSingleDDBAttribute(attributes);
 }
 public static IEnumerable <string> GetTags(this ITypeInfo type)
 {
     return(type.GetCustomAttributes(FullNames.TagsAttribute, false)
            .SelectMany(x => x.GetParameters())
            .Distinct());
 }
예제 #25
0
        /// <summary>
        /// We look for attributes implementing IFixtureBuilder at one level 
        /// of inheritance at a time. Attributes on base classes are not used 
        /// unless there are no fixture builder attributes at all on the derived
        /// class. This is by design.
        /// </summary>
        /// <param name="typeInfo">The type being examined for attributes</param>
        /// <returns>A list of the attributes found.</returns>
        private IFixtureBuilder[] GetFixtureBuilderAttributes(ITypeInfo typeInfo)
        {
            IFixtureBuilder[] attrs = new IFixtureBuilder[0];

            while (typeInfo != null && !typeInfo.IsType(typeof(object)))
            {
                attrs = typeInfo.GetCustomAttributes<IFixtureBuilder>(false);

                if (attrs.Length > 0)
                {
                    // We want to eliminate duplicates that have no args.
                    // If there is just one, no duplication is possible.
                    if (attrs.Length == 1)
                        return attrs;

                    // Count how many have arguments
                    int withArgs = 0;
                    foreach (var attr in attrs)
                        if (HasArguments(attr))
                            withArgs++;

                    // If all have args, just return them
                    if (withArgs == attrs.Length)
                        return attrs;

                    // If none of them have args, return the first one
                    if (withArgs == 0)
                        return new IFixtureBuilder[] { attrs[0] };
                    
                    // Some of each - extract those with args
                    var result = new IFixtureBuilder[withArgs];
                    int count = 0;
                    foreach (var attr in attrs)
                        if (HasArguments(attr))
                            result[count++] = attr;

                    return result;
                }

                typeInfo = typeInfo.BaseType;
            }

            return attrs;
        }
 private static IEnumerable<ParadigmDataAttribute> GetDataAttributes(ITypeInfo type)
 {
     return type.GetCustomAttributes(typeof(ParadigmDataAttribute))
         .Select(x => x.GetInstance<ParadigmDataAttribute>());
 }
예제 #27
0
        public static string EvaluateSkipConditions(this ITestMethod testMethod)
        {
            ITypeInfo     testClass = testMethod.TestClass.Class;
            IAssemblyInfo assembly  = testMethod.TestClass.TestCollection.TestAssembly.Assembly;

            System.Collections.Generic.IEnumerable <System.Attribute> conditionAttributes = testMethod.Method
                                                                                            .GetCustomAttributes(typeof(ITestCondition))
                                                                                            .Concat(testClass.GetCustomAttributes(typeof(ITestCondition)))
                                                                                            .Concat(assembly.GetCustomAttributes(typeof(ITestCondition)))
                                                                                            .OfType <ReflectionAttributeInfo>()
                                                                                            .Select(attributeInfo => attributeInfo.Attribute);

            foreach (ITestCondition condition in conditionAttributes)
            {
                if (!condition.IsMet)
                {
                    return(condition.SkipReason);
                }
            }

            return(null);
        }
 public static bool IsIgnored(this ITypeInfo type)
 {
     return(type.GetCustomAttributes(FullNames.IgnoreAttribute, false).Any());
 }
예제 #29
0
 public static IEnumerable <IAttributeInfo> GetCustomAttributes(this ITypeInfo typeInfo, Type attributeType)
 {
     return(typeInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName));
 }
 private bool HasMatchingAttribute(ITypeInfo testClass)
 {
     return(ContainsMatchingAttribute(testClass.GetCustomAttributes(typeof(TAttribute))));
 }
 public IEnumerable <IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName)
 {
     return(_typeInfoImplementation.GetCustomAttributes(assemblyQualifiedAttributeTypeName));
 }
예제 #32
0
 public IEnumerable <IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName)
 {
     return(inner.GetCustomAttributes(assemblyQualifiedAttributeTypeName));
 }
예제 #33
0
        void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
        {
            string displayNameBase = factAttribute.GetNamedArgument<string>("DisplayName") ?? type.Name + "." + method.Name;
            ITypeInfo[] resolvedTypes = null;

            if (arguments != null && method.IsGenericMethodDefinition)
            {
                resolvedTypes = ResolveGenericTypes(method, arguments);
                method = method.MakeGenericMethod(resolvedTypes);
            }

            Assembly = assembly;
            Class = type;
            Method = method;
            Arguments = arguments;
            DisplayName = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
            SkipReason = factAttribute.GetNamedArgument<string>("Skip");
            Traits = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
            TestCollection = testCollection;

            foreach (var traitAttribute in Method.GetCustomAttributes(typeof(ITraitAttribute))
                                                 .Concat(Class.GetCustomAttributes(typeof(ITraitAttribute))))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).First();
                var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererAttribute);
                if (discoverer != null)
                    foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        Traits.Add(keyValuePair.Key, keyValuePair.Value);
            }

            uniqueID = new Lazy<string>(GetUniqueID, true);
        }
 public static bool IsBehaviorContainer(this ITypeInfo type)
 {
     return(!type.IsAbstract &&
            type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() &&
            type.GetFields().Any(x => x.IsSpecification()));
 }
예제 #35
0
 private static Type GetClusterFromIntegrationAttribute(ITypeInfo testClass) =>
 testClass.GetCustomAttributes(typeof(IntegrationTestClusterAttribute))
 .FirstOrDefault()?.GetNamedArgument <Type>(nameof(IntegrationTestClusterAttribute.ClusterType));
 public T[] GetCustomAttributes <T>(bool inherit) where T : class
 {
     return(_baseInfo.GetCustomAttributes <T>(inherit));
 }
        private static IEnumerable<HtmlTargetElementAttribute> GetValidHtmlTargetElementAttributes(
            ITypeInfo typeInfo,
            ErrorSink errorSink)
        {
            var targetElementAttributes = typeInfo.GetCustomAttributes<HtmlTargetElementAttribute>();

            return targetElementAttributes.Where(
                attribute => ValidHtmlTargetElementAttributeNames(attribute, errorSink));
        }
예제 #38
0
        public static OneToSetMap <string, string> GetTraits(this ITypeInfo typeInfo)
        {
            var attributes = typeInfo.GetCustomAttributes(typeof(TraitAttribute));

            return(GetTraitsFromAttributes(attributes));
        }
        private static IEnumerable<string> GetAllowedChildren(ITypeInfo typeInfo, ErrorSink errorSink)
        {
            var restrictChildrenAttribute = typeInfo
                .GetCustomAttributes<RestrictChildrenAttribute>()
                .FirstOrDefault();
            if (restrictChildrenAttribute == null)
            {
                return null;
            }

            var allowedChildren = restrictChildrenAttribute.ChildTags;
            var validAllowedChildren = GetValidAllowedChildren(allowedChildren, typeInfo.FullName, errorSink);

            if (validAllowedChildren.Any())
            {
                return validAllowedChildren;
            }
            else
            {
                // All allowed children were invalid, return null to indicate that any child is acceptable.
                return null;
            }
        }