コード例 #1
0
        public void TestRoundtrip()
        {
            var archive = new SrcMLArchive("DataRepositoryTests");

            archive.AddOrUpdateFile(@"..\..\TestInputs\A.h");
            archive.AddOrUpdateFile(@"..\..\TestInputs\A.cpp");
            var data = new DataRepository(archive);

            data.InitializeData();
            data.Save(@"DataRepositoryTests\saved.dar");

            var newData = new DataRepository(@"DataRepositoryTests\saved.dar");

            newData.InitializeData();
            IScope globalScope, newGlobalScope;

            Assert.That(data.TryLockGlobalScope(Timeout.Infinite, out globalScope));
            Assert.That(newData.TryLockGlobalScope(Timeout.Infinite, out newGlobalScope));

            try {
                Assert.IsTrue(TestHelper.ScopesAreEqual(globalScope, newGlobalScope));
            } finally {
                data.ReleaseGlobalScopeLock();
                newData.ReleaseGlobalScopeLock();
            }
        }
コード例 #2
0
        public void TestRoundtrip() {
            var archive = new SrcMLArchive("DataRepositoryTests");
            archive.AddOrUpdateFile(@"..\..\TestInputs\A.h");
            archive.AddOrUpdateFile(@"..\..\TestInputs\A.cpp");
            var data = new DataRepository(archive);
            data.Save(@"DataRepositoryTests\saved.dar");

            var newData = new DataRepository(archive, @"DataRepositoryTests\saved.dar");

            Assert.IsTrue(TestHelper.ScopesAreEqual(data.GlobalScope, newData.GlobalScope));
        }
コード例 #3
0
        public void TestQueryDuringUpdate()
        {
            var sourceFolder = "TestQueryDuringUpdate";
            var dataFolder   = "TestQueryDuringUpdate_Data";

            Directory.CreateDirectory(sourceFolder);
            string fooSourcePath = Path.Combine(sourceFolder, "foo.cpp");
            string barSourcePath = Path.Combine(sourceFolder, "bar.cpp");

            var fooRevisions = new string[] { "void foo() { }", "void foo() { bar(); }" };

            File.WriteAllText(fooSourcePath, fooRevisions[1]);
            File.WriteAllText(barSourcePath, "void bar() { }");

            int iterations = 1000;

            using (var archive = new SrcMLArchive(dataFolder)) {
                using (var data = new DataRepository(archive)) {
                    data.InitializeData();
                    archive.AddOrUpdateFile(Path.Combine(sourceFolder, "foo.cpp"));
                    archive.AddOrUpdateFile(Path.Combine(sourceFolder, "bar.cpp"));

                    var developer = new Task(() => {
                        for (int i = 0; i < iterations; i++)
                        {
                            File.WriteAllText(fooSourcePath, fooRevisions[i % 2]);
                            archive.AddOrUpdateFile(fooSourcePath);
                        }
                    });

                    developer.Start();
                    Assert.DoesNotThrow(() => {
                        for (int i = 0; i < iterations; i++)
                        {
                            var foo = GetMethodWithName(data, 500, "foo");
                            Thread.Sleep(1);
                            var bar = GetMethodWithName(data, 500, "bar");

                            foo.ContainsCallTo(bar);
                            if (i % 10 == 0 && i > 0)
                            {
                                Console.WriteLine("Finished {0} iterations", i);
                            }
                        }
                    });

                    developer.Wait();
                }
            }
        }
コード例 #4
0
        public void TestFindMethodCalls_Nested()
        {
            using (var sa = new SrcMLArchive("DataRepositoryTests")) {
                sa.AddOrUpdateFile(@"..\..\TestInputs\nested_method_calls.cpp");

                using (var da = new DataRepository(sa)) {
                    da.InitializeData();
                    IScope globalScope;
                    Assert.That(da.TryLockGlobalScope(Timeout.Infinite, out globalScope));
                    try {
                        var foo      = globalScope.ChildScopes.OfType <IMethodDefinition>().First(md => md.Name == "Foo");
                        var expected = new[]
                        {
                            foo.MethodCalls.First(mc => mc.Name == "ToString"),
                            foo.MethodCalls.First(mc => mc.Name == "SomeMethodCall"),
                            foo.MethodCalls.First(mc => mc.Name == "printf")
                        };

                        var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\nested_method_calls.cpp", 4, 41));
                        Assert.IsNotNull(actual);
                        Assert.AreEqual(expected.Length, actual.Count);
                        for (int i = 0; i < expected.Length; i++)
                        {
                            Assert.AreEqual(expected[i], actual[i]);
                        }
                    } finally {
                        da.ReleaseGlobalScopeLock();
                    }
                }
            }
        }
