Ejemplo n.º 1
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var block           = _dataProvider.GetSelectedElement <IBlock>();
            var selectedElement = _dataProvider.GetSelectedElement <IObjectCreationExpression>(false, false);

            if (!(selectedElement.TypeReference.Resolve()?.DeclaredElement is IClass c))
            {
                return(null);
            }

            var constructor = c.Constructors.OrderByDescending(x => x.Parameters.Count).FirstOrDefault(x => !x.IsParameterless);

            if (constructor == null)
            {
                return(null);
            }

            var parameters = _csharpMemberProvider.GetConstructorParameters(constructor.ToString()).ToArray();

            for (int i = 0; i < constructor.Parameters.Count; i++)
            {
                var shortName = constructor.Parameters[i].ShortName;
                var localVariableStatement = _dataProvider.ElementFactory.CreateStatement("var $0 = new Mock<$1>();", (object)shortName, (object)parameters[i]);

                block.AddStatementBefore(localVariableStatement, selectedElement.GetContainingStatement());
                var argument = _dataProvider.ElementFactory.CreateArgument(ParameterKind.VALUE, _dataProvider.ElementFactory.CreateExpression($"{shortName}.Object"));
                selectedElement.AddArgumentBefore(argument, null);
            }
            return(null);
        }
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var block            = _dataProvider.GetSelectedElement <IBlock>();
            var classBody        = _dataProvider.GetSelectedElement <IClassBody>();
            var classDeclaration = classBody?.GetContainingTypeDeclaration() as IClassLikeDeclaration;

            if (classDeclaration == null || block == null)
            {
                return(null);
            }

            var parameters       = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray();
            var naming           = _dataProvider.PsiServices.Naming;
            var mockFieldsByType = _csharpMemberProvider.GetClassFields(classBody, _selectedElement.Language);

            for (int i = 0; i < _constructor.Parameters.Count; i++)
            {
                var shortName  = _constructor.Parameters[i].ShortName;
                var typeString = _csharpMemberProvider.GetGenericMock(parameters[i]);

                if (!mockFieldsByType.TryGetValue(typeString, out var name))
                {
                    var mockType = TypeFactory.CreateTypeByCLRName(typeString, _dataProvider.PsiModule);
                    var field    = _dataProvider.ElementFactory.CreateTypeMemberDeclaration("private $0 $1;", (object)mockType, (object)shortName);
                    var options  = new SuggestionOptions(defaultName: shortName);
                    name = naming.Suggestion.GetDerivedName(field.DeclaredElement, NamedElementKinds.PrivateInstanceFields, ScopeKind.Common, _selectedElement.Language,
                                                            options, _dataProvider.SourceFile);
                    field.SetName(name);
                    classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)field);

                    var statement = _dataProvider.ElementFactory.CreateStatement("$0 = new Mock<$1>();", (object)name, (object)parameters[i]);
                    block.AddStatementBefore(statement, _selectedElement.GetContainingStatement());
                }

                var argument = _dataProvider.ElementFactory.CreateArgument(ParameterKind.VALUE, _dataProvider.ElementFactory.CreateExpression($"{name}.Object"));
                _selectedElement.AddArgumentBefore(argument, null);
            }

            return(null);
        }
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var argumentList     = _selectedElement.ArgumentList;
            var parameters       = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray();
            var mockFieldsByType = _csharpMemberProvider.GetClassFields(_classBody, _selectedElement.Language);
            var shortName        = _constructor.Parameters[_parameterNumber].ShortName;
            var currentParam     = parameters[_parameterNumber];
            var typeString       = _csharpMemberProvider.GetGenericMock(currentParam);

            if (!mockFieldsByType.TryGetValue(typeString, out var name))
            {
                var mockType = TypeFactory.CreateTypeByCLRName(typeString, _dataProvider.PsiModule);
                var field    = _dataProvider.ElementFactory.CreateTypeMemberDeclaration("private $0 $1;", (object)mockType, (object)shortName);
                var options  = new SuggestionOptions(defaultName: shortName);
                name = _dataProvider.PsiServices.Naming.Suggestion.GetDerivedName(field.DeclaredElement, NamedElementKinds.PrivateInstanceFields, ScopeKind.Common, _selectedElement.Language, options, _dataProvider.SourceFile);
                field.SetName(name);
                _classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)field);

                var statement = _dataProvider.ElementFactory.CreateStatement("$0 = new Mock<$1>();", (object)name, (object)currentParam);
                _block.AddStatementBefore(statement, _selectedElement.GetContainingStatement());
            }

            return(_csharpMemberProvider.FillCurrentParameterWithMock(name, argumentList, _selectedElement, _parameterNumber, _dataProvider));
        }