public void ParameterNameSafe_PositiveCase()
        {
            const string reserved_word       = "this";
            ParameterDeclarationExpression p = new ParameterDeclarationExpression(new TypeReference("foo"), reserved_word);

            Assert.AreEqual("@this", p.ParameterNameSafe());
        }
        public static MethodDeclaration add_Method(this TypeDeclaration typeDeclaration, string methodName, Dictionary <string, object> invocationParameters, BlockStatement body)
        {
            var newMethod = new MethodDeclaration
            {
                Name = methodName,
                //Modifier = Modifiers.None | Modifiers.Public | Modifiers.Static,
                Modifier = Modifiers.None | Modifiers.Public,
                Body     = body
            };

            newMethod.setReturnType();
            if (invocationParameters != null)
            {
                foreach (var invocationParameter in invocationParameters)
                {
                    var parameterType = new TypeReference(
                        (invocationParameter.Value != null && invocationParameter.Key != "returnData")
                        ? invocationParameter.Value.typeFullName()
                        : "System.Object", true);
                    var parameter = new ParameterDeclarationExpression(parameterType, invocationParameter.Key);
                    newMethod.Parameters.Add(parameter);
                }
            }
            typeDeclaration.AddChild(newMethod);
            return(newMethod);
        }
        public void ParameterNameSafe_NegativeCase()
        {
            const string not_a_reserverd_word = "not_a_reserverd_word";
            ParameterDeclarationExpression p  = new ParameterDeclarationExpression(new TypeReference("foo"), not_a_reserverd_word);

            Assert.AreEqual("not_a_reserverd_word", p.ParameterNameSafe());
        }
Exemplo n.º 4
0
        public void IndexOf()
        {
            string program = TestUtil.GetInput();

            CompilationUnit      cu = TestUtil.ParseProgram(program);
            NamespaceDeclaration ns = (NamespaceDeclaration)cu.Children[0];
            TypeDeclaration      ty = (TypeDeclaration)ns.Children[0];
            IList methodDecList     = AstUtil.GetChildrenWithType(ty, typeof(MethodDeclaration));

            ParameterDeclarationExpression p1 = new ParameterDeclarationExpression(new TypeReference("Circle"), "circle");

            p1.TypeReference.RankSpecifier = new int[] {};
            List <ParameterDeclarationExpression> md1Param = new List <ParameterDeclarationExpression>();

            md1Param.Add(p1);
            MethodDeclaration md1 = new MethodDeclaration("GetRadius", Modifiers.Private, new TypeReference("int"), md1Param, null);

            int md1Index = IndexOf(methodDecList, md1);

            Assert.AreEqual(1, md1Index);

            MethodDeclaration md2 = new MethodDeclaration("ToString", Modifiers.Protected, new TypeReference("string"), null, null);
            int md2Index          = IndexOf(methodDecList, md2);

            Assert.AreEqual(-1, md2Index);
        }
        /// <summary>
        /// Called when a method is visited.
        /// </summary>
        /// <param name="methodDeclaration"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
        {
            INode nodeParent = methodDeclaration.Parent;

            if (nodeParent is TypeDeclaration)
            {
                TypeDeclaration typeDeclaration = (TypeDeclaration)methodDeclaration.Parent;

                EventType evt = new EventType();

                evt.id = methodDeclaration.Name;
                if (methodDeclaration.Parameters.Count > 0)
                {
                    evt.parameter = new ParameterType[methodDeclaration.Parameters.Count];
                    for (int i = 0; i < methodDeclaration.Parameters.Count; i++)
                    {
                        ParameterDeclarationExpression paramDecl = methodDeclaration.Parameters[i];
                        ParameterType param = new ParameterType();
                        param.name       = paramDecl.ParameterName;
                        param.type       = paramDecl.TypeReference.ToString();
                        evt.parameter[i] = param;
                    }
                }
                model.AddEvent(evt);
                model.AddEventToFeeder(feeder, evt.id);
            }

            return(base.VisitMethodDeclaration(methodDeclaration, data));
        }
