Exemplo n.º 1
0
        public void TestSiblingsBeforeSelf_MissingChild()
        {
            var a = new VariableUse()
            {
                Name = "a"
            };
            var plus = new OperatorUse()
            {
                Text = "+"
            };
            var foo = new VariableUse()
            {
                Name = "foo"
            };
            var times = new OperatorUse()
            {
                Text = "*"
            };
            var b = new VariableUse()
            {
                Name = "b"
            };
            var exp = new Expression();

            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var dot = new OperatorUse {
                Text             = ".",
                ParentExpression = exp
            };

            Assert.Throws <InvalidOperationException>(() => dot.GetSiblingsBeforeSelf());
        }
Exemplo n.º 2
0
 private static void TestEquality(OperatorUse expected, OperatorUse actual, string propertyName)
 {
     try {
         IsTrue(expected.Text == actual.Text, "Text");
         ExpressionsAreEqual((Expression)expected, (Expression)actual, propertyName);
     } catch (DataAssertionException e) {
         e.Add(propertyName);
         throw e;
     }
 }
Exemplo n.º 3
0
 private bool IsNameInclusionOp(OperatorUse opu)
 {
     foreach (var op in NameInclusionOperators)
     {
         if (op.Equals(opu.Text))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 4
0
        public void TestSiblingsBeforeSelf_MissingChild() {
            var a = new VariableUse() { Name = "a" };
            var plus = new OperatorUse() { Text = "+" };
            var foo = new VariableUse() { Name = "foo" };
            var times = new OperatorUse() { Text = "*" };
            var b = new VariableUse() { Name = "b" };
            var exp = new Expression();
            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var dot = new OperatorUse {
                Text = ".",
                ParentExpression = exp
            };

            Assert.Throws<InvalidOperationException>(() => dot.GetSiblingsBeforeSelf());
        }
Exemplo n.º 5
0
        public void TestSiblingsBeforeSelf() {
            var a = new VariableUse() { Name = "a" };
            var plus = new OperatorUse() { Text = "+" };
            var foo = new VariableUse() { Name = "foo" };
            var times = new OperatorUse() { Text = "*" };
            var b = new VariableUse() { Name = "b" };
            var exp = new Expression();
            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var fooSiblings = foo.GetSiblingsBeforeSelf().ToList();
            Assert.AreEqual(2, fooSiblings.Count());
            Assert.AreSame(a, fooSiblings[0]);
            Assert.AreSame(plus, fooSiblings[1]);

            var aSiblings = a.GetSiblingsBeforeSelf().ToList();
            Assert.AreEqual(0, aSiblings.Count());
        }
Exemplo n.º 6
0
        public void TestSiblingsAfterSelf() {
            var a = new VariableUse() { Name = "a" };
            var plus = new OperatorUse() { Text = "+" };
            var foo = new VariableUse() { Name = "foo" };
            var times = new OperatorUse() { Text = "*" };
            var b = new VariableUse() { Name = "b" };
            var exp = new Expression();
            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var plusSiblings = plus.GetSiblingsAfterSelf().ToList();
            Assert.AreEqual(3, plusSiblings.Count());
            Assert.AreSame(foo, plusSiblings[0]);
            Assert.AreSame(times, plusSiblings[1]);
            Assert.AreSame(b, plusSiblings[2]);

            var bSiblings = b.GetSiblingsAfterSelf().ToList();
            Assert.AreEqual(0, bSiblings.Count());
        }
Exemplo n.º 7
0
        public void TestSiblingsAfterSelf()
        {
            var a = new VariableUse()
            {
                Name = "a"
            };
            var plus = new OperatorUse()
            {
                Text = "+"
            };
            var foo = new VariableUse()
            {
                Name = "foo"
            };
            var times = new OperatorUse()
            {
                Text = "*"
            };
            var b = new VariableUse()
            {
                Name = "b"
            };
            var exp = new Expression();

            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var plusSiblings = plus.GetSiblingsAfterSelf().ToList();

            Assert.AreEqual(3, plusSiblings.Count());
            Assert.AreSame(foo, plusSiblings[0]);
            Assert.AreSame(times, plusSiblings[1]);
            Assert.AreSame(b, plusSiblings[2]);

            var bSiblings = b.GetSiblingsAfterSelf().ToList();

            Assert.AreEqual(0, bSiblings.Count());
        }
Exemplo n.º 8
0
        public void TestSiblingsBeforeSelf()
        {
            var a = new VariableUse()
            {
                Name = "a"
            };
            var plus = new OperatorUse()
            {
                Text = "+"
            };
            var foo = new VariableUse()
            {
                Name = "foo"
            };
            var times = new OperatorUse()
            {
                Text = "*"
            };
            var b = new VariableUse()
            {
                Name = "b"
            };
            var exp = new Expression();

            exp.AddComponents(new Expression[] { a, plus, foo, times, b });

            var fooSiblings = foo.GetSiblingsBeforeSelf().ToList();

            Assert.AreEqual(2, fooSiblings.Count());
            Assert.AreSame(a, fooSiblings[0]);
            Assert.AreSame(plus, fooSiblings[1]);

            var aSiblings = a.GetSiblingsBeforeSelf().ToList();

            Assert.AreEqual(0, aSiblings.Count());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Find and update the next Name use (varable use)
        /// newPos will return the new start position
        /// </summary>
        /// <param name="subExps"></param>
        /// <param name="startPos"></param>
        /// <param name="newPos"></param>
        /// <returns></returns>
        private NameUse FindAndUpdateNextVarUse(List <Expression> subExps, int startPos, out int newPos, bool IsUpdate)
        {
            bool haveSeenUniop = false; //have seen uniop which (should) bind with a variable

            for (int curPos = startPos; curPos <= subExps.Count() - 1; curPos++)
            {
                var curExp = subExps.ElementAt(curPos);

                //The subExp contains method call
                if (curExp is MethodCall && IsUpdate)
                {
                    UpdateMethodCall((MethodCall)curExp);
                    continue;
                }

                ////skip everyting on the left of += = or -=, if ISUpdate == false, since we do not care the rest expression
                var curOp = curExp as OperatorUse;
                if (IsUpdate == false && curOp != null && (curOp.Text == "=" || curOp.Text == "+=" || curOp.Text == "-="))
                {
                    curPos = subExps.Count();
                    continue;
                }

                //record previous uniop
                if (curOp != null && (curOp.Text == "--" || curOp.Text == "++"))
                {
                    haveSeenUniop = true;
                    continue;
                }

                //class - might has static field visit such as int i = School.b;
                var varUse = curExp as NameUse;
                if (varUse != null)
                {
                    var td = varUse.FindMatches().FirstOrDefault() as TypeDefinition;
                    if (td != null)
                    {
                        continue;
                    }
                }

                //Find next Variable use
                bool IsFieldUpdate = false;
                var  curNameUse    = GetNameUseOrObj(curExp, out IsFieldUpdate);


                // No deep search and update if IsUpdate is false
                if (curNameUse != null && IsUpdate == false)
                {
                    newPos = curPos + 1;
                    return(curNameUse);
                }

                // Update and return;
                if (curNameUse != null)
                {
                    OperatorUse nextOp = null;
                    if (curPos + 1 <= subExps.Count() - 1)
                    {
                        nextOp = subExps.ElementAt(curPos + 1) as OperatorUse;
                    }
                    if ((nextOp != null && (nextOp.Text == "--" || nextOp.Text == "++")) || haveSeenUniop)
                    {
                        //updae if assigedVar is a field (Field contains static field? Currently, we do not distinguish static or not. )
                        VariableDeclaration assignedSelfField = FindFieldVarDecl(curNameUse);
                        if (assignedSelfField != null)
                        {
                            SetSelfFields.Add(assignedSelfField);
                        }
                        else
                        {
                            //update if assignedVar is a local variable
                            var lVarInfo = FindLocalVarInfo(curNameUse);
                            if (lVarInfo != null)
                            {
                                lVarInfo.IsFieldChange = IsFieldUpdate;
                                lVarInfo.IsModified    = true;
                            }
                            //update if assignedVar is a paramether
                            var pVarInfo = FindParaVarInfo(curNameUse);
                            if (pVarInfo != null)
                            {
                                pVarInfo.IsFieldChange = IsFieldUpdate;
                                pVarInfo.IsModified    = true;
                            }
                        }
                    }
                    if (nextOp != null && (nextOp.Text == "--" || nextOp.Text == "++"))
                    {
                        newPos = curPos + 2;
                    }
                    else
                    {
                        newPos = curPos + 1;
                    }
                    if (newPos < subExps.Count)
                    {
                        var newOp = subExps.ElementAt(newPos) as OperatorUse;
                        if (newOp != null && (newOp.Text == "=" || newOp.Text == "+=" || newOp.Text == "-="))
                        {
                            //analyse all the rest expressions
                            UpdateByExpToTheEnd(subExps, curPos);
                            newPos = subExps.Count;
                        }
                    }
                    return(curNameUse);
                }
            }
            newPos = subExps.Count();
            return(null);
        }