public void ObjectFieldTest2(Language lang)
        {
            // class Test {
            //    class Student { public int d;}
            //    class School { public int vb;}
            //    Student s = new Student();
            //    public void foo2() {
            //        School c = new School();
            //        int b;
            //        int i = 9;
            //        b = s.d - i/3;
            //        s.d = i + c.vb;
            //    }
            //}
            string xml         = @"<class pos:line=""1"" pos:column=""1"">class <name pos:line=""1"" pos:column=""7"">Test</name> <block pos:line=""1"" pos:column=""12"">{
        <class pos:line=""2"" pos:column=""9"">class <name pos:line=""2"" pos:column=""15"">Student</name> <block pos:line=""2"" pos:column=""23"">{ <decl_stmt><decl><type><specifier pos:line=""2"" pos:column=""25"">public</specifier> <name pos:line=""2"" pos:column=""32"">int</name></type> <name pos:line=""2"" pos:column=""36"">d</name></decl>;</decl_stmt>}</block></class>
        <class pos:line=""3"" pos:column=""9"">class <name pos:line=""3"" pos:column=""15"">School</name> <block pos:line=""3"" pos:column=""22"">{ <decl_stmt><decl><type><specifier pos:line=""3"" pos:column=""24"">public</specifier> <name pos:line=""3"" pos:column=""31"">int</name></type> <name pos:line=""3"" pos:column=""35"">vb</name></decl>;</decl_stmt>}</block></class>
         <decl_stmt><decl><type><name pos:line=""4"" pos:column=""10"">Student</name></type> <name pos:line=""4"" pos:column=""18"">s</name> <init pos:line=""4"" pos:column=""20"">= <expr><op:operator pos:line=""4"" pos:column=""22"">new</op:operator> <call><name pos:line=""4"" pos:column=""26"">Student</name><argument_list pos:line=""4"" pos:column=""33"">()</argument_list></call></expr></init></decl>;</decl_stmt>
        <function><type><specifier pos:line=""5"" pos:column=""9"">public</specifier> <name pos:line=""5"" pos:column=""16"">void</name></type> <name pos:line=""5"" pos:column=""21"">foo2</name><parameter_list pos:line=""5"" pos:column=""25"">()</parameter_list> <block pos:line=""5"" pos:column=""28"">{
            <decl_stmt><decl><type><name pos:line=""6"" pos:column=""13"">School</name></type> <name pos:line=""6"" pos:column=""20"">c</name> <init pos:line=""6"" pos:column=""22"">= <expr><op:operator pos:line=""6"" pos:column=""24"">new</op:operator> <call><name pos:line=""6"" pos:column=""28"">School</name><argument_list pos:line=""6"" pos:column=""34"">()</argument_list></call></expr></init></decl>;</decl_stmt>
            <decl_stmt><decl><type><name pos:line=""7"" pos:column=""13"">int</name></type> <name pos:line=""7"" pos:column=""17"">b</name></decl>;</decl_stmt>
            <decl_stmt><decl><type><name pos:line=""8"" pos:column=""13"">int</name></type> <name pos:line=""8"" pos:column=""17"">i</name> <init pos:line=""8"" pos:column=""19"">= <expr><lit:literal type=""number"" pos:line=""8"" pos:column=""21"">9</lit:literal></expr></init></decl>;</decl_stmt>
            <expr_stmt><expr><name pos:line=""9"" pos:column=""13"">b</name> <op:operator pos:line=""9"" pos:column=""15"">=</op:operator> <name><name pos:line=""9"" pos:column=""17"">s</name><op:operator pos:line=""9"" pos:column=""18"">.</op:operator><name pos:line=""9"" pos:column=""19"">d</name></name> <op:operator pos:line=""9"" pos:column=""21"">-</op:operator> <name pos:line=""9"" pos:column=""23"">i</name> <op:operator pos:line=""9"" pos:column=""25"">/</op:operator> <lit:literal type=""number"" pos:line=""9"" pos:column=""27"">3</lit:literal></expr>;</expr_stmt>
            <expr_stmt><expr><name><name pos:line=""10"" pos:column=""13"">s</name><op:operator pos:line=""10"" pos:column=""14"">.</op:operator><name pos:line=""10"" pos:column=""15"">d</name></name> <op:operator pos:line=""10"" pos:column=""17"">=</op:operator> <name pos:line=""10"" pos:column=""19"">i</name> <op:operator pos:line=""10"" pos:column=""21"">+</op:operator> <name><name pos:line=""10"" pos:column=""23"">c</name><op:operator pos:line=""10"" pos:column=""24"">.</op:operator><name pos:line=""10"" pos:column=""25"">vb</name></name></expr>;</expr_stmt>
        }</block></function>
    }</block></class>";
            var    xmlElement  = fileSetup[lang].GetFileUnitForXmlSnippet(xml, "test.code");
            var    globalScope = codeParsers[lang].ParseFileUnit(xmlElement);

            var method          = globalScope.GetDescendants <MethodDefinition>().FirstOrDefault();
            TestCaseAnalyzer ma = new TestCaseAnalyzer(method);
        }
        public void VAssignmentManagerTest1(Language lang)
        {
            //class Test {
            //    int d = 9;
            //    public void foo() {
            //        int c = 2;
            //        int b;
            //        b = c + 3;
            //    }
            //}
            string xml = @"<class pos:line=""1"" pos:column=""13"">class <name pos:line=""1"" pos:column=""19"">Test</name> <block pos:line=""1"" pos:column=""24"">{
                <decl_stmt><decl><type><name pos:line=""2"" pos:column=""17"">int</name></type> <name pos:line=""2"" pos:column=""21"">d</name> <init pos:line=""2"" pos:column=""23"">= <expr><lit:literal type=""number"" pos:line=""2"" pos:column=""25"">9</lit:literal></expr></init></decl>;</decl_stmt>
                <function><type><specifier pos:line=""3"" pos:column=""17"">public</specifier> <name pos:line=""3"" pos:column=""24"">void</name></type> <name pos:line=""3"" pos:column=""29"">foo</name><parameter_list pos:line=""3"" pos:column=""32"">()</parameter_list> <block pos:line=""3"" pos:column=""35"">{
                    <decl_stmt><decl><type><name pos:line=""4"" pos:column=""21"">int</name></type> <name pos:line=""4"" pos:column=""25"">c</name> <init pos:line=""4"" pos:column=""27"">= <expr><lit:literal type=""number"" pos:line=""4"" pos:column=""29"">2</lit:literal></expr></init></decl>;</decl_stmt>
                    <decl_stmt><decl><type><name pos:line=""5"" pos:column=""21"">int</name></type> <name pos:line=""5"" pos:column=""25"">b</name></decl>;</decl_stmt>
                    <expr_stmt><expr><name pos:line=""6"" pos:column=""21"">b</name> <op:operator pos:line=""6"" pos:column=""23"">=</op:operator> <name pos:line=""6"" pos:column=""25"">c</name> <op:operator pos:line=""6"" pos:column=""27"">+</op:operator> <lit:literal type=""number"" pos:line=""6"" pos:column=""29"">3</lit:literal></expr>;</expr_stmt>
                }</block></function>
            }</block></class>";

            var xmlElement  = fileSetup[lang].GetFileUnitForXmlSnippet(xml, "test.code");
            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);

            var method          = globalScope.GetDescendants <MethodDefinition>().FirstOrDefault();
            TestCaseAnalyzer ma = new TestCaseAnalyzer(method);
            var dict            = ma.VAssignmentManager.VarDictionary;

            Assert.AreEqual("b", dict.ElementAt(0).Key.Name);
            var listPath = dict.ElementAt(0).Value;

            Console.WriteLine(listPath.ElementAt(0).VariableStmts.ElementAt(1).Stmt);
            Assert.AreEqual(2, listPath.ElementAt(0).VariableStmts.Count);
        }