Exemplo n.º 6
0
        public override List <Change> PerformChanges(RefactoringOptions options, object prop)
        {
            IResolver              resolver = options.GetResolver();
            List <Change>          result   = new List <Change> ();
            INRefactoryASTProvider provider = options.GetASTProvider();

            if (resolver == null || provider == null)
            {
                return(result);
            }

            TypeDeclaration newType = new TypeDeclaration(ICSharpCode.NRefactory.Ast.Modifiers.None, null);

            newType.Name = createExpression.CreateType.Type;
            newType.Type = GetNewTypeType();

            ConstructorDeclaration constructor = new ConstructorDeclaration(newType.Name, ICSharpCode.NRefactory.Ast.Modifiers.Public, null, null);

            constructor.Body = new BlockStatement();
            int i = 0;

            foreach (Expression expression in createExpression.Parameters)
            {
                i++;
                string output = provider.OutputNode(options.Dom, expression);
                string parameterName;
                if (Char.IsLetter(output[0]) || output[0] == '_')
                {
                    parameterName = output;
                }
                else
                {
                    parameterName = "par" + i;
                }

                ResolveResult resolveResult2 = resolver.Resolve(new ExpressionResult(output), options.ResolveResult.ResolvedExpression.Region.Start);
                TypeReference typeReference  = new TypeReference(resolveResult2.ResolvedType.ToInvariantString());
                typeReference.IsKeyword = true;
                ParameterDeclarationExpression pde = new ParameterDeclarationExpression(typeReference, parameterName);
                constructor.Parameters.Add(pde);
            }
            ICSharpCode.NRefactory.Ast.INode node = newType;
            IType curType = options.Document.CompilationUnit.GetTypeAt(options.Document.Editor.Caret.Line, options.Document.Editor.Caret.Column);

            if (curType != null && !string.IsNullOrEmpty(curType.Namespace))
            {
                NamespaceDeclaration namespaceDeclaration = new NamespaceDeclaration(curType.Namespace);
                namespaceDeclaration.Children.Add(newType);
                node = namespaceDeclaration;
            }
            newType.Children.Add(constructor);
            string fileName = GetName(Path.Combine(Path.GetDirectoryName(options.Document.FileName), newType.Name + Path.GetExtension(options.Document.FileName)));
            string header   = options.Dom.Project is DotNetProject?StandardHeaderService.GetHeader(options.Dom.Project, fileName, true) + Environment.NewLine : "";

            CreateFileChange createFile = new CreateFileChange(fileName, header + provider.OutputNode(options.Dom, node));

            result.Add(createFile);
            result.Add(new OpenFileChange(fileName));
            return(result);
        }
        public override object VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data)
        {
            if (!IsClassType(ClassType.Interface) && (propertyDeclaration.Modifier & Modifiers.Visibility) == 0)
            {
                propertyDeclaration.Modifier |= Modifiers.Public;
            }

            if (propertyDeclaration.HasSetRegion)
            {
                string from = "Value";
                if (propertyDeclaration.SetRegion.Parameters.Count > 0)
                {
                    ParameterDeclarationExpression p = propertyDeclaration.SetRegion.Parameters[0];
                    from            = p.ParameterName;
                    p.ParameterName = "Value";
                }
                propertyDeclaration.SetRegion.AcceptVisitor(new RenameIdentifierVisitor(from, "value", StringComparer.InvariantCultureIgnoreCase), null);
            }

            if (propertyDeclaration.HasGetRegion && propertyDeclaration.GetRegion.Block.Children.Count > 0)
            {
                BlockStatement block = propertyDeclaration.GetRegion.Block;
                ReplaceAllFunctionAssignments(block, propertyDeclaration.Name, propertyDeclaration.TypeReference);
            }

            return(base.VisitPropertyDeclaration(propertyDeclaration, data));
        }
            public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
            {
                if (methodDeclaration.Name.ToLower().Equals("main") && !methodDeclaration.Modifier.HasFlag(Modifiers.Constructors))
                {
                    if (!methodDeclaration.IsExtensionMethod && !methodDeclaration.Modifier.HasFlag(Modifiers.Abstract))
                    {
                        if (methodDeclaration.Parameters.Count == 1)
                        {
                            ParameterDeclarationExpression param = methodDeclaration.Parameters[0];

                            if (param.ParameterName.Equals("args") && param.TypeReference.ToString().Equals("System.String[]"))
                            {
                                //now check if body uses argsparam
                                //This is waaaaay to naive....
                                if (methodDeclaration.Body.ToString().Contains("args"))
                                {
                                    UnlockWith(methodDeclaration);
                                }
                            }
                        }
                    }
                }

                return(base.VisitMethodDeclaration(methodDeclaration, data));
            }
        public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
        {
            base.VisitMethodDeclaration(methodDeclaration, data);             // visit parameters
            methodDeclaration.Attributes.Clear();
            methodDeclaration.Body     = new BlockStatement();
            methodDeclaration.Modifier = Modifiers.Public;

            if (methodDeclaration.Parameters.Count > 0)
            {
                ParameterDeclarationExpression lastParameter = methodDeclaration.Parameters[methodDeclaration.Parameters.Count - 1];
                if (lastParameter.ParamModifier == ParameterModifiers.Out)
                {
                    methodDeclaration.TypeReference = lastParameter.TypeReference;
                    methodDeclaration.Parameters.RemoveAt(methodDeclaration.Parameters.Count - 1);

                    VariableDeclaration tmpVarDecl = new VariableDeclaration("tmp");
                    tmpVarDecl.TypeReference = methodDeclaration.TypeReference;
                    methodDeclaration.Body.AddChild(new LocalVariableDeclaration(tmpVarDecl));

                    IdentifierExpression tmpIdent = new IdentifierExpression("tmp");
                    InvocationExpression ie       = CreateMethodCall(methodDeclaration);
                    ie.Arguments.Add(new DirectionExpression(FieldDirection.Out, tmpIdent));
                    methodDeclaration.Body.AddChild(new ExpressionStatement(ie));

                    methodDeclaration.Body.AddChild(new ReturnStatement(tmpIdent));
                    return(null);
                }
            }

            methodDeclaration.Body.AddChild(new ExpressionStatement(CreateMethodCall(methodDeclaration)));
            return(null);
        }
