Beispiel #1
0
        /// <inheritdoc/>
        public virtual ValueTask <IReadOnlyCollection <ITheoryDataRow>?> GetData(
            _IAttributeInfo dataAttribute,
            _IMethodInfo testMethod)
        {
            Guard.ArgumentNotNull(dataAttribute);
            Guard.ArgumentNotNull(testMethod);

            if (dataAttribute is _IReflectionAttributeInfo reflectionDataAttribute &&
                testMethod is _IReflectionMethodInfo reflectionTestMethod)
            {
                var attribute = (DataAttribute)reflectionDataAttribute.Attribute;

                try
                {
                    return(attribute.GetData(reflectionTestMethod.MethodInfo));
                }
                catch (ArgumentException)
                {
                    // If we couldn't find the data on the base type, check if it is in current type.
                    // This allows base classes to specify data that exists on a sub type, but not on the base type.
                    var reflectionTestMethodType = (_IReflectionTypeInfo)reflectionTestMethod.Type;
                    if (attribute is MemberDataAttribute memberDataAttribute && memberDataAttribute.MemberType == null)
                    {
                        memberDataAttribute.MemberType = reflectionTestMethodType.Type;
                    }

                    return(attribute.GetData(reflectionTestMethod.MethodInfo));
                }
            }

            return(new(default(IReadOnlyCollection <ITheoryDataRow>)));
        }
Beispiel #2
0
        /// <inheritdoc/>
        public virtual IEnumerable <object?[]>?GetData(
            _IAttributeInfo dataAttribute,
            _IMethodInfo testMethod)
        {
            Guard.ArgumentNotNull(nameof(dataAttribute), dataAttribute);
            Guard.ArgumentNotNull(nameof(testMethod), testMethod);

            if (dataAttribute is _IReflectionAttributeInfo reflectionDataAttribute &&
                testMethod is _IReflectionMethodInfo reflectionTestMethod)
            {
                var attribute = (DataAttribute)reflectionDataAttribute.Attribute;

                try
                {
                    return(attribute.GetData(reflectionTestMethod.MethodInfo));
                }
                catch (ArgumentException)
                {
                    // If we couldn't find the data on the base type, check if it is in current type.
                    // This allows base classes to specify data that exists on a sub type, but not on the base type.
                    var reflectionTestMethodType = (_IReflectionTypeInfo)reflectionTestMethod.Type;
                    if (attribute is MemberDataAttribute memberDataAttribute && memberDataAttribute.MemberType == null)
                    {
                        memberDataAttribute.MemberType = reflectionTestMethodType.Type;
                    }

                    return(attribute.GetData(reflectionTestMethod.MethodInfo));
                }
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Xunit2MethodInfo"/> class.
        /// </summary>
        /// <param name="v3MethodInfo">The v3 method info to wrap.</param>
        public Xunit2MethodInfo(_IMethodInfo v3MethodInfo)
        {
            V3MethodInfo = Guard.ArgumentNotNull(nameof(v3MethodInfo), v3MethodInfo);

            ReturnType = new Xunit2TypeInfo(V3MethodInfo.ReturnType);
            Type       = new Xunit2TypeInfo(V3MethodInfo.Type);
        }
Beispiel #4
0
    /// <summary>
    /// Gets all the custom attributes for the method that are of the given type.
    /// </summary>
    /// <param name="methodInfo">The method</param>
    /// <param name="attributeType">The type of the attribute</param>
    /// <returns>The matching attributes that decorate the method</returns>
    public static IEnumerable <_IAttributeInfo> GetCustomAttributes(this _IMethodInfo methodInfo, Type attributeType)
    {
        Guard.ArgumentNotNull(nameof(methodInfo), methodInfo);
        Guard.ArgumentNotNull(nameof(attributeType), attributeType);
        Guard.NotNull("Attribute type cannot be a generic type parameter", attributeType.AssemblyQualifiedName);

        return(methodInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName));
    }
Beispiel #5
0
    /// <summary>
    /// Gets all the custom attributes for the method that are of the given type.
    /// </summary>
    /// <param name="methodInfo">The method</param>
    /// <param name="attributeType">The type of the attribute</param>
    /// <returns>The matching attributes that decorate the method</returns>
    public static IReadOnlyCollection <_IAttributeInfo> GetCustomAttributes(this _IMethodInfo methodInfo, Type attributeType)
    {
        Guard.ArgumentNotNull(methodInfo);
        Guard.ArgumentNotNull(attributeType);
        Guard.NotNull("Attribute type cannot be a generic type parameter", attributeType.AssemblyQualifiedName);

        return(methodInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName));
    }
