private static IEnumerable <ResolvedArgument> ResolveArguments(
            IEnumerable <Type> parameterTypes,
            IDummyValueResolver resolver,
            LoopDetectingResolutionContext resolutionContext)
        {
            var resolvedArguments = new List <ResolvedArgument>();

            foreach (var parameterType in parameterTypes)
            {
                var resolvedArgument = new ResolvedArgument(parameterType);
                try
                {
                    var creationResult = resolver.TryResolveDummyValue(parameterType, resolutionContext);
                    resolvedArgument.WasResolved = creationResult.WasSuccessful;
                    if (creationResult.WasSuccessful)
                    {
                        resolvedArgument.ResolvedValue = creationResult.Result;
                    }
                }
                catch
                {
                    resolvedArgument.WasResolved = false;
                }

                resolvedArguments.Add(resolvedArgument);
            }

            return(resolvedArguments);
        }
        private static IEnumerable<ResolvedArgument> ResolveArguments(
            IEnumerable<Type> parameterTypes,
            DummyCreationSession session,
            IDummyValueResolver resolver)
        {
            var resolvedArguments = new List<ResolvedArgument>();
            foreach (var parameterType in parameterTypes)
            {
                var resolvedArgument = new ResolvedArgument { ArgumentType = parameterType };
                try
                {
                    var creationResult = resolver.TryResolveDummyValue(session, parameterType);
                    resolvedArgument.WasResolved = creationResult.WasSuccessful;
                    if (creationResult.WasSuccessful)
                    {
                        resolvedArgument.ResolvedValue = creationResult.Result;
                    }
                }
                catch
                {
                    resolvedArgument.WasResolved = false;
                }

                resolvedArguments.Add(resolvedArgument);
            }

            return resolvedArguments;
        }
        private static void StubResolverToFailForType <T>(IDummyValueResolver resolver)
        {
            object outResult;

            A.CallTo(() => resolver.TryResolveDummyValue(A <DummyCreationSession> ._, typeof(T), out outResult))
            .Returns(false);
        }
示例#4
0
        private static ResolvedConstructor ResolveConstructorArguments(ConstructorInfo constructor, DummyCreationSession session, IDummyValueResolver resolver)
        {
            var resolvedArguments = new List <ResolvedArgument>();

            foreach (var argument in constructor.GetParameters())
            {
                bool   wasResolved;
                object result = null;

                try
                {
                    wasResolved = resolver.TryResolveDummyValue(session, argument.ParameterType, out result);
                }
                catch
                {
                    wasResolved = false;
                }

                var resolvedArgument = new ResolvedArgument
                {
                    WasResolved   = wasResolved,
                    ResolvedValue = result,
                    ArgumentType  = argument.ParameterType
                };

                resolvedArguments.Add(resolvedArgument);
            }

            return(new ResolvedConstructor
            {
                Arguments = resolvedArguments.ToArray()
            });
        }