Пример #3
0
        public void analyzeProjectTestCases(string projectLocation, HashSet <TestCaseID> projectTestCases, HashSet <TestCaseSummary> projectTestSummary)
        {
            Console.WriteLine($"Analysing project {Path.GetFileName(projectLocation)}");

            using (var project = new DataProject <CompleteWorkingSet>(projectLocation, projectLocation, srcMLexeLocation))
            {
                project.Update();
                NamespaceDefinition globalNamespace;
                project.WorkingSet.TryObtainReadLock(5000, out globalNamespace);
                try
                {
                    // Step 1.   Build the call graph
                    Console.WriteLine("======  project summary ========= ");
                    Console.WriteLine("{0,10:N0} files", project.Data.GetFiles().Count());
                    Console.WriteLine("{0,10:N0} namespaces/packages", globalNamespace.GetDescendants <NamespaceDefinition>().Count());
                    Console.WriteLine("{0,10:N0} types", globalNamespace.GetDescendants <TypeDefinition>().Count());
                    Console.WriteLine("{0,10:N0} methods", globalNamespace.GetDescendants <MethodDefinition>().Count());
                    //Console.Read();
                    var methods = globalNamespace.GetDescendants <MethodDefinition>();

                    ////print test cases
                    //StreamWriter wt = File.AppendText(@"C:\testpropagation\clones\testCases.csv");
                    //wt.WriteLine($"nameSpaceName;className;methodName");
                    //wt.Flush();
                    //foreach(TestCaseID id in projectTestCases)
                    //{
                    //    wt.WriteLine($"{id.NamespaceName};{id.ClassName};{id.MethodName}");
                    //    wt.Flush();
                    //}
                    //wt.Close();
                    ////print methods
                    //StreamWriter w = File.AppendText(@"C:\testpropagation\clones\budMethods.csv");
                    //w.WriteLine($"nameSpaceName;className;methodName");
                    //w.Flush();

                    int i = 0;
                    // Step 2.   Testing
                    Console.WriteLine("\n======  analysing methods and identifying UUTs ========= ");
                    foreach (MethodDefinition method in methods)
                    {
                        //colect basic ID information
                        var declaringClass = method.GetAncestors <TypeDefinition>().FirstOrDefault();
                        var className      = "";
                        if (declaringClass != null)
                        {
                            className = declaringClass.Name;
                        }
                        var nameSpaceName = Utilities.GetNamespaceByMethod(method);
                        //w.WriteLine($"{nameSpaceName};{className};{method.Name}");
                        //w.Flush();
                        //continue;

                        if (Utilities.IsTestCase(method, projectTestCases))
                        {
                            //Console.WriteLine("Method Name : {0}", method.GetFullName());
                            SwumSummary swumSummary = new SwumSummary(method);
                            swumSummary.BasicSummary();
                            TestCaseAnalyzer analyzer = new TestCaseAnalyzer(method);
                            //analyzer.GetTestingObject();
                            string desc = swumSummary.Describe();
                            //writetext.WriteLine(method.Name + "  ,  " + desc);
                            //Console.WriteLine(nameSpaceName + "," + className + "," + method.Name + "Swum Description : " + desc);

                            TestCaseSummary tcSummary = new TestCaseSummary(desc, analyzer.ListAssertInfo, method);
                            tcSummary.NameSpaceName = nameSpaceName;
                            tcSummary.ClassName     = className;
                            tcSummary.MethodName    = method.Name;

                            projectTestSummary.Add(tcSummary);

                            Dictionary <Statement, List <Statement> > focalToAssert = Utilities.GetFocalToAssert(analyzer.ListAssertInfo);
                            if (focalToAssert != null && focalToAssert.Count > 0)
                            {
                                foreach (Statement statement in focalToAssert.Keys)
                                {
                                    Utilities.printMethodDetails(method, className, nameSpaceName, statement);
                                }
                            }
                            else
                            {
                                //print all methods in asserts
                                foreach (AssertSTInfo info in analyzer.ListAssertInfo)
                                {
                                    Utilities.printMethodDetails(method, className, nameSpaceName, info.AssertStatment);
                                }
                            }
                            //Console.WriteLine(tcSummary.GetBodyDescriptions());
                            //var stmts = method.GetDescendants<Statement>();
                            //Console.WriteLine($"===Printing local methods tested by {method.GetFullName()}===");
                            //HashSet<MethodDefinition> local = analyzer.InvokedLocalMethods;
                            //foreach(MethodDefinition definition in local)
                            //{
                            //    if (definition == null)
                            //    {
                            //        continue;
                            //    }
                            //    Console.WriteLine(definition.GetFullName());
                            //}
                            //Console.WriteLine($"===Printing external methods tested by {method.GetFullName()}===");
                            //HashSet<MethodDefinition> external = analyzer.InvokedExternalMethods;
                            //foreach (MethodDefinition definition in external)
                            //{
                            //    if (definition == null)
                            //    {
                            //        continue;
                            //    }
                            //    Console.WriteLine(definition.GetFullName());
                            //}

                            //delete me
                            //using (StreamWriter sw = File.AppendText(@"D:\d.csv"))
                            //{
                            //   var number = stmts.Count() + 2;
                            //   sw.WriteLine(method.Name + "," + number);
                            //    sw.Close();
                            //}
                            i++;
                        }
                    }
                    //w.Close();
                }
                finally
                {
                    project.WorkingSet.ReleaseReadLock();
                }
            }
        }