Exemplo n.º 10
0
        public void Equals()
        {
            string          program = TestUtil.TypeMemberParse("public string Sentence(string title , string text);");
            CompilationUnit cu      = TestUtil.ParseProgram(program);

            NamespaceDeclaration           ns       = (NamespaceDeclaration)cu.Children[0];
            TypeDeclaration                ty       = (TypeDeclaration)ns.Children[0];
            MethodDeclaration              pgMethod = (MethodDeclaration)ty.Children[0];
            ParameterDeclarationExpression p1       = new ParameterDeclarationExpression(new TypeReference("string"), "title");

            p1.TypeReference.RankSpecifier = new int[] {};
            ParameterDeclarationExpression p2 = new ParameterDeclarationExpression(new TypeReference("string"), "text");

            p2.TypeReference.RankSpecifier = new int[] {};
            List <ParameterDeclarationExpression> argList = new List <ParameterDeclarationExpression>();

            argList.Add(p1);
            argList.Add(p2);
            MethodDeclaration exMethod = new MethodDeclaration("Sentence",
                                                               Modifiers.Public,
                                                               new TypeReference("string"),
                                                               argList, null);

            Assert.IsTrue(Equals(exMethod, pgMethod));

            string program2 = TestUtil.TypeMemberParse("public string Sentence(string title , string[] text);");

            cu = TestUtil.ParseProgram(program2);

            ns       = (NamespaceDeclaration)cu.Children[0];
            ty       = (TypeDeclaration)ns.Children[0];
            pgMethod = (MethodDeclaration)ty.Children[0];
            Assert.IsFalse(Equals(exMethod, pgMethod));
        }
		public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
		{
			if (nameComparer.Equals(from, parameterDeclarationExpression.ParameterName)) {
				parameterDeclarationExpression.ParameterName = to;
			}
			return base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data);
		}
 public override object TrackedVisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (keywords.Contains(parameterDeclarationExpression.ParameterName))
     {
         parameterDeclarationExpression.ParameterName = "_" + parameterDeclarationExpression.ParameterName;
     }
     return(base.TrackedVisitParameterDeclarationExpression(parameterDeclarationExpression, data));
 }
Exemplo n.º 13
0
        public static ParameterDeclarationExpression AddParameter(this MethodDeclaration methodDeclaration, string name, string parameterType)
        {
            var result = new ParameterDeclarationExpression(new TypeReference(parameterType), name);

            methodDeclaration.Parameters.Add(result);

            return(result);
        }
 public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (this.CheckNode(parameterDeclarationExpression))
     {
         return(null);
     }
     return(base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data));
 }
Exemplo n.º 15
0
 public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (nameComparer.Equals(from, parameterDeclarationExpression.ParameterName))
     {
         parameterDeclarationExpression.ParameterName = to;
     }
     return(base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data));
 }
Exemplo n.º 16
0
 public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (parameterDeclarationExpressionVisit != null)
     {
         data = parameterDeclarationExpressionVisit(parameterDeclarationExpression, data);
     }
     return(base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data));
 }
Exemplo n.º 17
0
        /// <summary>
        /// Adds a <see cref="ParameterDeclarationExpression"/> to <see cref="ParametrizedNode.Parameters"/> in a single
        /// call, for convenience.
        /// </summary>
        ///
        /// <param name="node">
        /// The method or constructor to add the parameter to.
        /// </param>
        ///
        /// <param name="parameterType">
        /// The <see cref="TypeReference"/> of the parameter to add.
        /// </param>
        ///
        /// <param name="parameterName">
        /// The name of the parameter to add.
        /// </param>
        ///
        /// <returns>
        /// The <see cref="ParameterDeclarationExpression"/> instance that was created and added to
        /// <paramref name="node"/>.
        /// </returns>
        public static ParameterDeclarationExpression AddParameter(this ParametrizedNode node,
                                                                  TypeReference parameterType, string parameterName)
        {
            var parameter = new ParameterDeclarationExpression(parameterType, parameterName);

            node.Parameters.Add(parameter);
            return(parameter);
        }
Exemplo n.º 18
0
 public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (parameterDeclarationExpression.ParamModifier == ParameterModifiers.Params)
     {
         UnlockWith(parameterDeclarationExpression);
     }
     return(base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data));
 }
 public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     if (RenameTable.ContainsKey(parameterDeclarationExpression.ParameterName))
     {
         parameterDeclarationExpression.ParameterName = RenameTable[parameterDeclarationExpression.ParameterName];
     }
     return(null);
 }
Exemplo n.º 20
0
 public Variable(Location start, Location end, ParameterDeclarationExpression param)
 {
     this.Type        = param.TypeReference;
     this.Name        = param.ParameterName;
     this.StartPos    = start;
     this.EndPos      = end;
     this.Initializer = param.DefaultValue;
     this.WasOutParam = (param.ParamModifier & ParameterModifiers.Out) == ParameterModifiers.Out;
     this.WasRefParam = (param.ParamModifier & ParameterModifiers.Ref) == ParameterModifiers.Ref;
 }
