public void FieldMistakenForVariable()
        {
            // Same thing happens with properties etc. Does not happen when using explicit this.
            CompleteInClass(@" 
                public string Str;

                public void M()
                {
                    Str.$
                }");


            //{
            //    "$type": "[SST:Statements.ExpressionStatement]",
            //    "Expression": {
            //        "$type": "[SST:Expressions.Assignable.CompletionExpression]",
            //        "VariableReference": {
            //            "$type": "[SST:References.VariableReference]",
            //            "Identifier": "Str"
            //        },
            //        "Token": ""
            //    }
            //}

            AssertBody(
                "M",
                VarDecl("$0", Fix.String),
                Assign("$0", RefExpr(FieldRef(Fix.Field(Fix.String, Type("C"), "Str"), VarRef("this")))),
                ExprStmt(Fix.CompletionOnVar(VarRef("$0"), "")));
        }
示例#2
0
        public void Right_NewHandler()
        {
            CompleteInClass(@"
                private event Handler E;
                private delegate void Handler(int i);
                private void Listener(int i) { }

                public void M()
                {
                    E += new Handler(Listener);
                    $
                }
            ");

            var en = ResultSST.Methods.GetEnumerator();

            en.MoveNext(); // .Listener
            en.MoveNext(); // .M
            Assert.NotNull(en.Current);
            var stmt   = en.Current.Body.FirstOrDefault(new EventSubscriptionStatement());
            var esStmt = stmt as IEventSubscriptionStatement;

            Assert.NotNull(esStmt);
            var inv = esStmt.Expression as IInvocationExpression;

            Assert.NotNull(inv);
            var actual = inv.MethodName;

            var delegateType = Names.Type("d:[{0}] [N.C+Handler, TestProject].([{1}] i)", Fix.Void, Fix.Int);
            var parameter    = Names.Parameter("[{0}] target", delegateType);
            var ctor         = Fix.Ctor(delegateType, parameter);

            Assert.AreEqual(ctor, actual);
        }
        public void IndexAccessOnMethodResult()
        {
            CompleteInClass(@"        
                public int[] GetArray()
                {
                    return new int[10];
                }

                public void M()
                {
                    int i = this.GetArray()[1];
                    $
                }");

            AssertBody(
                "M",
                VarDecl("i", Fix.Int),
                VarDecl("$0", Fix.IntArray),
                Assign("$0", Invoke("this", Fix.Method(Fix.IntArray, Type("C"), "GetArray"))),
                Assign(
                    "i",
                    new IndexAccessExpression
            {
                Reference = VarRef("$0"),
                Indices   = { Const("1") }
            }),
                Fix.EmptyCompletion);
        }
示例#4
0
        public void Right_NewHandler()
        {
            CompleteInClass(@"
                private event Handler E;
                private delegate void Handler(int i);
                private void Listener(int i) { }

                public void M()
                {
                    E += new Handler(Listener);
                    $
                }
            ");

            var delegateType =
                Names.Type(string.Format("d:[{0}] [N.C+Handler, TestProject].([{1}] i)", Fix.Void, Fix.Int));
            var parameter = Names.Parameter("[{0}] target", delegateType);
            var ctor      = Fix.Ctor(delegateType, parameter);

            var listenerName =
                Names.Method(string.Format("[{0}] [{1}].Listener([{2}] i)", Fix.Void, Fix.TestClass, Fix.Int));

            AssertBody(
                "M",
                new EventSubscriptionStatement
            {
                Reference  = EventRef("E", delegateType),
                Operation  = EventSubscriptionOperation.Add,
                Expression = InvokeCtor(ctor, RefExpr(MethodRef(listenerName, VarRef("this"))))
            },
                ExprStmt(new CompletionExpression()));
        }
        public void VariableFromMethodCall()
        {
            CompleteInClass(@"
                public StreamWriter M()
                {
                    return new StreamWriter(""file.txt"");
                }
                
                public void N()
                {
                    using (M())
                    {
                        $
                        return;
                    }
                }");

            AssertBody(
                "N",
                VarDecl("$0", _streamWriter),
                Assign("$0", Invoke("this", Fix.Method(_streamWriter, Type("C"), "M"))),
                new UsingBlock
            {
                Reference = VarRef("$0"),
                Body      =
                {
                    Fix.EmptyCompletion,
                    new ReturnStatement {
                        IsVoid = true
                    }
                }
            });
        }
        public void StaticMembersWhenCalledByAlias()
        {
            // This completion is only misanalysed when an alias is used. Completing "Object.$" yields correct results.
            // This is also true for other aliases such as "string" or "int".
            CompleteInMethod(@"
                object.$
            ");

            AssertBody(ExprStmt(Fix.CompletionOnType(Fix.Object, "")));
        }
示例#7
0
        public void Assigning_InvocationExpressions()
        {
            CompleteInMethod(@"
                var o = this.ToString();
                $
            ");

            AssertBody(
                VarDecl("o", Fix.String),
                Assign("o", Invoke("this", Fix.ToString(Fix.Object))),
                Fix.EmptyCompletion);
        }
        public void PrefixMinusMinus_After()
        {
            CompleteInMethod(@"
                --this;
                $
            ");

            AssertCompletionMarker <IPrefixOperatorExpression>(CompletionCase.EmptyCompletionAfter);

            AssertBody(
                Assign("this", Fix.ComposedExpr("this")),
                Fix.EmptyCompletion);
        }
        public void PrefixPlusPlus_Before()
        {
            CompleteInMethod(@"
                $
                ++this;
            ");

            AssertCompletionMarker <IPrefixOperatorExpression>(CompletionCase.EmptyCompletionBefore);

            AssertBody(
                Fix.EmptyCompletion,
                Assign("this", Fix.ComposedExpr("this")));
        }
示例#10
0
        public void InvocationOnInvocation()
        {
            CompleteInMethod(@"
                ToString().GetHashCode();
                $
            ");

            AssertBody(
                VarDecl("$0", Fix.String),
                Assign("$0", Invoke("this", Fix.ToString(Fix.Object))),
                InvokeStmt("$0", Fix.GetHashCode(Fix.String)),
                ExprStmt(new CompletionExpression()));
        }
        public void AssigningArray()
        {
            CompleteInMethod(@"
                var array = new[] {1, 2, 3, 4, 5};
                array.$");

            // Analysis will assign UnknownExpression. Should probably be ConstantValueExpression instead.
            AssertBody(
                VarDecl("array", Names.ArrayType(1, Fix.Int)),
                Assign("array", new ConstantValueExpression()),
                ExprStmt(Fix.CompletionOnVar(VarRef("array"), "")));

            Assert.Fail();
        }
        public void Unary_Minus()
        {
            CompleteInMethod(@"
                var i = 1;
                var j = -i;
                $
            ");

            AssertBody(
                VarDecl("i", Fix.Int),
                Assign("i", Const("1")),
                VarDecl("j", Fix.Int),
                Assign("j", Fix.ComposedExpr("i")),
                Fix.EmptyCompletion);
        }
示例#13
0
        public void InvocationOnField()
        {
            CompleteInClass(@"
                private int i;
                public void M(C c) {
                    c.i.GetHashCode();
                    $
                }
            ");

            AssertBody(
                VarDecl("$0", Fix.Int),
                Assign("$0", RefExpr(FieldRef("i", Fix.Int, "c"))),
                InvokeStmt("$0", Fix.GetHashCode(Fix.Int)),
                ExprStmt(new CompletionExpression()));
        }
示例#14
0
        public void InvocationOnProperty_ImplicitThis()
        {
            CompleteInClass(@"
                private int I { get; set; }
                public void M() {
                    I.GetHashCode();
                    $
                }
            ");

            AssertBody(
                VarDecl("$0", Fix.Int),
                Assign("$0", RefExpr(PropRef("I", Fix.Int))),
                InvokeStmt("$0", Fix.GetHashCode(Fix.Int)),
                ExprStmt(new CompletionExpression()));
        }
        public void UnnecessaryReassignmentOfThis()
        {
            CompleteInClass(@"
                public void M()
                {
                    this.GetH$
                }");

            //C $0;
            //$0 = this;
            //$0.GetH$;

            AssertBody(
                "M",
                ExprStmt(Fix.CompletionOnVar(VarRef("this"), "GetH")));
        }
        public void CastingMethodResult()
        {
            CompleteInClass(@"
                public int GetInt() { return 1; }
                public void M() 
                {
                    var i = (float)GetInt();
                    $
                }");

            AssertBody(
                "M",
                VarDecl("i", Fix.Float),
                VarDecl("$0", Fix.Int),
                Assign("$0", Invoke("this", Fix.Method(Fix.Int, Type("C"), "GetInt"))),
                Assign("i", new CastExpression {
                TargetType = Fix.Float, Reference = VarRef("$0")
            }),
                Fix.EmptyCompletion);
        }
        public void CallingMethodWithProperty()
        {
            CompleteInClass(@"        
                public string Name { get; set; }

                public void M()
                {
                    N(this.Name);
                }

                public void N(string s)
                {
                    s.$
                }");

            var nMethod         = Fix.Method(Fix.Void, Type("C"), "N", Fix.Parameter(Fix.String, "s"));
            var namePropertyRef = PropertyRef(Fix.Property(Fix.String, Type("C"), "Name"), VarRef("this"));

            AssertBody(
                "M",
                ExprStmt(Invoke("this", nMethod, RefExpr(namePropertyRef))));
        }
示例#18
0
        public void InvocationOnProperty_Base()
        {
            CompleteInCSharpFile(@"
                namespace N {
                    class C {
                        protected int I { get; set; }
                    }
                    class S : C {
                        protected int i;
                        public void M() {
                            base.I.GetHashCode();
                            $
                        }
                    }
                }
            ");

            AssertBody(
                VarDecl("$0", Fix.Int),
                Assign("$0", RefExpr(PropRef("I", Fix.Int, "base"))),
                InvokeStmt("$0", Fix.GetHashCode(Fix.Int)),
                ExprStmt(new CompletionExpression()));
        }
        public void ComplexInlineIfElse()
        {
            CompleteInClass(@"
                public void A(object o1, object o2, object o3)
                {
                    var compare = (o2.GetHashCode() > o3.GetHashCode())
                        ? o1.ToString().Equals(o2.ToString())
                        : o1.ToString().Equals(o3.ToString());
                    $
                }
            ");

            var mA = NewMethodDeclaration(
                Fix.Void,
                "A",
                string.Format("[{0}] o1", Fix.Object),
                string.Format("[{0}] o2", Fix.Object),
                string.Format("[{0}] o3", Fix.Object));

            mA.Body.Add(SSTUtil.Declare("compare", Fix.Bool));
            var ifBlock = new LoopHeaderBlockExpression(); // {Value = new[] {"$0", "$1"}};

            ifBlock.Body.Add(SSTUtil.Declare("$0", Fix.Int));
            ifBlock.Body.Add(
                SSTUtil.AssignmentToLocal("$0", SSTUtil.InvocationExpression("o2", Fix.GetHashCode(Fix.Object))));
            ifBlock.Body.Add(SSTUtil.Declare("$1", Fix.Int));
            ifBlock.Body.Add(
                SSTUtil.AssignmentToLocal("$1", SSTUtil.InvocationExpression("o3", Fix.GetHashCode(Fix.Object))));
            var thenBlock = new LoopHeaderBlockExpression(); //) {Value = new[] {"$4"}};

            thenBlock.Body.Add(SSTUtil.Declare("$2", Fix.String));
            thenBlock.Body.Add(
                SSTUtil.AssignmentToLocal("$2", SSTUtil.InvocationExpression("o1", Fix.ToString(Fix.Object))));
            thenBlock.Body.Add(SSTUtil.Declare("$3", Fix.String));
            thenBlock.Body.Add(
                SSTUtil.AssignmentToLocal("$3", SSTUtil.InvocationExpression("o2", Fix.ToString(Fix.Object))));
            thenBlock.Body.Add(SSTUtil.Declare("$4", Fix.Bool));
            thenBlock.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$4",
                    SSTUtil.InvocationExpression(
                        "$2",
                        Fix.Equals(Fix.String, Fix.String, "value"),
                        new[] { RefExpr("$3") })));
            var elseBlock = new LoopHeaderBlockExpression(); //) {Value = new[] {"$7"}};

            elseBlock.Body.Add(SSTUtil.Declare("$5", Fix.String));
            //elseBlock.Body.Add(SSTUtil.AssignmentToLocal("$5", InvocationExpression("o1", Fix.ToString(Fix.Object))));
            elseBlock.Body.Add(SSTUtil.Declare("$6", Fix.String));
            //elseBlock.Body.Add(SSTUtil.AssignmentToLocal("$6", InvocationExpression("o3", Fix.ToString(Fix.Object))));
            elseBlock.Body.Add(SSTUtil.Declare("$7", Fix.Bool));
            elseBlock.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$7",
                    SSTUtil.InvocationExpression(
                        "$5",
                        Fix.Equals(Fix.String, Fix.String, "value"),
                        new[] { RefExpr("$6") })));
            //  mA.Body.Add(
            //         new Assignment(
            //             "compare",
            //             new IfElseExpression {Condition = ifBlock, ThenExpression = thenBlock, ElseExpression = elseBlock}));
            AssertAllMethods(mA);
        }