Пример #4
0
        public static void analyzeProjectTestCases(string projectLocation, HashSet <TestCaseID> projectTestCases, DataController dc, string srcMLexeLocation, string testsAndUUTFile, string classesAndMethodsFile, string projectGitName, string parent, bool isFork)
        {
            Console.WriteLine($"Analysing project {projectGitName}");//Path.GetFileName(projectLocation)

            using (var project = new DataProject <CompleteWorkingSet>(projectLocation, projectLocation, srcMLexeLocation))
            {
                project.Update();
                NamespaceDefinition globalNamespace;
                project.WorkingSet.TryObtainReadLock(5000, out globalNamespace);
                //StreamWriter classesWriter = File.AppendText(classesAndMethodsFile);
                //StreamWriter testsWriter = File.AppendText(testsAndUUTFile);
                try
                {
                    // Step 1.   Build the call graph
                    Console.WriteLine("======  project summary ========= ");
                    Console.WriteLine("{0,10:N0} files", project.Data.GetFiles().Count());
                    Console.WriteLine("{0,10:N0} namespaces/packages", globalNamespace.GetDescendants <NamespaceDefinition>().Count());
                    Console.WriteLine("{0,10:N0} types", globalNamespace.GetDescendants <TypeDefinition>().Count());
                    var methods = globalNamespace.GetDescendants <MethodDefinition>();
                    int total   = methods.Count();
                    Console.WriteLine("{0,10:N0} methods", total);
                    //Console.Read();

                    //=====THIS IS JUST TO PRINT OUT METHODS AND CLASSES
                    int i = 0;
                    //foreach(MethodDefinition method in methods)
                    //{
                    //    var declaringClass = method.GetAncestors<TypeDefinition>().FirstOrDefault();
                    //    var className = "";
                    //    if (declaringClass != null)
                    //    {
                    //        className = declaringClass.Name;
                    //    }
                    //    var nameSpaceName = Utilities.GetNamespaceByMethod(method);
                    //    printMethod(classesWriter, projectGitName, parent, isFork, nameSpaceName, className, method);
                    //    Console.WriteLine($"{++i}/{total}");
                    //}
                    //return;
                    //==END PRINITNG
                    // Step 2.   Testing
                    Console.WriteLine("\n======  analysing methods and identifying UUTs ========= ");
                    foreach (MethodDefinition method in methods)
                    {
                        try
                        {
                            //colect basic ID information
                            var declaringClass = method.GetAncestors <TypeDefinition>().FirstOrDefault();
                            var className      = "";
                            if (declaringClass != null)
                            {
                                className = declaringClass.Name;
                            }
                            var nameSpaceName = Utilities.GetNamespaceByMethod(method);

                            dc.ProjectClassesInsert(projectGitName, parent, isFork, nameSpaceName, className, method.Name, method.ToString(), method.PrimaryLocation.SourceFileName, method.PrimaryLocation.StartingLineNumber, method.PrimaryLocation.EndingLineNumber, method.PrimaryLocation.StartingColumnNumber, method.PrimaryLocation.EndingColumnNumber);

                            //printMethod(dc, projectGitName, parent, isFork, nameSpaceName, className, method);
                            //printMethod(classesWriter, projectGitName, parent, isFork, nameSpaceName, className, method);

                            if (Utilities.IsTestCase(method, projectTestCases))
                            {
                                try
                                {
                                    //Console.WriteLine("Test Method Name : {0}", method.GetFullName());
                                    //SwumSummary swumSummary = new SwumSummary(method);
                                    //swumSummary.BasicSummary();
                                    TestCaseAnalyzer analyzer = new TestCaseAnalyzer(method);
                                    //Console.WriteLine("Passed Analyzer for Test Method Name : {0}", method.GetFullName());
                                    ////analyzer.GetTestingObject();
                                    //string desc = swumSummary.Describe();
                                    ////writetext.WriteLine(method.Name + "  ,  " + desc);
                                    ////Console.WriteLine(nameSpaceName + "," + className + "," + method.Name + "Swum Description : " + desc);

                                    //TestCaseSummary tcSummary = new TestCaseSummary(desc, analyzer.ListAssertInfo, method);
                                    //tcSummary.NameSpaceName = nameSpaceName;
                                    //tcSummary.ClassName = className;
                                    //tcSummary.MethodName = method.Name;

                                    //projectTestSummary.Add(tcSummary);

                                    //======For the test case propagation, we are interested in reusing an entire test case, hence we must know all methods referenced in the test case. Thus we use theinvoked methods case

                                    //Dictionary<Statement, List<Statement>> focalToAssert = Utilities.GetFocalToAssert(analyzer.ListAssertInfo);
                                    //if (focalToAssert != null && focalToAssert.Count > 0)
                                    //{
                                    //    foreach (Statement statement in focalToAssert.Keys)
                                    //    {
                                    //        printMethodDetails(testsWriter, projectGitName, parent, isFork, method, className, nameSpaceName, statement);
                                    //    }
                                    //}
                                    //else if (analyzer.ListAssertInfo != null && analyzer.ListAssertInfo.Count > 0)
                                    //{
                                    //    //print all methods in asserts
                                    //    foreach (AssertSTInfo info in analyzer.ListAssertInfo)
                                    //    {
                                    //        printMethodDetails(testsWriter, projectGitName, parent, isFork, method, className, nameSpaceName, info.AssertStatment);
                                    //    }
                                    //}
                                    //else
                                    //{
                                    //all external methods referenced by the test method
                                    HashSet <MethodDefinition> external = analyzer.InvokedExternalMethods;
                                    foreach (MethodDefinition definition in external)
                                    {
                                        if (definition == null)
                                        {
                                            continue;
                                        }
                                        var mdCall = definition;


                                        var decClass = mdCall.GetAncestors <TypeDefinition>().FirstOrDefault();
                                        var mdClass  = "";
                                        if (decClass != null)
                                        {
                                            mdClass = decClass.Name;
                                        }
                                        var mdPackage = GetNamespaceByMethod(mdCall);
                                        dc.TestUUTPairInsert(projectGitName, parent, isFork, nameSpaceName, className, method.Name, method.ToString(), method.PrimaryLocation.SourceFileName, method.PrimaryLocation.StartingLineNumber, method.PrimaryLocation.EndingLineNumber, method.PrimaryLocation.StartingColumnNumber, method.PrimaryLocation.EndingColumnNumber, mdPackage, mdClass, mdCall.Name, mdCall.ToString(), mdCall.PrimaryLocation.SourceFileName, mdCall.PrimaryLocation.StartingLineNumber, mdCall.PrimaryLocation.EndingLineNumber, mdCall.PrimaryLocation.StartingColumnNumber, mdCall.PrimaryLocation.EndingColumnNumber);

                                        //printMethodDetails(dc, projectGitName, parent, isFork, method, className, nameSpaceName, definition);
                                        //printMethodDetails(testsWriter, projectGitName, parent, isFork, method, className, nameSpaceName, definition);
                                    }
                                    //}
                                }catch (StackOverflowException ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            }
                        }catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        i++;
                        Console.WriteLine($"{i}/{total}");
                    }
                }
                finally
                {
                    //if (classesWriter != null)
                    //{
                    //    classesWriter.Close();
                    //}
                    //if (testsWriter != null)
                    //{
                    //    testsWriter.Close();
                    //}
                    project.WorkingSet.ReleaseReadLock();
                }
            }
        }