Exemplo n.º 21
0
        /// <summary>
        /// Prepends a @ to a parameter name in case the name chosen is also a keyword.
        /// </summary>
        /// <param name="pde"></param>
        /// <returns></returns>
        public static string ParameterNameSafe(this ParameterDeclarationExpression pde)
        {
            var p_name = pde.ParameterName;

            if (Keywords.IsNonIdentifierKeyword(p_name))
            {
                return("@" + p_name);
            }
            return(p_name);
        }
 public Variable(BlockStatement block, ParameterDeclarationExpression param)
 {
     this.Type        = param.TypeReference;
     this.Name        = param.ParameterName;
     this.StartPos    = block.StartLocation;
     this.EndPos      = block.EndLocation;
     this.Initializer = param.DefaultValue;
     this.WasOutParam = (param.ParamModifier & ParameterModifiers.Out) == ParameterModifiers.Out;
     this.WasRefParam = (param.ParamModifier & ParameterModifiers.Ref) == ParameterModifiers.Ref;
 }
Exemplo n.º 23
0
        public void GenerateTestMethod_InstanceMethod()
        {
            #region public class Cruncher
            var classUnderTest = new TypeDeclaration(Modifiers.Public, null)
            {
                Name = "Cruncher"
            };

            #region internal int SafeCrunch ( int x, int y )
            var methodToTest = new MethodDeclaration
            {
                Name     = "SafeCrunch",
                Modifier = Modifiers.Internal
            };

            #region returns int
            var retval = new TypeReference("int", true);
            methodToTest.TypeReference = retval;
            #endregion

            #region int x
            var xParam = new ParameterDeclarationExpression(new TypeReference("int", true), "x");
            methodToTest.Parameters.Add(xParam);
            #endregion

            #region int y
            var yParam = new ParameterDeclarationExpression(new TypeReference("int", true), "y");
            methodToTest.Parameters.Add(yParam);
            #endregion

            #endregion

            classUnderTest.Children.Add(methodToTest);
            #endregion

            const string expected = @"        /// <summary>
        /// Tests the <c>SafeCrunch</c> method with
        /// TODO: write about scenario
        /// </summary>
        [Test()]
        public void SafeCrunch_TODO()
        {
            Cruncher cruncher = new Cruncher();
            Assert.Fail(""TODO: initialize variable(s) and expected value"");
            int x = 0;
            int y = 0;
            int actual = cruncher.SafeCrunch(x, y);
            int expected = 0;
            Assert.AreEqual(expected, actual);
        }
";
            Assert.AreEqual(expected, Parent.UnitTest.GenerateTestMethod(methodToTest, classUnderTest));
        }
Exemplo n.º 24
0
 // general:
 public virtual object Visit(ParameterDeclarationExpression parameterDeclarationExpression, object data)
 {
     Debug.Assert(parameterDeclarationExpression != null);
     Debug.Assert(parameterDeclarationExpression.Attributes != null);
     Debug.Assert(parameterDeclarationExpression.TypeReference != null);
     foreach (AttributeSection section in parameterDeclarationExpression.Attributes)
     {
         Debug.Assert(section != null);
         section.AcceptVisitor(this, data);
     }
     return(parameterDeclarationExpression.TypeReference.AcceptVisitor(this, data));
 }
Exemplo n.º 25
0
 private bool ContainsParameter(IList parameters, ParameterDeclarationExpression parameterDeclaration)
 {
     foreach (ParameterDeclarationExpression parameter in parameters)
     {
         if (parameterDeclaration.ParameterName == parameter.ParameterName &&
             parameterDeclaration.TypeReference.Type == parameter.TypeReference.Type)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 26
0
        public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data)
        {
            if (invocationExpression.TargetObject is IdentifierExpression)
            {
                TypeDeclaration      typeDeclaration  = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration));
                IdentifierExpression methodIdentifier = (IdentifierExpression)invocationExpression.TargetObject;

                if (typeDeclaration.Parent is TypeDeclaration)
                {
                    List <ParameterDeclarationExpression> argList = new List <ParameterDeclarationExpression>();
                    int i = 0;
                    foreach (Expression argument in invocationExpression.Arguments)
                    {
                        TypeReference argumentType = GetExpressionType(argument);
                        if (argumentType != null)
                        {
                            string argType = argumentType.Type;

                            TypeReference typeReference = new TypeReference(argType);
                            ParameterDeclarationExpression parameterExpression = new ParameterDeclarationExpression(typeReference, "arg" + i);
                            parameterExpression.TypeReference.RankSpecifier = new int[0];
                            i++;
                            argList.Add(parameterExpression);
                        }
                    }
                    MethodDeclaration argMethod = new MethodDeclaration(methodIdentifier.Identifier, Modifiers.None, null, argList, null);

                    IList parentMethods = GetAccessibleMethods((TypeDeclaration)typeDeclaration.Parent);
                    if (Contains(parentMethods, argMethod))
                    {
                        int methodIndex = IndexOf(parentMethods, argMethod);
                        argMethod = (MethodDeclaration)parentMethods[methodIndex];
                        if (!AstUtil.ContainsModifier(argMethod, Modifiers.Static))
                        {
                            string parentTypeName = ((TypeDeclaration)typeDeclaration.Parent).Name;
                            AddInstanceField(typeDeclaration, parentTypeName);
                            AddProperConstructor(typeDeclaration, parentTypeName);

                            FieldReferenceExpression newReference = new FieldReferenceExpression(
                                new IdentifierExpression(parentTypeName),
                                ((IdentifierExpression)invocationExpression.TargetObject).Identifier);
                            InvocationExpression newInvication = new InvocationExpression(newReference, invocationExpression.Arguments);
                            newInvication.Parent = invocationExpression.Parent;

                            ReplaceCurrentNode(newInvication);
                        }
                    }
                }
            }
            return(base.TrackedVisitInvocationExpression(invocationExpression, data));
        }
