public void SimpleIntegrationTest()
        {
            var n = new Network();

            n.ReadFile(@"C:\Users\seb\Dropbox\Shared (Mac)\smile-export.xdsl");

            var tStringBuilder = new CoReTypeName("LSystem/Text/StringBuilder");
            var rec            = new SmilePBNRecommender(tStringBuilder, n);
            var query          = new Query
            {
                type       = tStringBuilder,
                classCtx   = new CoReTypeName("LCC"),
                methodCtx  = new CoReMethodName("LMC.m()LV;"),
                definition =
                    DefinitionSites.CreateDefinitionByConstructor("LSystem/Text/StringBuilder.<init>()LSystem/Void;")
            };

            var call = CallSites.CreateReceiverCallSite("LSystem/Text/StringBuilder.ToString()LSystem/String;");

            PrintProposals("before", rec.Query(query));
            query.sites.Add(call);
            PrintProposals("added ToString", rec.Query(query));
            query.sites.Remove(call);
            PrintProposals("removed ToString", rec.Query(query));
        }
Exemplo n.º 2
0
        public void TypeOfThisUsages_SuperTypeIfAvailable()
        {
            SetupDefaultEnclosingMethod(
                InvokeStmt("this", Method(Fix.Void, DefaultClassContext, "M")));

            Context.TypeShape.TypeHierarchy = new TypeHierarchy
            {
                Element = DefaultClassContext,
                Extends = new TypeHierarchy
                {
                    Element = Type("TSuper")
                }
            };

            AssertQueriesWithoutSettingContexts(
                new Query
            {
                type       = Type("TSuper").ToCoReName(),
                classCtx   = Type("TSuper").ToCoReName(),
                methodCtx  = DefaultMethodContext.ToCoReName(),
                definition = DefinitionSites.CreateDefinitionByThis(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(
                        Method(Fix.Void, DefaultClassContext, "M"))
                }
            });
        }
Exemplo n.º 3
0
        public void AmbiguousCallsOnUndeclaredVariable()
        {
            SetupDefaultEnclosingMethod(
                InvokeStmt("a", Method(Fix.Void, Type("A"), "MA")),
                InvokeStmt("a", Method(Fix.Void, Type("B"), "MB")));

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("A").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(
                        Method(Fix.Void, Type("A"), "MA"))
                }
            },
                new Query
            {
                type  = Type("B").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(
                        Method(Fix.Void, Type("B"), "MB"))
                }
            });
        }
        public void ForLoopCondition()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("a", Type("A")),
                new ForLoop
            {
                Condition = new LoopHeaderBlockExpression
                {
                    Body =
                    {
                        new ExpressionStatement
                        {
                            Expression = new InvocationExpression
                            {
                                MethodName = Method(Type("R"), Type("A"), "methodA"),
                                Reference  = VarRef("a")
                            }
                        }
                    }
                }
            }
                );

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("A").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(Method(Type("R"), Type("A"), "methodA"))
                }
            });
        }
        public void TryBlockCatchBlockParameter()
        {
            SetupDefaultEnclosingMethod(
                new TryBlock
            {
                CatchBlocks =
                {
                    new CatchBlock
                    {
                        Parameter = Parameter(Type("T"), "p"),
                        Body      =
                        {
                            InvokeStmt("p", Method(Fix.Void, Type("T"), "M"))
                        }
                    }
                }
            }
                );

            AssertQueriesInDefault(
                new Query
            {
                type       = Type("T").ToCoReName(),
                definition = DefinitionSites.CreateUnknownDefinitionSite(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method(Fix.Void, Type("T"), "M"))
                }
            });
        }
        public void TryBlockCatchBlockBody()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("a", Type("A")),
                new TryBlock
            {
                CatchBlocks =
                {
                    new CatchBlock
                    {
                        Body =
                        {
                            InvokeStmt("a", Method(Fix.Void, Type("A"), "M"))
                        }
                    }
                }
            }
                );

            AssertQueriesInDefault(
                new Query
            {
                type       = Type("A").ToCoReName(),
                definition = DefinitionSites.CreateUnknownDefinitionSite(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method(Fix.Void, Type("A"), "M"))
                }
            });
        }
