private bool IsMethodStartingWithString([CanBeNull] IDeclaredElement declaredElement,
                                                params string[] methodName)
        {
            var methodString = declaredElement.ConvertToString();

            return(methodString != null && methodName.Any(m => methodString.StartsWith(m)));
        }
예제 #2
0
        public static bool IsMoqSetupMethod([CanBeNull] IDeclaredElement declaredElement)
        {
            var declaredElementAsString = declaredElement.ConvertToString();

            if (declaredElementAsString == "Method:Moq.Mock`1.Setup(System.Linq.Expressions.Expression`1[TDelegate -> System.Action`1[T -> T]] expression)")
            {
                return(true);
            }
            if (declaredElementAsString == "Method:Moq.Mock`1.Setup(System.Linq.Expressions.Expression`1[TDelegate -> System.Func`2[T -> T, TResult -> TResult]] expression)")
            {
                return(true);
            }
            return(false);
        }
        private bool IsMethodString([CanBeNull] IDeclaredElement declaredElement, params string[] methodName)
        {
            var declaredElementAsString = declaredElement.ConvertToString();

            return(methodName.Contains(declaredElementAsString));
        }
        protected override bool AddLookupItems(CSharpCodeCompletionContext context, GroupedItemsCollector collector)
        {
            bool         moqIsSeen = false;
            var          candidateExistingElements = new List <ISymbolInfo>();
            ISymbolTable table = GetSymbolTable(context);

            if (table != null)
            {
                table.ForAllSymbolInfos(info =>
                {
                    IDeclaredElement declaredElement = info.GetDeclaredElement();
                    if (declaredElement.ConvertToString() == "Class:Moq.Mock")
                    {
                        moqIsSeen = true;
                    }
                    IType type = declaredElement.Type();
                    if (type != null)
                    {
                        if (type.GetClassType().ConvertToString() == "Class:Moq.Mock`1")
                        {
                            IType typeParameter = TypesUtil.GetTypeArgumentValue(type, 0);
                            if (typeParameter != null && context.ExpectedTypesContext != null && context.ExpectedTypesContext.ExpectedITypes != null && context.ExpectedTypesContext.ExpectedITypes.Select(x => x.Type).Where(x => x != null).Any(x => typeParameter.IsExplicitlyConvertibleTo(x, ClrPredefinedTypeConversionRule.INSTANCE)))
                            {
                                candidateExistingElements.Add(info);
                            }
                        }
                    }
                });
            }
            foreach (ISymbolInfo candidateExistingElement in candidateExistingElements)
            {
#if RESHARPER8
                collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(candidateExistingElement.ShortName + ".Object"));
#endif
#if RESHARPER9
                var lookupItem = new TextLookupItem(candidateExistingElement.ShortName + ".Object");
                lookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                lookupItem.PlaceTop();
                collector.Add(lookupItem);
#endif
            }
            if (moqIsSeen && !candidateExistingElements.Any() && context.ExpectedTypesContext != null)
            {
                foreach (ExpectedTypeCompletionContextBase.ExpectedIType expectedType in context.ExpectedTypesContext.ExpectedITypes)
                {
                    if (expectedType.Type == null)
                    {
                        continue;
                    }
                    if (expectedType.Type.IsInterfaceType())
                    {
                        string typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
#if RESHARPER8
                        if (candidateExistingElements.Any())
                        {
                            collector.AddToBottom(context.LookupItemsFactory.CreateTextLookupItem("new Mock<" + typeName + ">().Object"));
                        }
                        else
                        {
                            collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem("new Mock<" + typeName + ">().Object"));
                        }
#endif
#if RESHARPER9
                        var lookupItem = new TextLookupItem("new Mock<" + typeName + ">().Object");
                        lookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                        if (candidateExistingElements.Any())
                        {
                            lookupItem.PlaceBottom();
                        }
                        else
                        {
                            lookupItem.PlaceTop();
                        }
                        collector.Add(lookupItem);
#endif
                    }
                }
            }
            return(true);
        }
        protected override bool AddLookupItems(CSharpCodeCompletionContext context, GroupedItemsCollector collector)
        {
            bool         nSubstituteIsSeen = false;
            ISymbolTable table             = GetSymbolTable(context);

            if (table != null)
            {
                table.ForAllSymbolInfos(info =>
                {
                    IDeclaredElement declaredElement = info.GetDeclaredElement();
                    if (declaredElement.ConvertToString() == "Class:NSubstitute.Substitute")
                    {
                        nSubstituteIsSeen = true;
                    }
                });
            }
            if (!nSubstituteIsSeen)
            {
                return(true);
            }
            if (context.ExpectedTypesContext != null)
            {
                foreach (ExpectedTypeCompletionContextBase.ExpectedIType expectedType in context.ExpectedTypesContext.ExpectedITypes)
                {
                    if (expectedType.Type == null)
                    {
                        continue;
                    }
                    var typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
#if RESHARPER9
                    var textLookupItem = new TextLookupItem("Arg.Any<" + typeName + ">()");
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif
                }
            }
            if (context.TerminatedContext == null)
            {
                return(true);
            }
            var identifier           = context.TerminatedContext.TreeNode as IIdentifier;
            var mockedMethodArgument = identifier
                                       .GetParentSafe <IReferenceExpression>()
                                       .GetParentSafe <ICSharpArgument>();
            if (mockedMethodArgument == null)
            {
                return(true);
            }
            var mockedMethodInvocationExpression = mockedMethodArgument
                                                   .GetParentSafe <IArgumentList>()
                                                   .GetParentSafe <IInvocationExpression>();
            if (mockedMethodInvocationExpression == null)
            {
                return(true);
            }
            if (mockedMethodInvocationExpression.Reference != null)
            {
                var mockedMethodResolved = mockedMethodInvocationExpression.Reference.Resolve();
                var declaredElements     = Enumerable.Repeat(mockedMethodResolved.DeclaredElement, 1)
                                           .Concat(mockedMethodResolved.Result.Candidates)
                                           .Where(x => x != null);
                var methods = declaredElements
                              .OfType <IMethod>()
                              .Where(x => x.Parameters.Count() > 1)
                              .ToList();
                methods.ForEach(method =>
                {
                    var parameter = method.Parameters.Select(x => "Arg.Any<" + x.Type.GetPresentableName(CSharpLanguage.Instance) + ">()");
#if RESHARPER8
                    collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(string.Join(", ", parameter)));
#endif
#if RESHARPER9
                    var textLookupItem = new TextLookupItem(string.Join(", ", parameter));
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif
                });
            }
            return(true);
        }