コード例 #1
0
ファイル: UnresolvedType.cs プロジェクト: soelske/mbunit-v3
        private MemberInfo[] GetMembers(string name, MemberTypes type, BindingFlags bindingAttr)
        {
            var members = new List <MemberInfo>();

            if ((type & MemberTypes.Constructor) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetConstructors(bindingAttr));
            }
            if ((type & MemberTypes.Event) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetEvents(bindingAttr));
            }
            if ((type & MemberTypes.Field) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetFields(bindingAttr));
            }
            if ((type & MemberTypes.Method) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetMethods(bindingAttr));
            }
            if ((type & MemberTypes.Property) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetProperties(bindingAttr));
            }
            if ((type & MemberTypes.NestedType) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetNestedTypes(bindingAttr));
            }

            return(members.ToArray());
        }
コード例 #2
0
        /// <summary>
        /// Core implementation to discover unit tests in a given test class.
        /// </summary>
        /// <param name="type">The test class.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to attempt to include source information.</param>
        /// <param name="messageBus">The message sink to send discovery messages to.</param>
        /// <returns>Returns <c>true</c> if discovery should continue; <c>false</c> otherwise.</returns>
        protected virtual bool FindImpl(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            var    testCollection   = TestCollectionFactory.Get(type);

            try
            {
                if (!String.IsNullOrEmpty(assemblyInfo.AssemblyPath))
                {
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyInfo.AssemblyPath));
                }

                foreach (var method in type.GetMethods(includePrivateMethods: true))
                {
                    var factAttribute = method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
                    if (factAttribute != null)
                    {
                        var discovererAttribute = factAttribute.GetCustomAttributes(typeof(TestCaseDiscovererAttribute)).FirstOrDefault();
                        if (discovererAttribute != null)
                        {
                            var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                            var discovererType = Reflector.GetType(args[1], args[0]);
                            if (discovererType != null)
                            {
                                var discoverer = GetDiscoverer(discovererType);

                                if (discoverer != null)
                                {
                                    foreach (var testCase in discoverer.Discover(testCollection, assemblyInfo, type, method, factAttribute))
                                    {
                                        if (!messageBus.QueueMessage(new TestCaseDiscoveryMessage(UpdateTestCaseWithSourceInfo(testCase, includeSourceInformation))))
                                        {
                                            return(false);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                messageAggregator.Add(new EnvironmentalWarning {
                                    Message = String.Format("Could not create discoverer type '{0}, {1}'", args[0], args[1])
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                messageAggregator.Add(new EnvironmentalWarning {
                    Message = String.Format("Exception during discovery:{0}{1}", Environment.NewLine, ex)
                });
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }

            return(true);
        }
コード例 #3
0
 protected override IMethodInfo[] GetMethodsToImplement(ITypeInfo controllerType)
 {
     return(controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
            .Where(m => !((MethodWrapper)m).Method.IsSpecialName)
            .Where(x => x.DeclaringType.Equals(controllerType))
            .ToArray());
 }
コード例 #4
0
 public IEnumerable <XunitMethodInfo> GetMethods()
 {
     foreach (IMethodInfo method in target.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
     {
         yield return(new XunitMethodInfoAdapter(method));
     }
 }
コード例 #5
0
 /// <summary>
 /// Retrieves a list of the test methods from the test class.
 /// </summary>
 /// <param name="type">The type to be inspected</param>
 /// <returns>The test methods</returns>
 public static IEnumerable <IMethodInfo> GetTestMethods(ITypeInfo type)
 {
     foreach (IMethodInfo method in type.GetMethods())
     {
         if (MethodUtility.IsTest(method))
         {
             yield return(method);
         }
     }
 }
コード例 #6
0
        private static IMethodInfo GetMethodWithAttribute <T>(ITypeInfo type)
        {
            foreach (IMethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (AttributeUtils.HasAttribute <T>(method, true))
                {
                    return(method);
                }
            }

            return(null);
        }
コード例 #7
0
ファイル: ReflectorTests.cs プロジェクト: zvirja/xunit
            public void ReturnsPublicAndPrivateStaticAndNonStaticMethods()
            {
                ITypeInfo typeInfo = Reflector.Wrap(typeof(TestClass));

                List <IMethodInfo> methods = new List <IMethodInfo>(typeInfo.GetMethods());

                foreach (string name in new string[] { "PrivateMethod", "PrivateStaticMethod", "PublicMethod", "PublicStaticMethod" })
                {
                    Assert.NotNull(methods.Find(methodInfo => methodInfo.Name == name));
                }

                Assert.Null(methods.Find(methodInfo => methodInfo.Name == "Property"));
            }
コード例 #8
0
        private void PopulateTestFixture(Test fixtureTest, ITypeInfo type, TestFixtureAttribute2 fixtureAttrib)
        {
            IMethodInfo setUpMethod    = GetMethodWithAttribute <SetUpAttribute2>(type);
            IMethodInfo tearDownMethod = GetMethodWithAttribute <TearDownAttribute2>(type);

            string namePrefix = setUpMethod != null ? setUpMethod.Name + @"." : string.Empty;
            string nameSuffix = tearDownMethod != null ? @"." + tearDownMethod.Name : string.Empty;

            foreach (IMethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                BuildTestsFromMethod(fixtureTest, method, namePrefix, nameSuffix);
            }
        }
コード例 #9
0
        /// <summary>
        /// Returns the methods marked by given type of attribute
        /// </summary>
        /// <param name="me">this</param>
        /// <param name="attribute">target attribute type</typeparam>
        /// <returns>Array of the found method, return zero length array if nothing is found.</returns>
        public static IMethodInfo[] GetMethodMarkedByAttribute(this ITypeInfo me, Type attribute)
        {
            var retval = new List <IMethodInfo>();

            foreach (var m in me.GetMethods())
            {
                if (m.HasAttribute(attribute))
                {
                    retval.Add(m);
                }
            }

            return(retval.ToArray());
        }
コード例 #10
0
        /// <inheritdoc/>
        protected override bool FindTestsForType(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus)
        {
            var testCollection = TestCollectionFactory.Get(type);

            foreach (var method in type.GetMethods(includePrivateMethods: true))
            {
                if (!FindTestsForMethod(testCollection, type, method, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #11
0
ファイル: MSTestExplorer.cs プロジェクト: citizenmatt/gallio
        private MSTest CreateTypeTest(ITypeInfo typeInfo)
        {
            MSTest typeTest = new MSTest(typeInfo.Name, typeInfo);

            typeTest.Kind = TestKinds.Fixture;

            foreach (IMethodInfo method in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                IEnumerable <IAttributeInfo> methodAttributes = method.GetAttributeInfos(null, true);
                foreach (IAttributeInfo methodAttribute in methodAttributes)
                {
                    if (methodAttribute.Type.FullName.CompareTo(MSTestAttributes.TestMethodAttribute) == 0)
                    {
                        try
                        {
                            MSTest testMethod = CreateMethodTest(typeInfo, method);
                            typeTest.AddChild(testMethod);
                        }
                        catch (Exception ex)
                        {
                            TestModel.AddAnnotation(new Annotation(AnnotationType.Error, method, "An exception was thrown while exploring an MSTest test method.", ex));
                        }
                        break;
                    }
                }
            }

            PopulateTestClassMetadata(typeInfo, typeTest);

            // Add XML documentation.
            string xmlDocumentation = typeInfo.GetXmlDocumentation();

            if (xmlDocumentation != null)
            {
                typeTest.Metadata.SetValue(MetadataKeys.XmlDocumentation, xmlDocumentation);
            }

            return(typeTest);
        }
コード例 #12
0
        /// <summary>
        /// Infers whether the type is a test type based on its structure.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Returns true if the type any associated patterns, if it has
        /// non-nested type members (subject to <see cref="GetMemberBindingFlags" />)
        /// with patterns, if it has generic parameters with patterns, or if any
        /// of its nested types satisfy the preceding rules.
        /// </para>
        /// </remarks>
        /// <param name="evaluator">The pattern evaluator.</param>
        /// <param name="type">The type.</param>
        /// <returns>True if the type is likely a test type.</returns>
        protected virtual bool InferTestType(IPatternEvaluator evaluator, ITypeInfo type)
        {
            if (evaluator.HasPatterns(type))
            {
                return(true);
            }

            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            if (HasCodeElementWithPattern(evaluator, type.GetMethods(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetProperties(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetFields(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetEvents(bindingFlags)))
            {
                return(true);
            }

            if (type.IsGenericTypeDefinition && HasCodeElementWithPattern(evaluator, type.GenericArguments))
            {
                return(true);
            }

            if (ShouldConsumeConstructors(type) &&
                HasCodeElementWithPattern(evaluator, type.GetConstructors(ConstructorBindingFlags)))
            {
                return(true);
            }

            foreach (ITypeInfo nestedType in type.GetNestedTypes(NestedTypeBindingFlags))
            {
                if (InferTestType(evaluator, nestedType))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #13
0
        /// <summary>
        /// Consumes type members including fields, properties, methods and events.
        /// </summary>
        /// <param name="typeScope">The scope to be used as the containing scope.</param>
        /// <param name="type">The type whose members are to be consumed.</param>
        protected void ConsumeMembers(IPatternScope typeScope, ITypeInfo type)
        {
            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            // TODO: We should probably process groups of members in sorted order working outwards
            //       from the base type, like an onion.
            foreach (IFieldInfo field in CodeElementSorter.SortMembersByDeclaringType(type.GetFields(bindingFlags)))
            {
                typeScope.Consume(field, false, DefaultFieldPattern);
            }

            foreach (IPropertyInfo property in CodeElementSorter.SortMembersByDeclaringType(type.GetProperties(bindingFlags)))
            {
                typeScope.Consume(property, false, DefaultPropertyPattern);
            }

            foreach (IMethodInfo method in CodeElementSorter.SortMembersByDeclaringType(type.GetMethods(bindingFlags)))
            {
                typeScope.Consume(method, false, DefaultMethodPattern);
            }

            foreach (IEventInfo @event in CodeElementSorter.SortMembersByDeclaringType(type.GetEvents(bindingFlags)))
            {
                typeScope.Consume(@event, false, DefaultEventPattern);
            }
        }
コード例 #14
0
ファイル: TypeUtility.cs プロジェクト: JoB70/xunit
 /// <summary>
 /// Retrieves a list of the test methods from the test class.
 /// </summary>
 /// <param name="type">The type to be inspected</param>
 /// <returns>The test methods</returns>
 public static IEnumerable<IMethodInfo> GetTestMethods(ITypeInfo type)
 {
     foreach (IMethodInfo method in type.GetMethods())
         if (MethodUtility.IsTest(method))
             yield return method;
 }
コード例 #15
0
 private static IEnumerable <IMethodInfo> GetTestMethodsFromClass(ITypeInfo @class)
 => @class.GetMethods(includePrivateMethods: true).Where(m => m.GetCustomAttributes(typeof(TestCaseComponentAttribute)).Any());
 protected override IMethodInfo[] GetMethodsToImplement(ITypeInfo controllerType)
 {
     return(controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).ToArray());
 }
コード例 #17
0
        private MSTest CreateTypeTest(ITypeInfo typeInfo)
        {
            MSTest typeTest = new MSTest(typeInfo.Name, typeInfo);
            typeTest.Kind = TestKinds.Fixture;

            foreach (IMethodInfo method in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                IEnumerable<IAttributeInfo> methodAttributes = method.GetAttributeInfos(null, true);
                foreach (IAttributeInfo methodAttribute in methodAttributes)
                {
                    if (methodAttribute.Type.FullName.CompareTo(MSTestAttributes.TestMethodAttribute) == 0)
                    {
                        try
                        {
                            MSTest testMethod = CreateMethodTest(typeInfo, method);
                            typeTest.AddChild(testMethod);
                        }
                        catch (Exception ex)
                        {
                            TestModel.AddAnnotation(new Annotation(AnnotationType.Error, method, "An exception was thrown while exploring an MSTest test method.", ex));
                        }
                        break;
                    }
                }
            }

            PopulateTestClassMetadata(typeInfo, typeTest);

            // Add XML documentation.
            string xmlDocumentation = typeInfo.GetXmlDocumentation();
            if (xmlDocumentation != null)
                typeTest.Metadata.SetValue(MetadataKeys.XmlDocumentation, xmlDocumentation);

            return typeTest;
        }
コード例 #18
0
        /// <summary>
        /// Core implementation to discover unit tests in a given test class.
        /// </summary>
        /// <param name="type">The test class.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to attempt to include source information.</param>
        /// <param name="messageBus">The message sink to send discovery messages to.</param>
        /// <returns>Returns <c>true</c> if discovery should continue; <c>false</c> otherwise.</returns>
        protected virtual bool FindImpl(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            var testCollection = TestCollectionFactory.Get(type);

            try
            {
                if (!String.IsNullOrEmpty(assemblyInfo.AssemblyPath))
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyInfo.AssemblyPath));

                foreach (var method in type.GetMethods(includePrivateMethods: true))
                {
                    var factAttribute = method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
                    if (factAttribute != null)
                    {
                        var discovererAttribute = factAttribute.GetCustomAttributes(typeof(TestCaseDiscovererAttribute)).FirstOrDefault();
                        if (discovererAttribute != null)
                        {
                            var args = discovererAttribute.GetConstructorArguments().Cast<string>().ToList();
                            var discovererType = Reflector.GetType(args[1], args[0]);
                            if (discovererType != null)
                            {
                                var discoverer = GetDiscoverer(discovererType);

                                if (discoverer != null)
                                    foreach (var testCase in discoverer.Discover(testCollection, assemblyInfo, type, method, factAttribute))
                                        if (!messageBus.QueueMessage(new TestCaseDiscoveryMessage(UpdateTestCaseWithSourceInfo(testCase, includeSourceInformation))))
                                            return false;
                            }
                            else
                                messageAggregator.Add(new EnvironmentalWarning { Message = String.Format("Could not create discoverer type '{0}, {1}'", args[0], args[1]) });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                messageAggregator.Add(new EnvironmentalWarning { Message = String.Format("Exception during discovery:{0}{1}", Environment.NewLine, ex) });
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }

            return true;
        }
コード例 #19
0
        /// <inheritdoc/>
        protected override bool FindTestsForType(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus)
        {
            var testCollection = TestCollectionFactory.Get(type);

            foreach (var method in type.GetMethods(includePrivateMethods: true))
                if (!FindTestsForMethod(testCollection, type, method, includeSourceInformation, messageBus))
                    return false;

            return true;
        }
コード例 #20
0
        /// <summary>
        /// Core implementation to discover unit tests in a given test class.
        /// </summary>
        /// <param name="type">The test class.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to attempt to include source information.</param>
        /// <param name="messageSink">The message sink to send discovery messages to.</param>
        /// <returns>Returns <c>true</c> if discovery should continue; <c>false</c> otherwise.</returns>
        protected virtual bool FindImpl(ITypeInfo type, bool includeSourceInformation, IMessageSink messageSink)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            var testCollection = TestCollectionFactory.Get(type);

            try
            {
                if (!String.IsNullOrEmpty(assemblyInfo.AssemblyPath))
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyInfo.AssemblyPath));

                foreach (IMethodInfo method in type.GetMethods(includePrivateMethods: true))
                {
                    IAttributeInfo factAttribute = method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
                    if (factAttribute != null)
                    {
                        IAttributeInfo discovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitDiscovererAttribute)).FirstOrDefault();
                        if (discovererAttribute != null)
                        {
                            var args = discovererAttribute.GetConstructorArguments().Cast<string>().ToList();
                            var discovererType = Reflector.GetType(args[1], args[0]);
                            if (discovererType != null)
                            {
                                IXunitDiscoverer discoverer = (IXunitDiscoverer)Activator.CreateInstance(discovererType);

                                foreach (XunitTestCase testCase in discoverer.Discover(testCollection, assemblyInfo, type, method, factAttribute))
                                    if (!messageSink.OnMessage(new TestCaseDiscoveryMessage(UpdateTestCaseWithSourceInfo(testCase, includeSourceInformation))))
                                        return false;
                            }
                            // TODO: Figure out a way to report back an error when discovererType is not available
                            // TODO: What if the discovererType can't be created or cast to IXunitDiscoverer?
                            // TODO: Performance optimization: cache instances of the discoverer type
                        }
                    }
                }

                return true;
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }
        }
コード例 #21
0
 public IMethodInfo[] GetMethods(BindingFlags flags)
 {
     return(_baseInfo.GetMethods(flags)
            .Select(info => new CustomMethodWrapper(info, _extraMethodAttributes))
            .ToArray());
 }
コード例 #22
0
 public IEnumerable <IMethodInfo> GetMethods(bool includePrivateMethods)
 {
     return(_typeInfoImplementation.GetMethods(includePrivateMethods));
 }
コード例 #23
0
 public IEnumerable <IMethodInfo> GetMethods(bool includePrivateMethods)
 {
     return(inner.GetMethods(includePrivateMethods));
 }