コード例 #5
0
        public void TestGetXElement()
        {
            var archive    = new SrcMLArchive("SrcMLLocationTest", false, new SrcMLGenerator("SrcML"));
            var sourcePath = Path.GetFullPath(@"..\..\TestInputs\class_test.h");

            archive.AddOrUpdateFile(sourcePath);

            var unit = archive.GetXElementForSourceFile(sourcePath);

            Assert.IsNotNull(unit);
            var classElement = unit.Descendants(SRC.Class).FirstOrDefault();

            Assert.IsNotNull(classElement);

            var parser         = new CPlusPlusCodeParser();
            var globalScope    = parser.ParseFileUnit(unit);
            var typeDefinition = globalScope.ChildStatements.OfType <TypeDefinition>().FirstOrDefault();

            Assert.IsNotNull(typeDefinition);

            var element = typeDefinition.PrimaryLocation.GetXElement(archive);

            Assert.IsNotNull(element);
            Assert.AreEqual(classElement.GetSrcLineNumber(), element.GetSrcLineNumber());
            Assert.AreEqual(classElement.GetSrcLinePosition(), element.GetSrcLinePosition());
            Assert.AreEqual(classElement.GetXPath(), element.GetXPath());
        }
コード例 #6
0
        public void TestRoundtrip_Self()
        {
            var archive = new SrcMLArchive("DataRepositoryTests");

            foreach (var csFile in Directory.GetFiles(@"..\..\ABB.SrcML", "*.cs", SearchOption.AllDirectories))
            {
                archive.AddOrUpdateFile(csFile);
            }
            var data = new DataRepository(archive);

            data.InitializeData();
            data.Save(@"DataRepositoryTests\saved.dar");

            var newData = new DataRepository(archive, @"DataRepositoryTests\saved.dar");

            newData.InitializeData();

            IScope globalScope, newGlobalScope;

            Assert.That(data.TryLockGlobalScope(Timeout.Infinite, out globalScope));
            Assert.That(newData.TryLockGlobalScope(Timeout.Infinite, out newGlobalScope));

            try {
                Assert.IsTrue(TestHelper.ScopesAreEqual(globalScope, newGlobalScope));
            } finally {
                data.ReleaseGlobalScopeLock();
                newData.ReleaseGlobalScopeLock();
            }
        }
コード例 #7
0
        public void TestDontUseExistingSrcML()
        {
            //convert the test files and place in the xml directory
            ManualResetEvent resetEvent = new ManualResetEvent(false);
            var archive = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation)));

            archive.FileChanged += (o, e) => { resetEvent.Set(); };

            string[] sourceFiles = new[] { @"..\..\TestInputs\foo.c", @"..\..\TestInputs\baz.cpp", @"..\..\TestInputs\function_def.cpp" };

            foreach (var sourceFile in sourceFiles)
            {
                archive.AddOrUpdateFile(sourceFile);
                Assert.That(resetEvent.WaitOne(300), "Timed out waiting for " + sourceFile);
            }
            foreach (var sourceFile in sourceFiles)
            {
                Assert.That(archive.ContainsFile(sourceFile), sourceFile + " should be in the archive!");
            }
            archive.Dispose();

            //make new archive, and ignore existing srcml files in xml directory
            archive = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(TestConstants.SrcmlPath));
            foreach (var sourceFile in sourceFiles)
            {
                Assert.IsFalse(archive.ContainsFile(sourceFile));
            }
            archive.Dispose();
        }
コード例 #8
0
 public void TestFindMethodCalls_Simple() {
     using(var sa = new SrcMLArchive("DataRepositoryTests")) {
         sa.AddOrUpdateFile(@"..\..\TestInputs\function_def.cpp");
         var da = new DataRepository(sa);
         var expected = da.GlobalScope.ChildScopes.OfType<MethodDefinition>().First(md => md.Name == "main").MethodCalls.First();
         var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\function_def.cpp", 12, 20));
         Assert.IsNotNull(actual);
         Assert.AreEqual(1, actual.Count);
         Assert.AreEqual(expected, actual[0]);
     }
 }