Exemplo n.º 27
0
        private void AddProperConstructor(TypeDeclaration typeDeclaration, string instanceFieldName)
        {
            TypeReference type = new TypeReference(instanceFieldName);
            ParameterDeclarationExpression fieldParameter = new ParameterDeclarationExpression(type, instanceFieldName);

            FieldReferenceExpression fieldReference      = new FieldReferenceExpression(new ThisReferenceExpression(), instanceFieldName);
            IdentifierExpression     right               = new IdentifierExpression(instanceFieldName);
            AssignmentExpression     assignment          = new AssignmentExpression(fieldReference, AssignmentOperatorType.Assign, right);
            ExpressionStatement      expressionStatement = new ExpressionStatement(assignment);
            string fullName = GetFullName(typeDeclaration);

            IList constructors = AstUtil.GetChildrenWithType(typeDeclaration, typeof(ConstructorDeclaration));

            if (constructors.Count == 0)
            {
                string name = typeDeclaration.Name;
                List <ParameterDeclarationExpression> parameters = new List <ParameterDeclarationExpression>();
                parameters.Add(fieldParameter);
                ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(name, Modifiers.Public, parameters, null);

                constructorDeclaration.Body = new BlockStatement();
                constructorDeclaration.Body.AddChild(expressionStatement);
                constructorDeclaration.Parent = typeDeclaration;
                CodeBase.References.Add("Cons:" + fullName, null);

                typeDeclaration.Children.Add(constructorDeclaration);
            }
            else
            {
                foreach (ConstructorDeclaration constructor in constructors)
                {
                    if (!ContainsParameter(constructor.Parameters, fieldParameter))
                    {
                        constructor.Parameters.Add(fieldParameter);
                        if (constructor.ConstructorInitializer != null)
                        {
                            ConstructorInitializer ci = constructor.ConstructorInitializer;
                            if (ci.ConstructorInitializerType == ConstructorInitializerType.This)
                            {
                                ci.Arguments.Add(new IdentifierExpression(fieldParameter.ParameterName));
                            }
                        }
                        constructor.Body.Children.Insert(0, expressionStatement);
                    }
                }
                if (!CodeBase.References.Contains("Cons:" + fullName))
                {
                    CodeBase.References.Add("Cons:" + fullName, null);
                }
            }
        }
Exemplo n.º 28
0
        public void GenerateTestMethod_Typical()
        {
            #region public static class Unformatter
            var classUnderTest = new TypeDeclaration(Modifiers.Public | Modifiers.Static, null)
            {
                Name = "Unformatter"
            };

            #region public static string[] Unformat ( this string format, string formatted )
            var methodToTest = new MethodDeclaration
            {
                Name = "Unformat", IsExtensionMethod = true, Modifier = Modifiers.Static
            };

            #region returns string[]
            var retval = new TypeReference("string", new[] { 0 });
            methodToTest.TypeReference = retval;
            #endregion

            #region string format
            var formatParam = new ParameterDeclarationExpression(new TypeReference("string", true), "format");
            methodToTest.Parameters.Add(formatParam);
            #endregion

            #region string formatted
            var formattedParam = new ParameterDeclarationExpression(new TypeReference("string", true), "formatted");
            methodToTest.Parameters.Add(formattedParam);
            #endregion

            #endregion

            classUnderTest.Children.Add(methodToTest);
            #endregion
            const string expected = @"        /// <summary>
        /// Tests the <c>Unformat</c> method with
        /// TODO: write about scenario
        /// </summary>
        [Test()]
        public void Unformat_TODO()
        {
            Assert.Fail(""TODO: initialize variable(s) and expected value"");
            string format = ""TODO"";
            string formatted = ""TODO"";
            string[] actual = Unformatter.Unformat(format, formatted);
            string[] expected = default(string[]);
            Assert.AreEqual(expected, actual);
        }
";
            Assert.AreEqual(expected, Parent.UnitTest.GenerateTestMethod(methodToTest, classUnderTest));
        }
        public static List <ParameterDeclarationExpression> ConvertParameters(IList <IParameter> parameters, ClassFinder targetContext)
        {
            List <ParameterDeclarationExpression> l = new List <ParameterDeclarationExpression>(parameters.Count);

            foreach (IParameter p in parameters)
            {
                ParameterDeclarationExpression pd = new ParameterDeclarationExpression(ConvertType(p.ReturnType, targetContext),
                                                                                       p.Name,
                                                                                       ConvertModifier(p.Modifiers));
                pd.Attributes = ConvertAttributes(p.Attributes, targetContext);
                l.Add(pd);
            }
            return(l);
        }
