Esempio n. 1
0
        /// <summary>
        /// Get the traits from a method.
        /// </summary>
        /// <param name="member">The member (method, field, etc.) to get the traits for.</param>
        /// <returns>A list of traits that are defined on the method.</returns>
        public static IReadOnlyList <KeyValuePair <string, string> > GetTraits(MemberInfo member)
        {
            Guard.ArgumentNotNull(member);

            var result = new List <KeyValuePair <string, string> >();

            foreach (var traitAttributeData in member.CustomAttributes)
            {
                var traitAttributeType = traitAttributeData.AttributeType;
                if (!typeof(ITraitAttribute).IsAssignableFrom(traitAttributeType))
                {
                    continue;
                }

                var discovererAttributeData = FindDiscovererAttributeType(traitAttributeType);
                if (discovererAttributeData == null)
                {
                    continue;
                }

                var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(Reflector.Wrap(discovererAttributeData));
                if (discoverer == null)
                {
                    continue;
                }

                var traits = discoverer.GetTraits(Reflector.Wrap(traitAttributeData));
                if (traits != null)
                {
                    result.AddRange(traits);
                }
            }

            return(result);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override void Initialize()
        {
            base.Initialize();

            var factAttribute   = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();
            var baseDisplayName = factAttribute.GetNamedArgument <string>("DisplayName") ?? BaseDisplayName;

            DisplayName = TypeUtility.GetDisplayNameWithArguments(TestMethod.Method, baseDisplayName, TestMethodArguments, MethodGenericTypes);
            SkipReason  = factAttribute.GetNamedArgument <string>("Skip");

            foreach (var traitAttribute in TestMethod.Method.GetCustomAttributes(typeof(ITraitAttribute))
                     .Concat(TestMethod.TestClass.Class.GetCustomAttributes(typeof(ITraitAttribute))))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
                if (discovererAttribute != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(diagnosticMessageSink, discovererAttribute);
                    if (discoverer != null)
                    {
                        foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        {
                            Traits.Add(keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                else
                {
                    diagnosticMessageSink.OnMessage(new DiagnosticMessage("Trait attribute on '{0}' did not have [TraitDiscoverer]", DisplayName));
                }
            }
        }
Esempio n. 3
0
        /// <inheritdoc/>
        protected override void Initialize()
        {
            base.Initialize();

            var factAttribute   = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();
            var baseDisplayName = factAttribute.GetNamedArgument <string>("DisplayName") ?? BaseDisplayName;

            DisplayName = TypeUtility.GetDisplayNameWithArguments(TestMethod.Method, baseDisplayName, TestMethodArguments, MethodGenericTypes);
            SkipReason  = factAttribute.GetNamedArgument <string>("Skip");

            foreach (var traitAttribute in TestMethod.Method.GetCustomAttributes(typeof(ITraitAttribute))
                     .Concat(TestMethod.TestClass.Class.GetCustomAttributes(typeof(ITraitAttribute))))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
                if (discovererAttribute != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererAttribute);
                    if (discoverer != null)
                    {
                        foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        {
                            Traits.Add(keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                // TODO: Log an environmental warning about the missing discoverer attribute
            }
        }
Esempio n. 4
0
        /// <inheritdoc/>
        protected override void Initialize()
        {
            base.Initialize();

            var factAttribute   = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).First();
            var baseDisplayName = factAttribute.GetNamedArgument <string>("DisplayName") ?? BaseDisplayName;

            DisplayName = GetDisplayName(factAttribute, baseDisplayName);
            SkipReason  = GetSkipReason(factAttribute);

            foreach (var traitAttribute in GetTraitAttributesData(TestMethod))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
                if (discovererAttribute != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(DiagnosticMessageSink, discovererAttribute);
                    if (discoverer != null)
                    {
                        foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        {
                            Traits.Add(keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                else
                {
                    DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Trait attribute on '{DisplayName}' did not have [TraitDiscoverer]"));
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get the traits from a method.
        /// </summary>
        /// <param name="member">The member (method, field, etc.) to get the traits for.</param>
        /// <returns>A list of traits that are defined on the method.</returns>
        public static IReadOnlyList <KeyValuePair <string, string> > GetTraits(MemberInfo member)
        {
            var messageSink = new NullMessageSink();
            var result      = new List <KeyValuePair <string, string> >();

            foreach (var traitAttributeData in member.CustomAttributes)
            {
                var traitAttributeType = traitAttributeData.AttributeType;
                if (!typeof(ITraitAttribute).GetTypeInfo().IsAssignableFrom(traitAttributeType.GetTypeInfo()))
                {
                    continue;
                }

                var discovererAttributeData = traitAttributeType.GetTypeInfo().CustomAttributes.FirstOrDefault(cad => cad.AttributeType == typeof(TraitDiscovererAttribute));
                if (discovererAttributeData == null)
                {
                    continue;
                }

                var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(messageSink, Reflector.Wrap(discovererAttributeData));
                if (discoverer == null)
                {
                    continue;
                }

                var traits = discoverer.GetTraits(Reflector.Wrap(traitAttributeData));
                if (traits != null)
                {
                    result.AddRange(traits);
                }
            }

            return(result);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        void Initialize(ITestMethod testMethod, object[] arguments)
        {
            var factAttribute   = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();
            var type            = testMethod.TestClass.Class;
            var method          = testMethod.Method;
            var baseDisplayName = factAttribute.GetNamedArgument <string>("DisplayName") ?? type.Name + "." + method.Name;

            ITypeInfo[] resolvedTypes = null;

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

            Method              = method;
            TestMethod          = testMethod;
            TestMethodArguments = arguments;
            DisplayName         = TypeUtility.GetDisplayNameWithArguments(method, baseDisplayName, arguments, resolvedTypes);
            SkipReason          = factAttribute.GetNamedArgument <string>("Skip");
            Traits              = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);

            foreach (var traitAttribute in method.GetCustomAttributes(typeof(ITraitAttribute))
                     .Concat(type.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);
        }