示例#5
0
 private static Lazy <T> CreateLazyDummy <T>(IDummyValueResolver resolver)
 {
     return(new Lazy <T>(() =>
     {
         var creationResult = resolver.TryResolveDummyValue(typeof(T), new LoopDetectingResolutionContext());
         return creationResult.WasSuccessful
             ? (T)creationResult.Result !
             : default !;
        private static void StubResolverWithDummyValue <T>(IDummyValueResolver resolver, T dummyValue)
        {
            object outResult;

            A.CallTo(() => resolver.TryResolveDummyValue(A <DummyCreationSession> ._, typeof(T), out outResult))
            .Returns(true)
            .AssignsOutAndRefParameters(dummyValue);
        }
示例#7
0
            public override CreationResult TryCreateDummyValue(
                Type typeOfDummy,
                IDummyValueResolver resolver,
                LoopDetectingResolutionContext resolutionContext)
            {
                if (typeOfDummy == typeof(Task))
                {
                    return(CreationResult.SuccessfullyCreated(TaskHelper.CompletedTask));
                }

                if (typeOfDummy.IsGenericType && typeOfDummy.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var    typeOfTaskResult = typeOfDummy.GetGenericArguments()[0];
                    var    creationResult   = resolver.TryResolveDummyValue(typeOfTaskResult, resolutionContext);
                    object?taskResult       = creationResult.WasSuccessful
                        ? creationResult.Result
                        : typeOfTaskResult.GetDefaultValue();

                    var method = GenericFromResultMethodDefinition.MakeGenericMethod(typeOfTaskResult);
                    return(CreationResult.SuccessfullyCreated(method.Invoke(null, new[] { taskResult })));
                }

                if (typeOfDummy.FullName == "System.Threading.Tasks.ValueTask")
                {
                    return(CreationResult.SuccessfullyCreated(typeOfDummy.GetDefaultValue()));
                }

                if (typeOfDummy.IsGenericType &&
                    !typeOfDummy.IsGenericTypeDefinition &&
                    typeOfDummy.FullName is string fullName &&
                    fullName.StartsWith("System.Threading.Tasks.ValueTask`", StringComparison.Ordinal))
                {
                    var    typeOfTaskResult = typeOfDummy.GetGenericArguments()[0];
                    var    creationResult   = resolver.TryResolveDummyValue(typeOfTaskResult, resolutionContext);
                    object?taskResult       = creationResult.WasSuccessful
                        ? creationResult.Result
                        : typeOfTaskResult.GetDefaultValue();

                    var ctor = typeOfDummy.GetConstructor(new[] { typeOfTaskResult }) !;
                    return(CreationResult.SuccessfullyCreated(ctor.Invoke(new[] { taskResult })));
                }

                return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is not a Task."));
            }
示例#8
0
            public override CreationResult TryCreateDummyValue(Type typeOfDummy, IDummyValueResolver resolver)
            {
                if (typeOfDummy.GetTypeInfo().IsGenericType&& typeOfDummy.GetGenericTypeDefinition() == typeof(Lazy <>))
                {
                    var    typeOfLazyResult = typeOfDummy.GetGenericArguments()[0];
                    var    creationResult   = resolver.TryResolveDummyValue(typeOfLazyResult);
                    object lazyResult       = creationResult.WasSuccessful
                        ? creationResult.Result
                        : typeOfLazyResult.GetDefaultValue();

                    var funcType = typeof(Func <>).MakeGenericType(typeOfLazyResult);

                    var method = CreateGenericFromResultMethodDefinition().MakeGenericMethod(typeOfLazyResult);
                    var func   = method.Invoke(null, new[] { lazyResult });
                    var dummy  = typeOfDummy.GetConstructor(new[] { funcType, typeof(bool) }).Invoke(new[] { func, true });
                    return(CreationResult.SuccessfullyCreated(dummy));
                }

                return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is not a Lazy."));
            }
            public override CreationResult TryCreateDummyValue(Type typeOfDummy, IDummyValueResolver resolver, LoopDetectingResolutionContext resolutionContext)
            {
                if (IsTuple(typeOfDummy))
                {
                    var argTypes = typeOfDummy.GetTypeInfo().GetGenericArguments();
                    var args     = new object[argTypes.Length];
                    for (int i = 0; i < argTypes.Length; i++)
                    {
                        var argType        = argTypes[i];
                        var creationResult = resolver.TryResolveDummyValue(argType, resolutionContext);
                        args[i] = creationResult.WasSuccessful
                            ? creationResult.Result
                            : argType.GetDefaultValue();
                    }

                    var dummy = Activator.CreateInstance(typeOfDummy, args);
                    return(CreationResult.SuccessfullyCreated(dummy));
                }

                return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is not a tuple."));
            }
示例#10
0
            public override CreationResult TryCreateDummyValue(Type typeOfDummy, IDummyValueResolver resolver)
            {
                if (typeOfDummy == typeof(Task))
                {
                    return(CreationResult.SuccessfullyCreated(TaskHelper.FromResult(default(object))));
                }

                if (typeOfDummy.GetTypeInfo().IsGenericType&& typeOfDummy.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var    typeOfTaskResult = typeOfDummy.GetGenericArguments()[0];
                    var    creationResult   = resolver.TryResolveDummyValue(typeOfTaskResult);
                    object taskResult       = creationResult.WasSuccessful
                        ? creationResult.Result
                        : typeOfTaskResult.GetDefaultValue();

                    var method = GenericFromResultMethodDefinition.MakeGenericMethod(typeOfTaskResult);
                    return(CreationResult.SuccessfullyCreated(method.Invoke(null, new[] { taskResult })));
                }

                return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is not a Task."));
            }
示例#11
0
        private static ResolvedConstructor ResolveConstructorArguments(Type[] parameterTypes, DummyCreationSession session, IDummyValueResolver resolver)
        {
            if (parameterTypes == null)
            {
                return(new ResolvedConstructor());
            }

            var resolvedArguments = new List <ResolvedArgument>();

            foreach (var parameterType in parameterTypes)
            {
                bool   wasResolved;
                object result = null;

                try
                {
                    wasResolved = resolver.TryResolveDummyValue(session, parameterType, out result);
                }
                catch
                {
                    wasResolved = false;
                }

                var resolvedArgument = new ResolvedArgument
                {
                    WasResolved   = wasResolved,
                    ResolvedValue = result,
                    ArgumentType  = parameterType
                };

                resolvedArguments.Add(resolvedArgument);
            }

            return(new ResolvedConstructor
            {
                Arguments = resolvedArguments.ToArray()
            });
        }
示例#12
0
 private static void StubResolverWithDummyValue <T>(IDummyValueResolver resolver, T dummyValue)
 {
     A.CallTo(() => resolver.TryResolveDummyValue(typeof(T)))
     .Returns(CreationResult.SuccessfullyCreated(dummyValue));
 }
 private static void StubResolverWithDummyValue <T>(IDummyValueResolver resolver, T dummyValue)
 {
     A.CallTo(() => resolver.TryResolveDummyValue(typeof(T), A <LoopDetectingResolutionContext> ._))
     .Returns(CreationResult.SuccessfullyCreated(dummyValue));
 }