Exemplo n.º 30
0
        public void GenerateTestMethod_MethodWithNotReturnValue()
        {
            #region public class Cruncher
            var classUnderTest = new TypeDeclaration(Modifiers.Public, null)
            {
                Name = "Cruncher"
            };

            #region public static void CrunchNumbers ( int x, int y )
            var methodToTest = new MethodDeclaration
            {
                Name = "CrunchNumbers", Modifier = Modifiers.Public | Modifiers.Static
            };

            #region returns void
            var retval = new TypeReference("void", true);
            methodToTest.TypeReference = retval;
            #endregion

            #region int x
            var xParam = new ParameterDeclarationExpression(new TypeReference("int", true), "x");
            methodToTest.Parameters.Add(xParam);
            #endregion

            #region int y
            var yParam = new ParameterDeclarationExpression(new TypeReference("int", true), "y");
            methodToTest.Parameters.Add(yParam);
            #endregion

            #endregion

            classUnderTest.Children.Add(methodToTest);
            #endregion

            const string expected = @"        /// <summary>
        /// Tests the <c>CrunchNumbers</c> method with
        /// TODO: write about scenario
        /// </summary>
        [Test()]
        public void CrunchNumbers_TODO()
        {
            Assert.Fail(""TODO: initialize variable(s)"");
            int x = 0;
            int y = 0;
            Cruncher.CrunchNumbers(x, y);
        }
";
            Assert.AreEqual(expected, Parent.UnitTest.GenerateTestMethod(methodToTest, classUnderTest));
        }
Exemplo n.º 31
0
        public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
        {
            if (IncludeParameters)
            {
                if (parameterDeclarationExpression == null)
                {
                    return(data);
                }

                AddVariable(parameterDeclarationExpression.TypeReference,
                            parameterDeclarationExpression.ParameterName,
                            parameterDeclarationExpression.StartLocation,
                            parameterDeclarationExpression.EndLocation,
                            false, false, NullExpression.Instance, null);
            }

            return(base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data));
        }
Exemplo n.º 32
0
		public virtual object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data) {
			Debug.Assert((parameterDeclarationExpression != null));
			Debug.Assert((parameterDeclarationExpression.Attributes != null));
			Debug.Assert((parameterDeclarationExpression.TypeReference != null));
			Debug.Assert((parameterDeclarationExpression.DefaultValue != null));
			foreach (AttributeSection o in parameterDeclarationExpression.Attributes) {
				Debug.Assert(o != null);
				o.AcceptVisitor(this, data);
			}
			parameterDeclarationExpression.TypeReference.AcceptVisitor(this, data);
			return parameterDeclarationExpression.DefaultValue.AcceptVisitor(this, data);
		}
Exemplo n.º 33
0
		public virtual object TrackedVisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data) {
			return base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data);
		}
Exemplo n.º 34
0
			void AddParameter (AbstractCSharpNode parent, Mono.CSharp.AParametersCollection parameters)
			{
				if (parameters == null)
					return;
				var paramLocation = LocationsBag.GetLocations (parameters);
			
				for (int i = 0; i < parameters.Count; i++) {
					if (paramLocation != null && i > 0 && i - 1 < paramLocation.Count) 
						parent.AddChild (new CSharpTokenNode (Convert (paramLocation[i - 1]), 1), ParameterDeclarationExpression.Roles.Comma);
					var p = (Parameter)parameters.FixedParameters[i];
					var location = LocationsBag.GetLocations (p);
					
					ParameterDeclarationExpression parameterDeclarationExpression = new ParameterDeclarationExpression ();
					switch (p.ModFlags) {
					case Parameter.Modifier.OUT:
						parameterDeclarationExpression.ParameterModifier = ParameterModifier.Out;
						if (location != null)
							parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location[0]), "out".Length), ParameterDeclarationExpression.Roles.Keyword);
						break;
					case Parameter.Modifier.REF:
						parameterDeclarationExpression.ParameterModifier = ParameterModifier.Ref;
						if (location != null)
							parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location[0]), "ref".Length), ParameterDeclarationExpression.Roles.Keyword);
						break;
					case Parameter.Modifier.PARAMS:
						parameterDeclarationExpression.ParameterModifier = ParameterModifier.Params;
						if (location != null)
							parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location[0]), "params".Length), ParameterDeclarationExpression.Roles.Keyword);
						break;
					case Parameter.Modifier.This:
						parameterDeclarationExpression.ParameterModifier = ParameterModifier.This;
						if (location != null)
							parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location[0]), "this".Length), ParameterDeclarationExpression.Roles.Keyword);
						break;
					}
					if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
						parameterDeclarationExpression.AddChild ((INode)p.TypeExpression.Accept (this), ParameterDeclarationExpression.Roles.ReturnType);
					parameterDeclarationExpression.AddChild (new Identifier (p.Name, Convert (p.Location)), ParameterDeclarationExpression.Roles.Identifier);
					if (p.HasDefaultValue) {
						if (location != null)
							parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ParameterDeclarationExpression.Roles.Assign);
						parameterDeclarationExpression.AddChild ((INode)p.DefaultValue.Accept (this), ParameterDeclarationExpression.Roles.Expression);
					}
					parent.AddChild (parameterDeclarationExpression, InvocationExpression.Roles.Argument);
				}
			}