Beispiel #6
0
    static MethodInfo?GetMethodInfoFromIMethodInfo(this Type type, _IMethodInfo methodInfo)
    {
        var methods = methodInfo.IsStatic ? type.GetRuntimeMethods() : type.GetMethods();

        return
            (methods
             .Where(method => method.IsPublic == methodInfo.IsPublic && method.IsStatic == methodInfo.IsStatic && method.Name == methodInfo.Name)
             .FirstOrDefault());
    }
        /// <inheritdoc/>
        public override bool SupportsDiscoveryEnumeration(
            _IAttributeInfo dataAttribute,
            _IMethodInfo testMethod)
        {
            Guard.ArgumentNotNull(dataAttribute);
            Guard.ArgumentNotNull(testMethod);

            return(!dataAttribute.GetNamedArgument <bool>("DisableDiscoveryEnumeration"));
        }
Beispiel #8
0
        /// <inheritdoc/>
        public virtual bool SupportsDiscoveryEnumeration(
            _IAttributeInfo dataAttribute,
            _IMethodInfo testMethod)
        {
            Guard.ArgumentNotNull(dataAttribute);
            Guard.ArgumentNotNull(testMethod);

            return(true);
        }
Beispiel #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestMethod"/> class.
        /// </summary>
        /// <param name="testClass">The test class</param>
        /// <param name="method">The test method</param>
        /// <param name="uniqueID">The unique ID for the test method (only used to override default behavior in testing scenarios)</param>
        public TestMethod(
            _ITestClass testClass,
            _IMethodInfo method,
            string?uniqueID = null)
        {
            this.testClass = Guard.ArgumentNotNull(nameof(testClass), testClass);
            this.method    = Guard.ArgumentNotNull(nameof(method), method);

            this.uniqueID = uniqueID ?? UniqueIDGenerator.ForTestMethod(testClass.UniqueID, this.method.Name);
        }
Beispiel #10
0
        /// <summary>
        /// Used for de-serialization.
        /// </summary>
        protected TestMethod(
            SerializationInfo info,
            StreamingContext context)
        {
            testClass = Guard.NotNull("Could not retrieve TestClass from serialization", info.GetValue <_ITestClass>("TestClass"));
            uniqueID  = Guard.NotNull("Could not retrieve UniqueID from serialization", info.GetValue <string>("UniqueID"));

            var methodName = Guard.NotNull("Could not retrieve MethodName from serialization", info.GetValue <string>("MethodName"));

            method = Guard.NotNull($"Could not find test method {methodName} on test class {testClass.Class.Name}", TestClass.Class.GetMethod(methodName, true));
        }