Exemplo n.º 7
0
        public void RegisterCallsite(string id, IMethodName callsite)
        {
            // TODO @seb fix analysis and then remove this check!
            if (Equals("", callsite.Name))
            {
                return;
            }

            if (NameResolver.IsExisting(id))
            {
                var q = NameResolver.Find(id);
                q.sites.Add(CallSites.CreateReceiverCallSite(callsite));
            }
            else
            {
                var type = callsite.DeclaringType.ToCoReName();
                if (NameResolver.IsExisting(type))
                {
                    var q = NameResolver.Find(type);
                    q.sites.Add(CallSites.CreateReceiverCallSite(callsite));
                }
                else
                {
                    var q = NewQueryFor(type, DefinitionSites.CreateUnknownDefinitionSite());
                    q.sites.Add(CallSites.CreateReceiverCallSite(callsite));

                    NameResolver.Register(type, q);
                }
            }
        }
        public void TriggerOnThis_HasBaseClass()
        {
            var context = NewContextWithDefaults(
                new TypeHierarchy
            {
                Element = DefaultClassContext,
                Extends = new TypeHierarchy
                {
                    Element = Type("TSuper")
                }
            },
                new MethodHierarchy
            {
                Element = DefaultMethodContext
            },
                InvokeStmt("this", Method("R", "T", "M2")),
                Completion("this")
                );

            var actual   = _sut.Extract(context);
            var expected = new Query
            {
                type       = Type("TSuper").ToCoReName(),
                classCtx   = Type("TSuper").ToCoReName(),
                methodCtx  = DefaultMethodContext.ToCoReName(),
                definition = DefinitionSites.CreateDefinitionByThis(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method("R", "T", "M2"))
                }
            };

            Assert.AreEqual(expected, actual);
        }
        public void UsingBlock()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("b", Type("B")),
                new UsingBlock
            {
                Body =
                {
                    new ExpressionStatement
                    {
                        Expression = new InvocationExpression
                        {
                            MethodName = Method(Type("R"), Type("B"), "methodB"),
                            Reference  = VarRef("b")
                        }
                    }
                }
            }
                );

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("B").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(Method(Type("R"), Type("B"), "methodB"))
                }
            });
        }
Exemplo n.º 10
0
        public void HappyPath()
        {
            var context = NewContextWithDefaults(
                new TypeHierarchy
            {
                Element = DefaultClassContext
            },
                new MethodHierarchy
            {
                Element = DefaultMethodContext
            },
                VarDecl("A", "a"),
                InvokeStmt("a", Method("R", "A", "M")),
                Completion("a")
                );

            var actual   = _sut.Extract(context);
            var expected = new Query
            {
                type       = Type("A").ToCoReName(),
                classCtx   = DefaultClassContext.ToCoReName(),
                methodCtx  = DefaultMethodContext.ToCoReName(),
                definition = DefinitionSites.CreateUnknownDefinitionSite(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method("R", "A", "M"))
                }
            };

            Assert.AreEqual(expected, actual);
        }
        public void LabelledStatementTest()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("a", Type("A")),
                new LabelledStatement
            {
                Statement = new ExpressionStatement
                {
                    Expression = new InvocationExpression
                    {
                        MethodName = Method(Type("R"), Type("A"), "methodA"),
                        Reference  = VarRef("a")
                    }
                }
            });

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("A").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(Method(Type("R"), Type("A"), "methodA"))
                }
            });
        }
        public void AssignmentTest()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("i", Type("I")),
                Assign("i", Invoke("this", Method(Type("I"), DefaultClassContext, "M"))),
                InvokeStmt("i", SomeMethodOnType("I")));


            AssertQueriesInDefault(
                new Query
            {
                type       = DefaultClassContext.ToCoReName(),
                definition = DefinitionSites.CreateDefinitionByThis(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method(Type("I"), DefaultClassContext, "M"))
                }
            },
                new Query
            {
                type       = Type("I").ToCoReName(),
                definition = DefinitionSites.CreateDefinitionByReturn(Method(Type("I"), DefaultClassContext, "M")),
                sites      =
                {
                    SomeCallSiteOnType("I")
                }
            }
                );
        }
