public void CombinedInlineIfElse()
        {
            CompleteInClass(@"
                public void A()
                {
                    var i = (true ? 1 : 2) + 3;
                    $
                }
            ");

            var mA = NewMethodDeclaration(Fix.Void, "A");

            mA.Body.Add(SSTUtil.Declare("i", Fix.Int));
            mA.Body.Add(SSTUtil.Declare("$0", Fix.Int));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$0",
                    new IfElseExpression
            {
                Condition      = new ConstantValueExpression(),
                ThenExpression = new ConstantValueExpression(),
                ElseExpression = new ConstantValueExpression()
            }));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", SSTUtil.ComposedExpression("$0")));

            AssertAllMethods(mA);
        }
        public void Invocation_SimpleComposition()
        {
            CompleteInCSharpFile(@"
                namespace N {
                    public class H {
                        public int Get() {
                            return 1;
                        }
                    }
                    public class C {
                        public void A(H h) {
                            var i = 1 + h.Get();
                            $
                        }
                    }
                }
            ");

            var mA = NewMethodDeclaration(Fix.Void, "A", "[N.H, TestProject] h");

            mA.Body.Add(SSTUtil.Declare("i", Fix.Int));
            mA.Body.Add(SSTUtil.Declare("$0", Fix.Int));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$0",
                    SSTUtil.InvocationExpression(
                        "h",
                        Names.Method("[{0}] [N.H, TestProject].Get()", Fix.Int))));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", SSTUtil.ComposedExpression("$0")));

            AssertAllMethods(mA);
        }