Beispiel #11
0
    /// <summary>
    /// Converts an <see cref="_IMethodInfo"/> into a <see cref="MethodInfo"/>, if possible (for example, this
    /// will not work when the test method is based on source code rather than binaries).
    /// </summary>
    /// <param name="methodInfo">The method to convert</param>
    /// <returns>The runtime method, if available; <c>null</c>, otherwise</returns>
    public static MethodInfo?ToRuntimeMethod(this _IMethodInfo methodInfo)
    {
        Guard.ArgumentNotNull(nameof(methodInfo), methodInfo);

        if (methodInfo is _IReflectionMethodInfo reflectionMethodInfo)
        {
            return(reflectionMethodInfo.MethodInfo);
        }

        return(methodInfo.Type.ToRuntimeType()?.GetMethodInfoFromIMethodInfo(methodInfo));
    }
    public static void ResolveGenericType(string methodName, object?[] parameters, Type[] expected)
    {
        var methodInfo = typeof(ResolveGenericMethodTests).GetMethod(methodName);

        Assert.NotNull(methodInfo);

        _IMethodInfo method = Reflector.Wrap(methodInfo);

        Type[] actual = method.ResolveGenericTypes(parameters).Select(t => ((ReflectionTypeInfo)t).Type).ToArray();
        Assert.Equal(expected, actual);
    }
Beispiel #13
0
        static Dictionary <string, List <string> > GetTraits(_IMethodInfo method)
        {
            var result = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);

            foreach (var traitAttribute in method.GetCustomAttributes(typeof(TraitAttribute)))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                result.Add((string)ctorArgs[0] !, (string)ctorArgs[1] !);
            }

            return(result);
        }
Beispiel #14
0
        public static _ITestMethod TestMethod(
            _IMethodInfo methodInfo,
            _ITestClass testClass,
            string?uniqueID = null)
        {
            uniqueID ??= "method-id";

            var result = Substitute.For <_ITestMethod, InterfaceProxy <_ITestMethod> >();

            result.Method.Returns(methodInfo);
            result.TestClass.Returns(testClass);
            result.UniqueID.Returns(uniqueID);
            return(result);
        }
Beispiel #15
0
        /// <inheritdoc/>
        public virtual IEnumerable <object?[]> GetData(
            _IAttributeInfo dataAttribute,
            _IMethodInfo testMethod)
        {
            // The data from GetConstructorArguments does not maintain its original form (in particular, collections
            // end up as generic IEnumerable<T>). So we end up needing to call .ToArray() on the enumerable so that
            // we can restore the correct argument type from InlineDataAttribute.
            //
            // In addition, [InlineData(null)] gets translated into passing a null array, not a single array with a null
            // value in it, which is why the null coalesce operator is required (this is covered by the acceptance test
            // in Xunit2TheoryAcceptanceTests.InlineDataTests.SingleNullValuesWork).

            var args = (IEnumerable <object?>?)dataAttribute.GetConstructorArguments().Single() ?? new object?[] { null };

            return(new[] { args.ToArray() });
        }
Beispiel #16
0
        static (string displayName, Exception?initException, _IMethodInfo method, _ITypeInfo[]? methodGenericTypes) Initialize(
            string baseDisplayName,
            _ITestMethod testMethod,
            object?[]?testMethodArguments)
        {
            string?   displayName   = null;
            Exception?initException = null;

            _ITypeInfo[]? methodGenericTypes = null;

            var method = testMethod.Method;

            if (testMethodArguments != null)
            {
                if (method is _IReflectionMethodInfo reflectionMethod)
                {
                    try
                    {
                        testMethodArguments = reflectionMethod.MethodInfo.ResolveMethodArguments(testMethodArguments);
                    }
                    catch (Exception ex)
                    {
                        initException       = ex;
                        testMethodArguments = null;
                        displayName         = $"{baseDisplayName}(???)";
                    }
                }
            }

            if (testMethodArguments != null && method.IsGenericMethodDefinition)
            {
                methodGenericTypes = method.ResolveGenericTypes(testMethodArguments);
                method             = method.MakeGenericMethod(methodGenericTypes);
            }

            if (displayName == null)
            {
                displayName = method.GetDisplayNameWithArguments(baseDisplayName, testMethodArguments, methodGenericTypes);
            }

            return(displayName, initException, method, methodGenericTypes);
        }
Beispiel #17
0
 /// <inheritdoc/>
 public virtual bool SupportsDiscoveryEnumeration(
     _IAttributeInfo dataAttribute,
     _IMethodInfo testMethod) => true;