Exemplo n.º 13
0
        public void CallSite()
        {
            var callSite = CallSites.CreateReceiverCallSite("LType.M2()LVoid;");

            var          actual   = SmilePBNRecommenderConstants.NewReceiverCallSite(callSite);
            const string expected = "C_LType.M2()LVoid;";

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 14
0
        public void HasReceiverCallSites()
        {
            var query = new Query();

            Assert.False(query.HasReceiverCallSites);
            query.sites.Add(CallSites.CreateParameterCallSite("LT.m()LU;", 1));
            Assert.False(query.HasReceiverCallSites);
            query.sites.Add(CallSites.CreateReceiverCallSite("LT.m()LU;"));
            Assert.True(query.HasReceiverCallSites);
        }
        public void ShouldRemoveStoredCallsFromPreviousQueries()
        {
            // place a query that includes a call...
            var query = Fix.CreateDefaultQuery();

            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Execute()LVoid;"));
            _uut.Query(query);

            // ...then fall back to a default case
            ShouldProduceOrderedProposals();
        }
Exemplo n.º 16
0
        public void ReceiverCallSiteForStringIsCorrectInitialized()
        {
            var actual   = CallSites.CreateReceiverCallSite("LType.method(LArgument;)LReturn;");
            var expected = new CallSite
            {
                kind   = CallSiteKind.RECEIVER,
                method = new CoReMethodName("LType.method(LArgument;)LReturn;")
            };

            Assert.AreEqual(expected, actual);
        }
        public void ShouldSetEvidenceOnCallSite()
        {
            var query = Fix.CreateDefaultQuery();

            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Init()LVoid;"));

            _uut.Query(query);

            var actual = _network.GetEvidenceId(SmilePBNRecommender.ConvertToLegalSmileName("C_LType.Init()LVoid;"));

            Assert.AreEqual(Constants.StateTrue, actual);
        }
        public void ShouldNotProduceAnyProposalsIfAllMethodsAreAlreadyCalled()
        {
            var query = Fix.CreateDefaultQuery();

            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Init()LVoid;"));
            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Execute()LVoid;"));
            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Finish()LVoid;"));

            var expected = new CoReProposal[] { };
            var actual   = _uut.Query(query);

            CollectionAssert.AreEquivalent(expected, actual);
        }
        public void ShouldNotSetEvidencesOnNotMatchingQuery()
        {
            var query = Fix.CreateDefaultQuery();

            query.definition = DefinitionSites.CreateDefinitionByReturn("LStrangeType.M()LType;");
            query.sites.Add(CallSites.CreateReceiverCallSite("LStrangeType.M()LType;"));

            _uut.Query(query);

            var actual = _network.GetAllNodes().All(id => !_network.IsEvidence(id));

            Assert.IsTrue(actual);
        }
Exemplo n.º 20
0
 private static Query CreateQuery(ITypeName classCtx, IMethodName methodCtx)
 {
     return(new Query
     {
         type = Fix.Int.ToCoReName(),
         classCtx = classCtx.ToCoReName(),
         methodCtx = methodCtx.ToCoReName(),
         definition = DefinitionSites.CreateDefinitionByConstant(),
         sites =
         {
             CallSites.CreateReceiverCallSite(Fix.Int_GetHashCode)
         }
     });
 }
Exemplo n.º 21
0
        public void ReceiverCallSiteForMethodNameIsCorrectInitialized()
        {
            var actual =
                CallSites.CreateReceiverCallSite(
                    Names.Method("[LReturn,LAssembly] [LType,LAssembly].Method([LArgument,LAssembly] paramName)"));
            var expected = new CallSite
            {
                kind   = CallSiteKind.RECEIVER,
                method =
                    Names.Method("[LReturn,LAssembly] [LType,LAssembly].Method([LArgument,LAssembly] paramName)")
                    .ToCoReName()
            };

            Assert.AreEqual(expected, actual);
        }
        public void ShouldProduceAdaptedPropertiesIfCallSiteIsSet()
        {
            var query = Fix.CreateDefaultQuery();

            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Init()LVoid;"));

            var expected = new[]
            {
                new CoReProposal(new CoReMethodName("LType.Execute()LVoid;"), 0.682),
                new CoReProposal(new CoReMethodName("LType.Finish()LVoid;"), 0.164)
            };

            var actual = _uut.Query(query);

            CollectionAssert.AreEquivalent(expected, actual);
        }
Exemplo n.º 23
0
        private Query CreateQueryWithDefaults(ITypeName type, DefinitionSite def, params IMethodName[] methods)
        {
            var query = new Query
            {
                type       = type.ToCoReName(),
                classCtx   = SomeType.ToCoReName(),
                methodCtx  = SomeMethodName.ToCoReName(),
                definition = def
            };

            foreach (IMethodName method in methods)
            {
                query.sites.Add(CallSites.CreateReceiverCallSite(method));
            }
            return(query);
        }
Exemplo n.º 24
0
        private Query Usage(ITypeName type, DefinitionSite defSite, params string[] calls)
        {
            var q = new Query
            {
                type       = type.ToCoReName(),
                definition = defSite
            };

            foreach (var shortCall in calls)
            {
                var mStr = string.Format("[{0}] [{1}].{2}()", "System.Void", "A,P", shortCall);
                var m    = Names.Method(mStr);
                var call = CallSites.CreateReceiverCallSite(m);
                q.sites.Add(call);
            }
            return(q);
        }
        public void ShouldProduceSomeProposalsIfSomeMethodIsAlreadyCalled()
        {
            var query = Fix.CreateDefaultQuery();

            query.sites.Add(CallSites.CreateReceiverCallSite("LType.Init()LVoid;"));

            var expected = new[]
            {
                new CoReMethodName("LType.Execute()LVoid;"),
                new CoReMethodName("LType.Finish()LVoid;")
            };

            var proposals = _uut.Query(query);
            var actual    = proposals.Select(p => p.Name);

            CollectionAssert.AreEquivalent(expected, actual);
        }
Exemplo n.º 26
0
        public void CallOnSubtypeIsRegisteredForSuperType_EvenWhenTypeExists()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("a", Type("A")),
                VarDecl("b", Type("B")),
                InvokeStmt("a", Method(Fix.Void, Type("B"), "M")));

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("A").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(
                        Method(Fix.Void, Type("B"), "M"))
                }
            });
        }
        public void ForEachLoopDeclaration()
        {
            SetupDefaultEnclosingMethod(
                new ForEachLoop
            {
                Declaration = VarDecl("b", Type("B")),
                Body        =
                {
                    InvokeStmt("b", Method(Fix.Void, Type("B"), "M"))
                }
            }
                );

            AssertQueriesInDefault(
                new Query
            {
                type  = Type("B").ToCoReName(),
                sites =
                {
                    CallSites.CreateReceiverCallSite(Method(Fix.Void, Type("B"), "M"))
                }
            });
        }