コード例 #9
0
        public void TestEmptyArchive()
        {
            var archive = new SrcMLArchive(ArchiveDirectory);

            Assert.That(archive.IsEmpty);
            var foo_c = Path.Combine(SourceDirectory, "foo.c");

            File.WriteAllText(foo_c, String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine));
            archive.AddOrUpdateFile(foo_c);
            Assert.That(archive.IsEmpty, Is.False);
        }
コード例 #10
0
        public void TestEmptyArchive()
        {
            var archive = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation)));

            Assert.That(archive.IsEmpty);
            var foo_c = Path.Combine(SourceDirectory, "foo.c");

            File.WriteAllText(foo_c, String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine));
            archive.AddOrUpdateFile(foo_c);
            Assert.That(archive.IsEmpty, Is.False);
        }
コード例 #11
0
        public void TestRoundtrip_Self() {
            var archive = new SrcMLArchive("DataRepositoryTests");
            foreach(var csFile in Directory.GetFiles(@"..\..\ABB.SrcML", "*.cs", SearchOption.AllDirectories)) {
                archive.AddOrUpdateFile(csFile);
            }
            var data = new DataRepository(archive);
            data.Save(@"DataRepositoryTests\saved.dar");

            var newData = new DataRepository(archive, @"DataRepositoryTests\saved.dar");

            Assert.IsTrue(TestHelper.ScopesAreEqual(data.GlobalScope, newData.GlobalScope));
        }
コード例 #12
0
        public void TestFindScopeForAdjacentMethods()
        {
            using (var sa = new SrcMLArchive("DataRepositoryTests")) {
                sa.AddOrUpdateFile(@"..\..\TestInputs\adjacent_methods.cpp");

                using (var da = new DataRepository(sa)) {
                    da.InitializeData();
                    IScope globalScope;
                    Assert.That(da.TryLockGlobalScope(Timeout.Infinite, out globalScope));
                    try {
                        var mainMethod = globalScope.GetDescendantScopes <IMethodDefinition>().FirstOrDefault();
                        var fooMethod  = globalScope.GetDescendantScopes <IMethodDefinition>().LastOrDefault();

                        Assert.IsNotNull(mainMethod, "could not find main()");
                        Assert.IsNotNull(fooMethod, "could not find foo()");
                        Assert.AreEqual("main", mainMethod.Name);
                        Assert.AreEqual("Foo", fooMethod.Name);

                        var fileName = @"TestInputs\adjacent_methods.cpp";

                        var startOfMain    = new SourceLocation(fileName, 1, 1);
                        var locationInMain = new SourceLocation(fileName, 1, 11);

                        Assert.That(mainMethod.PrimaryLocation.Contains(startOfMain));
                        Assert.That(mainMethod.PrimaryLocation.Contains(locationInMain));

                        Assert.AreEqual(mainMethod, da.FindScope(startOfMain));
                        Assert.AreEqual(mainMethod, da.FindScope(locationInMain));

                        var startOfFoo    = new SourceLocation(fileName, 3, 1);
                        var locationInFoo = new SourceLocation(fileName, 3, 11);

                        Assert.That(fooMethod.PrimaryLocation.Contains(startOfFoo));
                        Assert.That(fooMethod.PrimaryLocation.Contains(locationInFoo));

                        Assert.AreEqual(fooMethod, da.FindScope(startOfFoo));
                        Assert.AreEqual(fooMethod, da.FindScope(locationInFoo));

                        var lineBetweenMethods = new SourceLocation(fileName, 2, 1);
                        Assert.That(mainMethod.PrimaryLocation.Contains(lineBetweenMethods));
                        Assert.IsFalse(fooMethod.PrimaryLocation.Contains(lineBetweenMethods));
                        Assert.AreEqual(mainMethod, da.FindScope(lineBetweenMethods));
                    } finally {
                        da.ReleaseGlobalScopeLock();
                    }
                }
            }
        }
