public MethodAnalyzerTests()
        {
            var t = typeof(SomeTypeWithManyMethods);

            _doVoidInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.DoVoid)) !);

            _acceptIntVoidInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.AcceptIntVoid)) !);

            _echoStringInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.EchoString)) !);

            _doTaskInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.DoTask)) !);

            _doValueTaskInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.DoValueTask)) !);

            _doAndReturnValueTaskInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.DoAndReturnValueTask)) !);

            _returnTaskInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.ReturnTask)) !);

            _multiInspection = new MethodInspection(
                t, t.GetMethod(nameof(SomeTypeWithManyMethods.Multi)) !);
        }
Ejemplo n.º 2
0
        public void Return_type_is_correct()
        {
            var inspection = new MethodInspection(_inspectedType, _inspectedMethod);

            Assert.NotNull(inspection.ReturnType);
            Assert.Equal(typeof(int), inspection.ReturnType);
        }
Ejemplo n.º 3
0
        public void Constructor_parameters_are_used()
        {
            var inspection = new MethodInspection(_inspectedType, _inspectedMethod);

            Assert.NotNull(inspection.InspectedType);
            Assert.Same(_inspectedType, inspection.InspectedType);
            Assert.NotNull(inspection.InspectedMethod);
            Assert.Same(_inspectedMethod, inspection.InspectedMethod);
        }
Ejemplo n.º 4
0
        public void Can_be_deconstructed()
        {
            var inspection = new MethodInspection(_inspectedType, _inspectedMethod);

            var(inspectedType, inspectedMethod, parameters, returnType) = inspection;

            Assert.Same(_inspectedType, inspectedType);
            Assert.Same(_inspectedMethod, inspectedMethod);
            Assert.Equal(3, parameters.Count);
            Assert.Equal(typeof(int), returnType);
        }
Ejemplo n.º 5
0
        public void Parameters_are_correct()
        {
            var inspection = new MethodInspection(_inspectedType, _inspectedMethod);

            Assert.NotNull(inspection.Parameters);
            Assert.NotEmpty(inspection.Parameters);
            Assert.Equal(3, inspection.Parameters.Count);
            Assert.Equal(typeof(Guid), inspection.Parameters[0].ParameterType);
            Assert.Equal(typeof(string), inspection.Parameters[1].ParameterType);
            Assert.Equal(typeof(IDisposable), inspection.Parameters[2].ParameterType);
        }
Ejemplo n.º 6
0
        public bool IsMatch(
            MethodInspection inspection,
            Type requestType,
            bool allowCancellationTokenParameter,
            [NotNullWhen(true)] out MethodInfo?match)
        {
            match = null;

            var(_, method, parameters, _) = inspection;

            if (parameters.Count == 0 && requestType == typeof(void))
            {
                match = method;
                return(true);
            }

            if (parameters.Count > 2)
            {
                return(false);
            }

            if (parameters.Count == 2)
            {
                if (!allowCancellationTokenParameter)
                {
                    return(false);
                }

                if (!AcceptsCancellationToken(inspection))
                {
                    return(false);
                }
            }

            var theParameter = parameters[0];

            if (IsSameOrCompatible(baseType: theParameter.ParameterType, requestType))
            {
                match = method;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 7
0
        public bool IsMatch(
            MethodInspection inspection,
            Type requestType,
            Type responseType,
            bool allowCancellationTokenParameter,
            [NotNullWhen(true)] out MethodInfo?match)
        {
            match = null;

            if (!IsMatch(inspection, requestType, allowCancellationTokenParameter, out _))
            {
                return(false);
            }

            // Method has the correct input parameter.
            // Check the return type!

            var(_, method, _, returnType) = inspection;

            if (IsSameOrCompatible(baseType: responseType, returnType))
            {
                match = method;
                return(true);
            }

            // The return type may be an awaitable!
            // (Task<TResponse>, ValueTask<TResponse>, ...)

            if (!returnType.IsAwaitableWithResult(out var awaitResultType))
            {
                return(false);
            }

            if (IsSameOrCompatible(baseType: responseType, awaitResultType))
            {
                // It's a match: Task/ValueTask/Awaitable of TResponse.
                match = method;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 8
0
 public bool AcceptsCancellationToken(MethodInspection inspection) =>
 AcceptCancellationToken(inspection.Parameters);