示例#3
0
        public void Declare()
        {
            var actual   = SSTUtil.Declare("a", Names.UnknownType);
            var expected = new VariableDeclaration
            {
                Reference = Ref("a"),
                Type      = Names.UnknownType
            };

            Assert.AreEqual(expected, actual);
        }
        public void CombinedCast()
        {
            CompleteInClass(@"
                public void A(object o)
                {
                    var i = ((string) o) + ((string) 1.0);
                    $
                }
            ");

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

            mA.Body.Add(SSTUtil.Declare("i", Fix.String));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", SSTUtil.ComposedExpression("o")));

            AssertAllMethods(mA);
        }
        public void CombinedUnary()
        {
            CompleteInClass(@"
                public void A(int i)
                {
                    var j = 0 + (-i);
                    $
                }
            ");

            var mA = NewMethodDeclaration(Fix.Void, "A", string.Format("[{0}] i", Fix.Int));

            mA.Body.Add(SSTUtil.Declare("j", Fix.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("j", SSTUtil.ComposedExpression("i")));

            AssertAllMethods(mA);
        }
        public void CompositionOfTypeOf()
        {
            CompleteInClass(@"
                public void A()
                {
                    var t = typeof(int) == typeof(string);
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("t", SSTAnalysisFixture.Bool));
            mA.Body.Add(SSTUtil.AssignmentToLocal("t", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
        public void TypeOf()
        {
            CompleteInClass(@"
                public void A()
                {
                    var t = typeof(int);
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("t", Names.Type("System.Type, mscorlib, 4.0.0.0")));
            mA.Body.Add(SSTUtil.AssignmentToLocal("t", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
        public void CombinedDefault()
        {
            CompleteInClass(@"
                public void A()
                {
                    var i = 0 + default(int);
                    $
                }
            ");

            var mA = NewMethodDeclaration(Fix.Void, "A");

            mA.Body.Add(SSTUtil.Declare("i", Fix.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
        public void Cast_Const()
        {
            CompleteInClass(@"
                public void A()
                {
                    var i = (int) 1.0;
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("i", SSTAnalysisFixture.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
        public void As_Const()
        {
            CompleteInClass(@"
                public void A()
                {
                    var cast = 1.0 as object;
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("cast", SSTAnalysisFixture.Object));
            mA.Body.Add(SSTUtil.AssignmentToLocal("cast", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
示例#11
0
        public void ArrayInit_Constant()
        {
            CompleteInClass(@"
                public void A()
                {
                    var arr = new[] {1,2,3};
                    $
                }
            ");

            var mA = NewMethodDeclaration(Fix.Void, "A");

            mA.Body.Add(SSTUtil.Declare("arr", Fix.IntArray));
            mA.Body.Add(SSTUtil.AssignmentToLocal("arr", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
        public void Is_Const()
        {
            CompleteInClass(@"
                public void A()
                {
                    var isInstanceOf = 1 is double;
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("isInstanceOf", SSTAnalysisFixture.Bool));
            mA.Body.Add(SSTUtil.AssignmentToLocal("isInstanceOf", new ConstantValueExpression()));

            AssertAllMethods(mA);
        }
示例#13
0
        public override void VisitLocalVariableDeclaration(ILocalVariableDeclaration decl, IList <IStatement> body)
        {
            if (IsTargetMatch(decl, CompletionCase.EmptyCompletionBefore))
            {
                body.Add(EmptyCompletionExpression);
            }

            var       id = decl.DeclaredName;
            ITypeName type;

            try
            {
                type = decl.Type.GetName();
            }
            catch (AssertException)
            {
                // TODO this is an intermediate "fix"... the analysis sometimes fails here ("cannot create name for anonymous type")
                type = Names.UnknownType;
            }
            body.Add(SSTUtil.Declare(id, type));

            IAssignableExpression initializer = null;

            if (decl.Initial != null)
            {
                initializer = _exprVisitor.ToAssignableExpr(decl.Initial, body);
            }
            else if (_marker.HandlingNode == decl && _marker.Case == CompletionCase.InBody)
            {
                initializer = new CompletionExpression();
            }

            if (initializer != null)
            {
                if (!IsSelfAssign(id, initializer))
                {
                    body.Add(SSTUtil.AssignmentToLocal(id, initializer));
                }
            }

            if (decl == _marker.HandlingNode && _marker.Case == CompletionCase.EmptyCompletionAfter)
            {
                body.Add(EmptyCompletionExpression);
            }
        }
        public void Invocation_NestedComposition()
        {
            CompleteInCSharpFile(@"
                namespace N {
                    public class U {
                        public int Plus(int i) {
                            return i + 1;
                        }
                    }
                    public class C {
                        public void A(U u1, U u2) {
                            var i = u1.Plus(u2.Plus(1));
                            $
                        }
                    }
                }
            ");

            var mA = NewMethodDeclaration(
                Fix.Void,
                "A",
                "[N.U, TestProject] u1",
                "[N.U, TestProject] u2");

            mA.Body.Add(SSTUtil.Declare("i", Fix.Int));
            mA.Body.Add(SSTUtil.Declare("$0", Fix.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("$0", new ConstantValueExpression()));
            mA.Body.Add(SSTUtil.Declare("$1", Fix.Int));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$1",
                    SSTUtil.InvocationExpression(
                        "u2",
                        Names.Method(string.Format("[{0}] [N.U, TestProject].Plus([{0}] i)", Fix.Int)),
                        RefExprs("$0"))));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "i",
                    SSTUtil.InvocationExpression(
                        "u1",
                        Names.Method(string.Format("[{0}] [N.U, TestProject].Plus([{0}] i)", Fix.Int)),
                        RefExprs("$1"))));

            AssertAllMethods(mA);
        }
        public void Invocation_RealComposition()
        {
            CompleteInCSharpFile(@"
                namespace N {
                    public class H {
                        public int Get() {
                            return 1;
                        }
                    }
                    public class C {
                        public void A(H h1, H h2) {
                            var i = h1.Get() + h2.Get() + 1;
                            $
                        }
                    }
                }
            ");

            var mA = NewMethodDeclaration(
                Fix.Void,
                "A",
                "[N.H, TestProject] h1",
                "[N.H, TestProject] h2");

            mA.Body.Add(SSTUtil.Declare("i", Fix.Int));
            mA.Body.Add(SSTUtil.Declare("$0", Fix.Int));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$0",
                    SSTUtil.InvocationExpression(
                        "h1",
                        Names.Method(string.Format("[{0}] [N.H, TestProject].Get()", Fix.Int)))));
            mA.Body.Add(SSTUtil.Declare("$1", Fix.Int));
            mA.Body.Add(
                SSTUtil.AssignmentToLocal(
                    "$1",
                    SSTUtil.InvocationExpression(
                        "h2",
                        Names.Method(string.Format("[{0}] [N.H, TestProject].Get()", Fix.Int)))));
            mA.Body.Add(SSTUtil.AssignmentToLocal("i", SSTUtil.ComposedExpression("$0", "$1")));

            AssertAllMethods(mA);
        }
示例#16
0
        public void ForEachLoop()
        {
            var sst = new ForEachLoop
            {
                Declaration     = SSTUtil.Declare("e", Names.Type("T,P")),
                LoopedReference = SSTUtil.VariableReference("elements"),
                Body            =
                {
                    new ContinueStatement()
                }
            };

            AssertPrint(
                sst,
                "foreach (T e in elements)",
                "{",
                "    continue;",
                "}");
        }
示例#17
0
        public void ArrayInit_Composed()
        {
            CompleteInClass(@"
                public void A()
                {
                    var n = 1;
                    var arr = new[] {1,2,n};
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("n", SSTAnalysisFixture.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("n", new ConstantValueExpression()));
            mA.Body.Add(SSTUtil.Declare("arr", SSTAnalysisFixture.IntArray));
            mA.Body.Add(SSTUtil.AssignmentToLocal("arr", SSTUtil.ComposedExpression("n")));

            AssertAllMethods(mA);
        }
        public void Is_Reference()
        {
            CompleteInClass(@"
                public void A(object o)
                {
                    var isInstanceOf = o is string;
                    $
                }
            ");

            var mA = NewMethodDeclaration(
                SSTAnalysisFixture.Void,
                "A",
                string.Format("[{0}] o", SSTAnalysisFixture.Object));

            mA.Body.Add(SSTUtil.Declare("isInstanceOf", SSTAnalysisFixture.Bool));
            mA.Body.Add(SSTUtil.AssignmentToLocal("isInstanceOf", SSTUtil.ComposedExpression("o")));

            AssertAllMethods(mA);
        }
示例#19
0
        public void ArrayInit_WithArrays()
        {
            CompleteInClass(@"
                public void A()
                {
                    var arr = new[] {new[] {1, 2}, new[] {3, 4}};
                    $
                }
            ");

            var mA = NewMethodDeclaration(SSTAnalysisFixture.Void, "A");

            mA.Body.Add(SSTUtil.Declare("arr", Names.Type("System.Int32[][], mscorlib, 4.0.0.0")));
            mA.Body.Add(SSTUtil.Declare("$0", SSTAnalysisFixture.IntArray));
            mA.Body.Add(SSTUtil.AssignmentToLocal("$0", new ConstantValueExpression()));
            mA.Body.Add(SSTUtil.Declare("$1", SSTAnalysisFixture.IntArray));
            mA.Body.Add(SSTUtil.AssignmentToLocal("$1", new ConstantValueExpression()));
            mA.Body.Add(SSTUtil.AssignmentToLocal("arr", SSTUtil.ComposedExpression("$0", "$1")));

            AssertAllMethods(mA);
        }
示例#20
0
        public void ForLoop()
        {
            var sst = new ForLoop
            {
                Init =
                {
                    SSTUtil.Declare("i",           Names.Type("T,P")),
                    SSTUtil.AssignmentToLocal("i", new ConstantValueExpression {
                        Value = "0"
                    })
                },
                Body      = { new ContinueStatement(), new BreakStatement() },
                Condition =
                    new LoopHeaderBlockExpression
                {
                    Body = { new ReturnStatement {
                                 Expression = new ConstantValueExpression{
                                     Value = "true"
                                 }
                             } }
                }
            };

            AssertPrint(
                sst,
                "for (",
                "    {",
                "        T i;",
                "        i = 0;",
                "    };",
                "    {",
                "        return true;",
                "    }; { }",
                ")",
                "{",
                "    continue;",
                "    break;",
                "}");
        }
示例#21
0
        public void ArrayInit_WithCalls()
        {
            CompleteInClass(@"
                public void A(object o)
                {
                    var arr = new[] {1,2,o.GetHashCode()};
                    $
                }
            ");

            var mA = NewMethodDeclaration(
                SSTAnalysisFixture.Void,
                "A",
                string.Format("[{0}] o", SSTAnalysisFixture.Object));

            mA.Body.Add(SSTUtil.Declare("arr", SSTAnalysisFixture.IntArray));
            mA.Body.Add(SSTUtil.Declare("$0", SSTAnalysisFixture.Int));
            mA.Body.Add(SSTUtil.AssignmentToLocal("$0", SSTUtil.InvocationExpression("o", Fix.Object_GetHashCode)));
            mA.Body.Add(SSTUtil.AssignmentToLocal("arr", SSTUtil.ComposedExpression("$0")));

            AssertAllMethods(mA);
        }
        public void VariableDeclaration_TranslatesSimpleTypeBackToAlias()
        {
            var sst = SSTUtil.Declare("var", Names.Type("System.Int32, mscore, 4.0.0.0"));

            AssertPrint(sst, "int var;");
        }
        public void VariableDeclaration()
        {
            var sst = SSTUtil.Declare("var", Names.Type("T,P"));

            AssertPrint(sst, "T var;");
        }
        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);
        }