コード例 #13
0
 public void TestFindMethodCalls_Nested() {
     using(var sa = new SrcMLArchive("DataRepositoryTests")) {
         sa.AddOrUpdateFile(@"..\..\TestInputs\nested_method_calls.cpp");
         var da = new DataRepository(sa);
         var foo = da.GlobalScope.ChildScopes.OfType<MethodDefinition>().First(md => md.Name == "Foo");
         var expected = new[]
                        {
                            foo.MethodCalls.First(mc => mc.Name == "ToString"),
                            foo.MethodCalls.First(mc => mc.Name == "SomeMethodCall"),
                            foo.MethodCalls.First(mc => mc.Name == "printf")
                        };
         
         var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\nested_method_calls.cpp", 4, 41));
         Assert.IsNotNull(actual);
         Assert.AreEqual(expected.Length, actual.Count);
         for(int i = 0; i < expected.Length; i++) {
             Assert.AreEqual(expected[i], actual[i]);
         }
     }
 }
コード例 #14
0
        public void TestGetXElement() {
            var archive = new SrcMLArchive("SrcMLLocationTest", false, new SrcMLGenerator("SrcML"));
            var sourcePath = Path.GetFullPath(@"..\..\TestInputs\class_test.h");
            archive.AddOrUpdateFile(sourcePath);

            var unit = archive.GetXElementForSourceFile(sourcePath);
            Assert.IsNotNull(unit);
            var classElement = unit.Descendants(SRC.Class).FirstOrDefault();
            Assert.IsNotNull(classElement);

            var parser = new CPlusPlusCodeParser();
            var globalScope = parser.ParseFileUnit(unit);
            var typeDefinition = globalScope.ChildStatements.OfType<TypeDefinition>().FirstOrDefault();
            Assert.IsNotNull(typeDefinition);

            var element = typeDefinition.PrimaryLocation.GetXElement(archive);
            Assert.IsNotNull(element);
            Assert.AreEqual(classElement.GetSrcLineNumber(), element.GetSrcLineNumber());
            Assert.AreEqual(classElement.GetSrcLinePosition(), element.GetSrcLinePosition());
            Assert.AreEqual(classElement.GetXPath(), element.GetXPath());
        }
コード例 #15
0
        public void TestFindMethodCalls_Simple()
        {
            using (var sa = new SrcMLArchive("DataRepositoryTests")) {
                sa.AddOrUpdateFile(@"..\..\TestInputs\function_def.cpp");

                using (var da = new DataRepository(sa)) {
                    da.InitializeData();
                    IScope globalScope;
                    Assert.That(da.TryLockGlobalScope(Timeout.Infinite, out globalScope));
                    try {
                        var expected = globalScope.ChildScopes.OfType <IMethodDefinition>().First(md => md.Name == "main").MethodCalls.First();
                        var actual   = da.FindMethodCalls(new SourceLocation(@"TestInputs\function_def.cpp", 12, 20));
                        Assert.IsNotNull(actual);
                        Assert.AreEqual(1, actual.Count);
                        Assert.AreEqual(expected, actual[0]);
                    } finally {
                        da.ReleaseGlobalScopeLock();
                    }
                }
            }
        }
コード例 #16
0
        public void TestFindScopeForAdjacentMethods() {
            using(var sa = new SrcMLArchive("DataRepositoryTests")) {
                sa.AddOrUpdateFile(@"..\..\TestInputs\adjacent_methods.cpp");
                var da = new DataRepository(sa);
                
                var mainMethod = da.GlobalScope.GetDescendantScopes<MethodDefinition>().FirstOrDefault();
                var fooMethod = da.GlobalScope.GetDescendantScopes<MethodDefinition>().LastOrDefault();

                Assert.IsNotNull(mainMethod, "could not find main()");
                Assert.IsNotNull(fooMethod, "could not find foo()");
                Assert.AreEqual("main", mainMethod.Name);
                Assert.AreEqual("Foo", fooMethod.Name);

                var fileName = @"TestInputs\adjacent_methods.cpp";

                var startOfMain = new SourceLocation(fileName, 1, 1);
                var locationInMain = new SourceLocation(fileName, 1, 11);

                Assert.That(mainMethod.PrimaryLocation.Contains(startOfMain));
                Assert.That(mainMethod.PrimaryLocation.Contains(locationInMain));

                Assert.AreEqual(mainMethod, da.FindScope(startOfMain));
                Assert.AreEqual(mainMethod, da.FindScope(locationInMain));

                var startOfFoo = new SourceLocation(fileName, 3, 1);
                var locationInFoo = new SourceLocation(fileName, 3, 11);

                Assert.That(fooMethod.PrimaryLocation.Contains(startOfFoo));
                Assert.That(fooMethod.PrimaryLocation.Contains(locationInFoo));

                Assert.AreEqual(fooMethod, da.FindScope(startOfFoo));
                Assert.AreEqual(fooMethod, da.FindScope(locationInFoo));

                var lineBetweenMethods = new SourceLocation(fileName, 2, 1);
                Assert.That(mainMethod.PrimaryLocation.Contains(lineBetweenMethods));
                Assert.IsFalse(fooMethod.PrimaryLocation.Contains(lineBetweenMethods));
                Assert.AreEqual(mainMethod, da.FindScope(lineBetweenMethods));
            }
        }