Exemplo n.º 35
0
		public sealed override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data) {
			BeginVisit(parameterDeclarationExpression);
			object result = TrackedVisitParameterDeclarationExpression(parameterDeclarationExpression, data);
			EndVisit(parameterDeclarationExpression);
			return result;
		}
Exemplo n.º 36
0
 private void FixedParameter(out ParameterDeclarationExpression p)
 {
     TypeReference reference;
     ParamModifier @in = ParamModifier.In;
     Point location = this.t.Location;
     if ((this.la.kind == 0x5c) || (this.la.kind == 0x63))
     {
         if (this.la.kind == 0x63)
         {
             base.lexer.NextToken();
             @in = ParamModifier.Ref;
         }
         else
         {
             base.lexer.NextToken();
             @in = ParamModifier.Out;
         }
     }
     this.Type(out reference);
     base.Expect(1);
     p = new ParameterDeclarationExpression(reference, this.t.val, @in);
     p.StartLocation = location;
     p.EndLocation = this.t.Location;
 }
Exemplo n.º 37
0
		public virtual object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data) {
			Debug.Assert((parameterDeclarationExpression != null));
			Debug.Assert((parameterDeclarationExpression.Attributes != null));
			Debug.Assert((parameterDeclarationExpression.TypeReference != null));
			Debug.Assert((parameterDeclarationExpression.DefaultValue != null));
			for (int i = 0; i < parameterDeclarationExpression.Attributes.Count; i++) {
				AttributeSection o = parameterDeclarationExpression.Attributes[i];
				Debug.Assert(o != null);
				nodeStack.Push(o);
				o.AcceptVisitor(this, data);
				o = (AttributeSection)nodeStack.Pop();
				if (o == null)
					parameterDeclarationExpression.Attributes.RemoveAt(i--);
				else
					parameterDeclarationExpression.Attributes[i] = o;
			}
			nodeStack.Push(parameterDeclarationExpression.TypeReference);
			parameterDeclarationExpression.TypeReference.AcceptVisitor(this, data);
			parameterDeclarationExpression.TypeReference = ((TypeReference)(nodeStack.Pop()));
			nodeStack.Push(parameterDeclarationExpression.DefaultValue);
			parameterDeclarationExpression.DefaultValue.AcceptVisitor(this, data);
			parameterDeclarationExpression.DefaultValue = ((Expression)(nodeStack.Pop()));
			return null;
		}
Exemplo n.º 38
0
 private void ParameterArray(out ParameterDeclarationExpression p)
 {
     TypeReference reference;
     base.Expect(0x5e);
     this.Type(out reference);
     base.Expect(1);
     p = new ParameterDeclarationExpression(reference, this.t.val, ParamModifier.Params);
 }
Exemplo n.º 39
0
	void ParameterArray(
#line  675 "cs.ATG" 
out ParameterDeclarationExpression p) {

#line  676 "cs.ATG" 
		TypeReference type; 
		Expect(95);
		Type(
#line  678 "cs.ATG" 
out type);
		Identifier();

#line  678 "cs.ATG" 
		p = new ParameterDeclarationExpression(type, t.val, ParameterModifiers.Params); 
	}
Exemplo n.º 40
0
	void LambdaExpressionParameter(
#line  2101 "cs.ATG" 
out ParameterDeclarationExpression p) {

#line  2102 "cs.ATG" 
		Location start = la.Location; p = null;
		TypeReference type;
		
		if (
#line  2106 "cs.ATG" 
Peek(1).kind == Tokens.Comma || Peek(1).kind == Tokens.CloseParenthesis) {
			Identifier();

#line  2108 "cs.ATG" 
			p = new ParameterDeclarationExpression(null, t.val);
			p.StartLocation = start; p.EndLocation = t.EndLocation;
			
		} else if (StartOf(10)) {
			Type(
#line  2111 "cs.ATG" 
out type);
			Identifier();

#line  2113 "cs.ATG" 
			p = new ParameterDeclarationExpression(type, t.val);
			p.StartLocation = start; p.EndLocation = t.EndLocation;
			
		} else SynErr(214);
	}
		public virtual object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data) {
			throw new global::System.NotImplementedException("ParameterDeclarationExpression");
		}