Exemplo n.º 28
0
        public void ContextRewrite()
        {
            // exemplary test, this is extensively tested for usage export

            var context = NewContextWithDefaults(
                new TypeHierarchy
            {
                Element = DefaultClassContext,
                Extends = new TypeHierarchy
                {
                    Element = Type("TSuper")
                }
            },
                new MethodHierarchy
            {
                Element = DefaultMethodContext
            },
                VarDecl("A", "a"),
                InvokeStmt("a", Method("R", "A", "M")),
                Completion("a")
                );

            var actual   = _sut.Extract(context);
            var expected = new Query
            {
                type       = Type("A").ToCoReName(),
                classCtx   = Type("TSuper").ToCoReName(),
                methodCtx  = DefaultMethodContext.ToCoReName(),
                definition = DefinitionSites.CreateUnknownDefinitionSite(),
                sites      =
                {
                    CallSites.CreateReceiverCallSite(Method("R", "A", "M"))
                }
            };

            Assert.AreEqual(expected, actual);
        }
        public void LambdaExpressionTest()
        {
            SetupDefaultEnclosingMethod(
                VarDecl("a", Fix.Action),
                Assign(
                    "a",
                    new LambdaExpression
            {
                Name = Names.Lambda("[{0}] ()", Fix.Void),
                Body =
                {
                    VarDecl("v", Type("V")),
                    new ExpressionStatement
                    {
                        Expression = new InvocationExpression
                        {
                            MethodName = Method(Type("R"),Type("V"), "methodV"),
                            Reference  = VarRef("v")
                        }
                    }
                }
            }));


            AssertQueriesWithoutSettingContexts(
                new Query
            {
                type      = Type("V").ToCoReName(),
                classCtx  = Type("TDecl$Lambda").ToCoReName(),
                methodCtx = Names.Method("[{0}] [{1}].M$Lambda()", Type("A"), DefaultClassContext).ToCoReName(),
                sites     =
                {
                    CallSites.CreateReceiverCallSite(Method(Type("R"), Type("V"), "methodV"))
                }
            });
        }
 protected static CallSite SomeCallSiteOnType(string typeName)
 {
     return(CallSites.CreateReceiverCallSite(Method(Fix.Void, Type(typeName), "M")));
 }