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()); }
/// <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); }
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()); }
public IEnumerable <XunitMethodInfo> GetMethods() { foreach (IMethodInfo method in target.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { yield return(new XunitMethodInfoAdapter(method)); } }
/// <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); } } }
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); }
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")); }
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); } }
/// <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()); }
/// <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); }
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); }
/// <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); }
/// <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); } }
/// <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; }
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()); }
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; }
/// <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; }
/// <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; }
/// <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); } }
public IMethodInfo[] GetMethods(BindingFlags flags) { return(_baseInfo.GetMethods(flags) .Select(info => new CustomMethodWrapper(info, _extraMethodAttributes)) .ToArray()); }
public IEnumerable <IMethodInfo> GetMethods(bool includePrivateMethods) { return(_typeInfoImplementation.GetMethods(includePrivateMethods)); }
public IEnumerable <IMethodInfo> GetMethods(bool includePrivateMethods) { return(inner.GetMethods(includePrivateMethods)); }