Exemplo n.º 42
0
		/// <summary>
		/// Creates an argfull stub for the specified implementation method.
		/// </summary>
		private MethodDeclaration CreateArgfull(MethodDeclaration template, bool skipThisParams, out bool hasThisParams)
		{
			hasThisParams = false;

			MethodDeclaration method = new MethodDeclaration(template.Name, template.Modifier,
				new TypeReference("Object"), new List<ParameterDeclarationExpression>(), new List<AttributeSection>());

			method.Body = new BlockStatement();

			Expression[] arguments = new Expression[template.Parameters.Count];

			// prepend a ScriptContext parameter and make all parameters Objects
			// (TODO: PhpReferences for ref parameters)
			method.Parameters.Add(new ParameterDeclarationExpression(new TypeReference("ScriptContext"), "__context"));

			int arg_counter = 0;
			foreach (ParameterDeclarationExpression param in template.Parameters)
			{
				ParameterDeclarationExpression new_param =
					new ParameterDeclarationExpression(new TypeReference("Object"), param.ParameterName);

				bool optional = false;

				if (Utility.IsDecoratedByAttribute(param.Attributes, Utility.OptionalAttrType))
				{
					AttributeSection section = new AttributeSection();
					new_param.Attributes.Add(section);
					section.Attributes.Add(new ICSharpCode.NRefactory.Parser.AST.Attribute(Utility.OptionalAttrType, null, null));

					optional = true;
				}

				bool this_param = Utility.IsDecoratedByAttribute(param.Attributes, "PHP.Core.ThisAttribute");
				if (this_param) hasThisParams = true;

				if (this_param && skipThisParams)
				{
					arguments[arg_counter++] = new PrimitiveExpression(null, String.Empty);
				}
				else
				{
					// generate conversion
					arguments[arg_counter++] = Convertor.ConvertTo(
						template.Name,
						new IdentifierExpression(param.ParameterName),
						param.TypeReference,
						method.Body,
						new ReturnStatement(new PrimitiveExpression(null, String.Empty)),
						Utility.IsDecoratedByAttribute(param.Attributes, "PHP.Core.NullableAttribute") || this_param,
						optional,
						arg_counter);

					method.Parameters.Add(new_param);
				}
			}

			// invoke the template method
			InvocationExpression invocation = new InvocationExpression(new IdentifierExpression(template.Name),
				new ArrayList(arguments));

			if (template.TypeReference.SystemType == "System.Void")
			{
				method.Body.AddChild(new StatementExpression(invocation));
				method.Body.AddChild(new ReturnStatement(new PrimitiveExpression(null, String.Empty)));
			}
			else method.Body.AddChild(new ReturnStatement(invocation));

			if (!hasThisParams || skipThisParams) Utility.MakeNonBrowsable(method);
			return method;
		}
Exemplo n.º 43
0
	void FormalParameter(
#line  2642 "VBNET.ATG" 
out ParameterDeclarationExpression p) {

#line  2644 "VBNET.ATG" 
		AttributeSection section;
		List<AttributeSection> attributes = new List<AttributeSection>();
		TypeReference type = null;
		ParamModifierList mod = new ParamModifierList(this);
		Expression expr = null;
		p = null;
		ArrayList arrayModifiers = null;
		
		while (la.kind == 28) {
			AttributeSection(
#line  2653 "VBNET.ATG" 
out section);

#line  2653 "VBNET.ATG" 
			attributes.Add(section); 
		}
		while (StartOf(35)) {
			ParameterModifier(
#line  2654 "VBNET.ATG" 
mod);
		}
		Identifier();

#line  2655 "VBNET.ATG" 
		string parameterName = t.val; 
		if (
#line  2656 "VBNET.ATG" 
IsDims()) {
			ArrayTypeModifiers(
#line  2656 "VBNET.ATG" 
out arrayModifiers);
		}
		if (la.kind == 50) {
			lexer.NextToken();
			TypeName(
#line  2657 "VBNET.ATG" 
out type);
		}

#line  2659 "VBNET.ATG" 
		if(type != null) {
		if (arrayModifiers != null) {
			if (type.RankSpecifier != null) {
				Error("array rank only allowed one time");
			} else {
				type.RankSpecifier = (int[])arrayModifiers.ToArray(typeof(int));
			}
		}
		} else {
			type = new TypeReference("System.Object", arrayModifiers == null ? null : (int[])arrayModifiers.ToArray(typeof(int)));
		}
		
		if (la.kind == 10) {
			lexer.NextToken();
			Expr(
#line  2671 "VBNET.ATG" 
out expr);
		}

#line  2673 "VBNET.ATG" 
		mod.Check();
		p = new ParameterDeclarationExpression(type, parameterName, mod.Modifier, expr);
		p.Attributes = attributes;
		
	}
Exemplo n.º 44
0
		public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
		{
			curBlock.Add(parameterDeclarationExpression.ParameterName);
			//print("add parameter ${parameterDeclarationExpression.ParameterName} to block")
			return base.VisitParameterDeclarationExpression(parameterDeclarationExpression, data);
		}
Exemplo n.º 45
0
	void FixedParameter(
#line  661 "cs.ATG" 
out ParameterDeclarationExpression p) {

#line  663 "cs.ATG" 
		TypeReference type;
		ParameterModifiers mod = ParameterModifiers.In;
		Location start = la.Location;
		
		if (la.kind == 93 || la.kind == 100) {
			if (la.kind == 100) {
				lexer.NextToken();

#line  669 "cs.ATG" 
				mod = ParameterModifiers.Ref; 
			} else {
				lexer.NextToken();

#line  670 "cs.ATG" 
				mod = ParameterModifiers.Out; 
			}
		}
		Type(
#line  672 "cs.ATG" 
out type);
		Identifier();

#line  672 "cs.ATG" 
		p = new ParameterDeclarationExpression(type, t.val, mod); p.StartLocation = start; p.EndLocation = t.Location; 
	}
Exemplo n.º 46
0
		public override object VisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
		{
			CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(ConvType(parameterDeclarationExpression.TypeReference), parameterDeclarationExpression.ParameterName);

            if (parameterDeclarationExpression.ParamModifier == ParameterModifiers.Params)
                parameter.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(ParamArrayAttribute))));

			parameters.Add(parameter);

			return parameter;
		}