コード例 #17
0
        public void GenerateXmlForDirectoryTest()
        {
            ManualResetEvent resetEvent = new ManualResetEvent(false);

            var           archive           = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation)));
            FileEventType expectedEventType = FileEventType.FileAdded;
            FileEventType actualEventType   = FileEventType.FileChanged;

            archive.FileChanged += (sender, e) => {
                actualEventType = e.EventType;
                bool shouldHaveSrcML = (e.EventType != FileEventType.FileDeleted);
                Assert.AreEqual(shouldHaveSrcML, e.HasSrcML);
                resetEvent.Set();
            };

            Dictionary <string, string> sourceFiles = new Dictionary <string, string>()
            {
                { Path.Combine(SourceDirectory, "foo.c"), String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "bar.c"), String.Format(@"int bar() {{{0}    printf(""goodbye, world!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "foo1.c"), String.Format(@"int foo1() {{{0}printf(""hello world 1!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "bar1.c"), String.Format(@"int bar1() {{{0}    printf(""goodbye, world 1!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "foo2.c"), String.Format(@"int foo2() {{{0}printf(""hello world 2!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "bar2.c"), String.Format(@"int bar2() {{{0}    printf(""goodbye, world 2!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c"), String.Format(@"int foo11() {{{0}printf(""hello world 11!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir11", "bar11.c"), String.Format(@"int bar11() {{{0}    printf(""goodbye, world 11!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir12", "foo12.c"), String.Format(@"int foo12() {{{0}printf(""hello world 12!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c"), String.Format(@"int bar12() {{{0}    printf(""goodbye, world 12!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir21", "foo21.c"), String.Format(@"int foo21() {{{0}printf(""hello world 21!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir21", "bar21.c"), String.Format(@"int bar21() {{{0}    printf(""goodbye, world 21!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir22", "foo22.c"), String.Format(@"int foo22() {{{0}printf(""hello world 22!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir22", "bar22.c"), String.Format(@"int bar22() {{{0}    printf(""goodbye, world 22!"");{0}}}", Environment.NewLine) },
            };

            foreach (var fileDataPair in sourceFiles)
            {
                var directory = Path.GetDirectoryName(fileDataPair.Key);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                File.WriteAllText(fileDataPair.Key, fileDataPair.Value);
                archive.AddOrUpdateFile(fileDataPair.Key);
                Assert.That(resetEvent.WaitOne(300));
                Assert.AreEqual(expectedEventType, actualEventType);
            }

            foreach (var fileName in sourceFiles.Keys)
            {
                Assert.That(archive.ContainsFile(fileName), String.Format("Archive should contain {0}", fileName));
            }

            var changedFileName     = Path.Combine(SourceDirectory, "foo.c");
            var changedFileContents = String.Format(@"int foo() {{{0}printf(""hello world! changed"");{0}}}", Environment.NewLine);

            expectedEventType = FileEventType.FileChanged;
            File.WriteAllText(changedFileName, changedFileContents);
            File.SetLastWriteTime(changedFileName, DateTime.Now);

            Assert.That(archive.ContainsFile(changedFileName));
            Assert.That(archive.IsOutdated(changedFileName));

            archive.AddOrUpdateFile(changedFileName);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);

            expectedEventType = FileEventType.FileDeleted;
            var deletedFileName = Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c");

            File.Delete(deletedFileName);
            Assert.That(archive.IsOutdated(deletedFileName));
            archive.DeleteFile(deletedFileName);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);

            expectedEventType = FileEventType.FileRenamed;
            var movedFileName   = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c");
            var newNameForMoved = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo1111111.c");

            File.Move(movedFileName, newNameForMoved);
            Assert.That(archive.IsOutdated(movedFileName));
            archive.RenameFile(movedFileName, newNameForMoved);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);
            Assert.That(archive.ContainsFile(newNameForMoved));
            Assert.IsFalse(archive.ContainsFile(movedFileName));
        }