/// <inheritdoc/> public IEnumerable <_IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName) { var attributeType = ReflectionAttributeNameCache.GetType(assemblyQualifiedAttributeTypeName); Guard.ArgumentValidNotNull(nameof(assemblyQualifiedAttributeTypeName), $"Could not load type: '{assemblyQualifiedAttributeTypeName}'", attributeType); return (Assembly .CustomAttributes .Where(attr => attributeType.IsAssignableFrom(attr.AttributeType)) .OrderBy(attr => attr.AttributeType.Name) .Select(a => Reflector.Wrap(a)) .Cast <_IAttributeInfo>() .ToList()); }
/// <summary> /// Initializes a new instance of the <see cref="TestFrameworkExecutor{TTestCase}"/> class. /// </summary> /// <param name="assemblyName">Name of the test assembly.</param> /// <param name="sourceInformationProvider">The source line number information provider.</param> /// <param name="diagnosticMessageSink">The message sink to report diagnostic messages to.</param> protected TestFrameworkExecutor(AssemblyName assemblyName, ISourceInformationProvider sourceInformationProvider, IMessageSink diagnosticMessageSink) { DisposalTracker = new DisposalTracker(); SourceInformationProvider = sourceInformationProvider; DiagnosticMessageSink = diagnosticMessageSink; #if PLATFORM_DOTNET var assembly = Assembly.Load(assemblyName); #else var assembly = Assembly.Load(assemblyName.Name); #endif AssemblyInfo = Reflector.Wrap(assembly); }
/// <summary> /// Initializes a new instance of the <see cref="TestFrameworkExecutor{TTestCase}"/> class. /// </summary> /// <param name="assemblyName">Name of the test assembly.</param> /// <param name="sourceInformationProvider">The source line number information provider.</param> /// <param name="diagnosticMessageSink">The message sink to report diagnostic messages to.</param> protected TestFrameworkExecutor(AssemblyName assemblyName, ISourceInformationProvider sourceInformationProvider, IMessageSink diagnosticMessageSink) { DisposalTracker = new DisposalTracker(); SourceInformationProvider = sourceInformationProvider; DiagnosticMessageSink = diagnosticMessageSink; #if !WIN8_STORE || WINDOWS_PHONE_APP || WINDOWS_PHONE var assembly = Assembly.Load(assemblyName); #else var assembly = Assembly.Load(assemblyName.Name); #endif AssemblyInfo = Reflector.Wrap(assembly); }
/// <inheritdoc/> public IEnumerable <IParameterInfo> GetParameters() { if (_cachedParameters == null) { ParameterInfo[] parameters = MethodInfo.GetParameters(); IParameterInfo[] iParameters = new IParameterInfo[parameters.Length]; for (int i = 0; i < iParameters.Length; i++) { iParameters[i] = Reflector.Wrap(parameters[i]); } _cachedParameters = iParameters; } return(_cachedParameters); }
/// <inheritdoc/> protected XunitTestCase(SerializationInfo info, StreamingContext context) { string assemblyName = info.GetString("AssemblyName"); string typeName = info.GetString("TypeName"); string methodName = info.GetString("MethodName"); object[] arguments = (object[])info.GetValue("Arguments", typeof(object[])); var testCollection = (ITestCollection)info.GetValue("TestCollection", typeof(ITestCollection)); var type = Reflector.GetType(assemblyName, typeName); var typeInfo = Reflector.Wrap(type); var methodInfo = Reflector.Wrap(type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)); var factAttribute = methodInfo.GetCustomAttributes(typeof(FactAttribute)).Single(); Initialize(testCollection, Reflector.Wrap(type.Assembly), typeInfo, methodInfo, factAttribute, arguments); }
/// <summary> /// Resolves a generic type for a test method. The test parameters (and associated parameter infos) are /// used to determine the best matching generic type for the test method that can be satisfied by all /// the generic parameters and their values. /// </summary> /// <param name="genericType">The generic type to be resolved</param> /// <param name="parameters">The parameter values being passed to the test method</param> /// <param name="parameterInfos">The parameter infos for the test method</param> /// <returns>The best matching generic type</returns> public static ITypeInfo ResolveGenericType(this ITypeInfo genericType, object[] parameters, IParameterInfo[] parameterInfos) { for (var idx = 0; idx < parameterInfos.Length; ++idx) { var methodParameterType = parameterInfos[idx].ParameterType; var passedParameterType = parameters[idx]?.GetType(); Type matchedType = null; if (ResolveGenericParameter(genericType, methodParameterType, passedParameterType, ref matchedType)) { return(Reflector.Wrap(matchedType)); } } return(ObjectTypeInfo); }
/// <inheritdoc/> public IEnumerable <_IParameterInfo> GetParameters() { if (cachedParameters == null) { var parameters = MethodInfo.GetParameters(); var parameterInfos = new _IParameterInfo[parameters.Length]; for (var i = 0; i < parameterInfos.Length; i++) { parameterInfos[i] = Reflector.Wrap(parameters[i]); } cachedParameters = parameterInfos; } return(cachedParameters); }
/// <inheritdoc/> public _IMethodInfo?GetMethod( string methodName, bool includePrivateMethod) { Guard.ArgumentNotNull(methodName); var method = Type .GetRuntimeMethods() .FirstOrDefault(m => (includePrivateMethod || m.IsPublic && m.DeclaringType != typeof(object)) && m.Name == methodName); if (method == null) { return(null); } return(Reflector.Wrap(method)); }
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); }
/// <inheritdoc/> public IReadOnlyCollection <_IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName) { var attributeType = ReflectionAttributeNameCache.GetType(assemblyQualifiedAttributeTypeName); Guard.ArgumentNotNull($"Could not load type: '{assemblyQualifiedAttributeTypeName}'", attributeType, nameof(assemblyQualifiedAttributeTypeName)); return (additionalAssemblyAttributes .Where(customAttribute => attributeType.IsAssignableFrom(customAttribute.Attribute.GetType())) .Concat( Assembly .CustomAttributes .Where(attr => attributeType.IsAssignableFrom(attr.AttributeType)) .OrderBy(attr => attr.AttributeType.Name) .Select(a => Reflector.Wrap(a)) .Cast <_IAttributeInfo>() ) .CastOrToReadOnlyCollection()); }
public RunTests(Executor executor, string _type, List <string> _methods, object _handler) { Guard.ArgumentNotNull("_type", _type); Guard.ArgumentNotNull("_methods", _methods); ExecutorCallback handler = ExecutorCallback.Wrap(_handler); Type realType = executor.assembly.GetType(_type); Guard.ArgumentValid("_type", "Type " + _type + " could not be found", realType != null); ITypeInfo type = Reflector.Wrap(realType); ITestClassCommand testClassCommand = TestClassCommandFactory.Make(type); List <IMethodInfo> methods = new List <IMethodInfo>(); foreach (string _method in _methods) { try { IMethodInfo method = type.GetMethod(_method); Guard.ArgumentValid("_methods", "Could not find method " + _method + " in type " + _type, method != null); methods.Add(method); } catch (AmbiguousMatchException) { throw new ArgumentException("Ambiguous method named " + _method + " in type " + _type); } } if (testClassCommand == null) { ClassResult result = new ClassResult(type.Type); OnTestResult(result, handler); return; } executor.RunOnSTAThreadWithPreservedWorkingDirectory(() => TestClassCommandRunner.Execute(testClassCommand, methods, command => OnTestStart(command, handler), result => OnTestResult(result, handler))); }
/// <inheritdoc/> protected TestAssembly(SerializationInfo info, StreamingContext context) { ConfigFileName = info.GetString("ConfigFileName"); var assemblyPath = info.GetString("AssemblyPath"); #if !WINDOWS_PHONE_APP && !WIN8_STORE var assembly = AppDomain.CurrentDomain .GetAssemblies() .First(a => !a.IsDynamic && String.Equals(a.GetLocalCodeBase(), assemblyPath, StringComparison.OrdinalIgnoreCase)); #else // On WPA, this will be the assemblyname var assembly = System.Reflection.Assembly.Load(new AssemblyName { Name = Path.GetFileNameWithoutExtension(assemblyPath) }); #endif Assembly = Reflector.Wrap(assembly); }
/// <summary> /// Initializes a new instance of the <see cref="XunitTestFrameworkExecutor"/> class. /// </summary> /// <param name="assemblyFileName">Path of the test assembly.</param> public XunitTestFrameworkExecutor(string assemblyFileName) { this.assemblyFileName = assemblyFileName; var assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyFileName)); assemblyInfo = Reflector.Wrap(assembly); var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault(); if (collectionBehaviorAttribute != null) { disableParallelization = collectionBehaviorAttribute.GetNamedArgument <bool>("DisableTestParallelization"); } var testCollectionFactory = XunitTestFrameworkDiscoverer.GetTestCollectionFactory(assemblyInfo, collectionBehaviorAttribute); displayName = String.Format("{0}-bit .NET {1} [{2}, {3}]", IntPtr.Size * 8, Environment.Version, testCollectionFactory.DisplayName, disableParallelization ? "non-parallel" : "parallel"); }
/// <inheritdoc/> public IReadOnlyCollection <_ITypeInfo> GetTypes(bool includePrivateTypes) { try { return (Assembly .DefinedTypes .Where(t => includePrivateTypes || t.IsPublic || t.IsNestedPublic) .Select(t => Reflector.Wrap(t.AsType())) .Cast <_ITypeInfo>() .CastOrToReadOnlyCollection()); } catch (ReflectionTypeLoadException ex) { return (ex .Types .WhereNotNull() .Select(t => Reflector.Wrap(t)) .Cast <_ITypeInfo>() .CastOrToReadOnlyCollection()); } }
/// <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(nameof(member), 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).IsAssignableFrom(traitAttributeType)) { continue; } var discovererAttributeData = FindDiscovererAttributeType(traitAttributeType); 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); }
/// <inheritdoc/> public IEnumerable <_ITypeInfo> GetGenericArguments() => Type .GenericTypeArguments .Select(t => Reflector.Wrap(t)) .ToList();
/// <inheritdoc /> protected override async Task RunTestsOnMethodAsync(IMessageBus messageBus, Type classUnderTest, object[] constructorArguments, MethodInfo methodUnderTest, List <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) { var executionTime = 0M; try { var testMethod = Reflector.Wrap(methodUnderTest); var dataAttributes = testMethod.GetCustomAttributes(typeof(DataAttribute)); foreach (var dataAttribute in dataAttributes) { var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First(); var args = discovererAttribute.GetConstructorArguments().Cast <string>().ToList(); var discovererType = Reflector.GetType(args[1], args[0]); var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(discovererType); foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod)) { var methodToRun = methodUnderTest; ITypeInfo[] resolvedTypes = null; if (methodToRun.IsGenericMethodDefinition) { resolvedTypes = ResolveGenericTypes(testMethod, dataRow); methodToRun = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray()); } executionTime += await RunTestWithArgumentsAsync(messageBus, classUnderTest, constructorArguments, methodToRun, dataRow, GetDisplayNameWithArguments(DisplayName, dataRow, resolvedTypes), beforeAfterAttributes, aggregator, cancellationTokenSource); if (cancellationTokenSource.IsCancellationRequested) { return; } } } } catch (Exception ex) { if (!messageBus.QueueMessage(new TestStarting(this, DisplayName))) { cancellationTokenSource.Cancel(); } else { if (!messageBus.QueueMessage(new TestFailed(this, DisplayName, executionTime, null, ex.Unwrap()))) { cancellationTokenSource.Cancel(); } } if (!messageBus.QueueMessage(new TestFinished(this, DisplayName, executionTime, null))) { cancellationTokenSource.Cancel(); } } }
/// <inheritdoc/> public ITypeInfo GetType(string typeName) { var type = Assembly.GetType(typeName); return(type == null ? null : Reflector.Wrap(type)); }
/// <summary> /// Creates the test class command, which implements <see cref="ITestClassCommand"/>, for a given type. /// </summary> /// <param name="type">The type under test</param> /// <returns>The test class command, if the class is a test class; null, otherwise</returns> public static ITestClassCommand Make(Type type) { return(Make(Reflector.Wrap(type))); }
/// <inheritdoc/> public IReadOnlyCollection <_ITypeInfo> GetGenericArguments() => Type .GenericTypeArguments .Select(t => Reflector.Wrap(t)) .CastOrToReadOnlyCollection();
/// <inheritdoc/> public IEnumerable <_ITypeInfo> GetGenericArguments() => MethodInfo.GetGenericArguments().Select(t => Reflector.Wrap(t)).ToArray();
/// <summary> /// Initializes a new instance of the <see cref="ReflectionParameterInfo"/> class. /// </summary> /// <param name="parameterInfo">The parameter to be wrapped.</param> public ReflectionParameterInfo(ParameterInfo parameterInfo) { ParameterInfo = Guard.ArgumentNotNull(parameterInfo); parameterType = new(() => Reflector.Wrap(ParameterInfo.ParameterType)); }
/// <inheritdoc/> public IMethodInfo MakeGenericMethod(params ITypeInfo[] typeArguments) { var unwrapedTypeArguments = typeArguments.Select(t => ((IReflectionTypeInfo)t).Type).ToArray(); return(Reflector.Wrap(MethodInfo.MakeGenericMethod(unwrapedTypeArguments))); }
/// <inheritdoc/> public IEnumerable <ITypeInfo> GetGenericArguments() { return(MethodInfo.GetGenericArguments().Select(t => Reflector.Wrap(t)).ToArray()); }
/// <inheritdoc/> public IEnumerable <ITypeInfo> GetGenericArguments() { return(Type.GetTypeInfo().GenericTypeArguments .Select(t => Reflector.Wrap(t)) .ToList()); }
/// <inheritdoc/> public IReadOnlyCollection <_ITypeInfo> GetGenericArguments() => MethodInfo.GetGenericArguments().Select(t => Reflector.Wrap(t)).ToArray();
/// <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); }
/// <inheritdoc/> protected override ICollection <ITestMethod> GetConcreteMethods(ITestMethod testMethod) { var methodInfo = testMethod.Method.ToRuntimeMethod( ); var concreteMethods = GenericTypeResolver.GetConcreteMethods(methodInfo); return(concreteMethods .Select(concreteMethod => (ITestMethod) new TestMethod(testMethod.TestClass, Reflector.Wrap(concreteMethod))) .ToList( )); }
/// <summary> /// Creates a new instance of the <see cref="TestClassCommand"/> class. /// </summary> /// <param name="typeUnderTest">The type under test</param> public TestClassCommand(Type typeUnderTest) : this(Reflector.Wrap(typeUnderTest)) { }