Exemple #1
0
        public void TestMethodDefinitionMerge_NoParameters()
        {
            string         declXml = "int Foo();";
            LibSrcMLRunner runF    = new LibSrcMLRunner();
            string         srcMLF  = runF.GenerateSrcMLFromString(declXml, "Foo.h", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var fileunitDecl     = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcMLF, "Foo.h");
            var declarationScope = CodeParser[Language.CPlusPlus].ParseFileUnit(fileunitDecl);

            string         defXml = @"int Foo() { return 0; }";
            LibSrcMLRunner runG   = new LibSrcMLRunner();
            string         srcMLG = runG.GenerateSrcMLFromString(defXml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var fileUnitDef     = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcMLG, "Foo.cpp");
            var definitionScope = CodeParser[Language.CPlusPlus].ParseFileUnit(fileUnitDef);

            var globalScope = declarationScope.Merge(definitionScope);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            Assert.AreEqual("Foo", ((MethodDefinition)globalScope.ChildStatements.First()).Name);
        }
Exemple #2
0
        public void TestHeaderRemovalWithNamespaceAndClass()
        {
            string hXml = @"namespace A {
              class Foo {
                  public:
                      int Bar(int b);
              };
            }";

            string cppXml = @"int A::Foo::Bar(int b) { }";

            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(hXml, "Foo.h", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var            hFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Foo.h");
            LibSrcMLRunner runB      = new LibSrcMLRunner();
            string         srcMLB    = runA.GenerateSrcMLFromString(cppXml, "Foo.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var cppFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "Foo.cpp");

            var beforeScope = CodeParser.ParseFileUnit(cppFileUnit);
            var afterScope  = beforeScope.Merge(CodeParser.ParseFileUnit(hFileUnit));

            afterScope.RemoveFile("Foo.h");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #3
0
        public void TestRemoveMethodDeclaration_Global()
        {
            string         defXml = "int Foo(char bar) { return 0; }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(defXml, "Foo.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var fileUnitDef = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Foo.cpp");
            var beforeScope = CodeParser.ParseFileUnit(fileUnitDef);

            string         declXml = "int Foo(char bar);";
            LibSrcMLRunner runB    = new LibSrcMLRunner();
            string         srcMLB  = runB.GenerateSrcMLFromString(declXml, "Foo.h", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var fileunitDecl = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "Foo.h");
            var afterScope   = beforeScope.Merge(CodeParser.ParseFileUnit(fileunitDecl));

            Assert.AreEqual(1, afterScope.ChildStatements.Count());
            Assert.AreEqual("Foo", ((MethodDefinition)afterScope.ChildStatements.First()).Name);

            afterScope.RemoveFile("Foo.h");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #4
0
        public void TestRemoveClassDefinition()
        {
            string         cppXml = @"int Foo::Add(int b) {
              return this->a + b;
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(cppXml, "A.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var cppFileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cpp");
            var beforeScope = CodeParser.ParseFileUnit(cppFileunit);

            string         hXml   = @"class Foo {
              public:
                int a;
                int Add(int b);
            };";
            LibSrcMLRunner runB   = new LibSrcMLRunner();
            string         srcMLB = runB.GenerateSrcMLFromString(hXml, "A.h", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var hFileunit  = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "A.h");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(hFileunit));

            Assert.AreEqual(1, afterScope.ChildStatements.Count());
            Assert.IsNotNull(afterScope.ChildStatements.First() as TypeDefinition);

            afterScope.RemoveFile("A.h");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #5
0
        public void TestRemovePartOfNamespace()
        {
            string         a1Xml  = @"namespace A {
              int Foo(){ return 0;}
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(a1Xml, "A1.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var a1FileUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "A1.cpp");
            var beforeScope = CodeParser.ParseFileUnit(a1FileUnit);

            string         a2Xml  = @"namespace A {
                char* Bar(){return 'Hello, World!';}
            }";
            LibSrcMLRunner runB   = new LibSrcMLRunner();
            string         srcMLB = runA.GenerateSrcMLFromString(a2Xml, "A2.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var a2Fileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "A2.cpp");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(a2Fileunit));

            Assert.AreEqual(1, afterScope.ChildStatements.OfType <NamespaceDefinition>().Count());
            Assert.AreEqual(2, afterScope.ChildStatements.First().ChildStatements.OfType <MethodDefinition>().Count());

            afterScope.RemoveFile("A2.cpp");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #6
0
        public void TestRemoveMethodFromGlobal()
        {
            string         fooXml = @"int Foo() { return 0; }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(fooXml, "Foo.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var fileunitFoo = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Foo.cpp");
            var beforeScope = CodeParser.ParseFileUnit(fileunitFoo);

            string         bazXml = "char* Baz() { return \"Hello, World!\"; }";
            LibSrcMLRunner runB   = new LibSrcMLRunner();
            string         srcMLB = runB.GenerateSrcMLFromString(bazXml, "Baz.cpp", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var fileunitBaz = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "Baz.cpp");
            var afterScope  = beforeScope.Merge(CodeParser.ParseFileUnit(fileunitBaz));

            Assert.AreEqual(2, afterScope.ChildStatements.OfType <MethodDefinition>().Count());

            afterScope.RemoveFile("Baz.cpp");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #7
0
        public void TestTypeUseForOtherNamespace()
        {
            string c_xml = @"package A.B;
            class C {
                int Foo();
            }";

            string e_xml = @"package D;
            import A.B.*;
            class E {
                public static void main() {
                    C c = new C();
                    c.Foo();
                }
            }";

            LibSrcMLRunner runC   = new LibSrcMLRunner();
            string         srcMLA = runC.GenerateSrcMLFromString(c_xml, "C.java", Language.Java, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var            cUnit  = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "C.java");
            LibSrcMLRunner runE   = new LibSrcMLRunner();
            string         srcMLE = runE.GenerateSrcMLFromString(e_xml, "E.java", Language.Java, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var eUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLE, "E.java");

            var globalScope = codeParser.ParseFileUnit(cUnit);

            globalScope = globalScope.Merge(codeParser.ParseFileUnit(eUnit));

            var typeC = globalScope.GetDescendants <TypeDefinition>().Where(t => t.Name == "C").FirstOrDefault();
            var typeE = globalScope.GetDescendants <TypeDefinition>().Where(t => t.Name == "E").FirstOrDefault();

            var mainMethod = typeE.GetNamedChildren <MethodDefinition>("main").FirstOrDefault();

            Assert.AreEqual("main", mainMethod.Name);

            var fooMethod = typeC.ChildStatements.OfType <MethodDefinition>().FirstOrDefault();

            Assert.IsNotNull(fooMethod, "no method foo found");
            Assert.AreEqual("Foo", fooMethod.Name);

            var cDeclaration = mainMethod.FindExpressions <VariableDeclaration>(true).FirstOrDefault();

            Assert.IsNotNull(cDeclaration, "No declaration found");
            Assert.AreSame(typeC, cDeclaration.VariableType.ResolveType().FirstOrDefault());

            var callToCConstructor = mainMethod.FindExpressions <MethodCall>(true).First();
            var callToFoo          = mainMethod.FindExpressions <MethodCall>(true).Last();

            Assert.AreEqual("C", callToCConstructor.Name);
            Assert.That(callToCConstructor.IsConstructor);
            Assert.IsNull(callToCConstructor.FindMatches().FirstOrDefault());

            Assert.AreEqual("Foo", callToFoo.Name);
            Assert.AreSame(fooMethod, callToFoo.FindMatches().FirstOrDefault());
        }
Exemple #8
0
        public void TestMethodCallCreation_SuperConstructor()
        {
            string         xml    = @"class B {
              public B(int num) { }
            }
            class C extends B { 
              public C() {
                super(17);
              }
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(xml, "A.java", Language.Java, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.java");

            var globalScope = codeParser.ParseFileUnit(xmlElement);

            Assert.AreEqual(2, globalScope.ChildStatements.Count);

            var bConstructor = globalScope.GetDescendants <MethodDefinition>().FirstOrDefault(m => m.Name == "B");

            Assert.IsNotNull(bConstructor);
            var cConstructor = globalScope.GetDescendants <MethodDefinition>().FirstOrDefault(m => m.Name == "C");

            Assert.IsNotNull(cConstructor);

            Assert.AreEqual(1, cConstructor.ChildStatements.Count);
            var superCall = cConstructor.ChildStatements[0].Content.GetDescendantsAndSelf <MethodCall>().FirstOrDefault();

            Assert.IsNotNull(superCall);
            Assert.IsTrue(superCall.IsConstructor);
            Assert.AreSame(bConstructor, superCall.FindMatches().FirstOrDefault());
        }
Exemple #9
0
        public void TestPartialMethodMerge_CSharp()
        {
            string         a1Xml  = @"public partial class A {
                public partial int Foo();
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(a1Xml, "A1.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var a1FileUnit  = FileUnitSetup[Language.CSharp].GetFileUnitForXmlSnippet(srcMLA, "A1.cs");
            var globalScope = CodeParser[Language.CSharp].ParseFileUnit(a1FileUnit);

            string         a2Xml  = @"public partial class A {
                public partial int Foo() { return 42; }
            }";
            LibSrcMLRunner runB   = new LibSrcMLRunner();
            string         srcMLB = runB.GenerateSrcMLFromString(a2Xml, "A2.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var a2FileUnit = FileUnitSetup[Language.CSharp].GetFileUnitForXmlSnippet(srcMLB, "A2.cs");

            globalScope = globalScope.Merge(CodeParser[Language.CSharp].ParseFileUnit(a2FileUnit));

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            var typeA = globalScope.ChildStatements.First() as TypeDefinition;

            Assert.IsNotNull(typeA);
            Assert.AreEqual(1, typeA.ChildStatements.OfType <MethodDefinition>().Count());
            var foo = typeA.ChildStatements.First() as MethodDefinition;

            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
        }
Exemple #10
0
        public void TestField(Language lang)
        {
            string         xml   = @"class A {
              int Foo;
              Bar baz;
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var declStmts   = globalScope.GetDescendantsAndSelf <DeclarationStatement>().ToList();

            Assert.AreEqual(2, declStmts.Count);

            var foo = declStmts[0].Content.GetDescendantsAndSelf <VariableDeclaration>().FirstOrDefault();

            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual("int", foo.VariableType.Name);
            Assert.AreEqual(AccessModifier.None, foo.Accessibility);

            var baz = declStmts[1].Content.GetDescendantsAndSelf <VariableDeclaration>().FirstOrDefault();

            Assert.IsNotNull(baz);
            Assert.AreEqual("baz", baz.Name);
            Assert.AreEqual("Bar", baz.VariableType.Name);
            Assert.AreEqual(AccessModifier.None, baz.Accessibility);
        }
        public void TestRemoveClass_Global() {
            string fooXml = @"class Foo {
                private int bar;
                public Foo() { bar = 42; }
                public int GetBar() { return bar; }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fooFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.cs");
            var beforeScope = CodeParser.ParseFileUnit(fooFileUnit);

            string bazXml = @"class Baz {
              public static int DoWork() { return 0; }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(bazXml, "Baz.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bazFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Baz.cs");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(bazFileUnit));

            Assert.AreEqual(0, afterScope.ChildStatements.OfType<NamespaceDefinition>().Count());
            Assert.AreEqual(2, afterScope.ChildStatements.OfType<TypeDefinition>().Count());

            afterScope.RemoveFile("Baz.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #12
0
        public void TestField(Language lang) {

            string xml = @"class A {
              int Foo;
              Bar baz;
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var declStmts = globalScope.GetDescendantsAndSelf<DeclarationStatement>().ToList();
            Assert.AreEqual(2, declStmts.Count);

            var foo = declStmts[0].Content.GetDescendantsAndSelf<VariableDeclaration>().FirstOrDefault();
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual("int", foo.VariableType.Name);
            Assert.AreEqual(AccessModifier.None, foo.Accessibility);

            var baz = declStmts[1].Content.GetDescendantsAndSelf<VariableDeclaration>().FirstOrDefault();
            Assert.IsNotNull(baz);
            Assert.AreEqual("baz", baz.Name);
            Assert.AreEqual("Bar", baz.VariableType.Name);
            Assert.AreEqual(AccessModifier.None, baz.Accessibility);
        }
        public void TestGetCallsTo_Simple() {

            string xml = @"void foo() {
              printf('Hello');
            }
            
            int main() {
              foo();
              return 0;
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(xmlElement);
            var fooMethod = globalScope.GetNamedChildren<MethodDefinition>("foo").First();
            var mainMethod = globalScope.GetNamedChildren<MethodDefinition>("main").First();

            Assert.That(mainMethod.ContainsCallTo(fooMethod));
            var fooCalls = mainMethod.GetCallsTo(fooMethod, true).ToList();
            Assert.AreEqual(1, fooCalls.Count);
            var expectedFooCall = mainMethod.FindExpressions<MethodCall>(true).First(mc => mc.Name == "foo");
            Assert.AreSame(expectedFooCall, fooCalls[0]);

            var callsToFoo = fooMethod.GetCallsToSelf().ToList();
            Assert.AreEqual(1, callsToFoo.Count);
            Assert.AreSame(expectedFooCall, callsToFoo[0]);
        }
        public void TestCppBuiltIns_WithSingleWord()
        {
            // #a.cpp TYPE a; TYPE b;

            string xmlFormat = @"{0} a; {0} b;";

            foreach (var builtIn in new string[] { "char", "short", "int", "long", "bool", "float", "double", "wchar_t" })
            {
                LibSrcMLRunner run   = new LibSrcMLRunner();
                string         srcML = run.GenerateSrcMLFromString(String.Format(xmlFormat, builtIn), "a.cpp", Language.CPlusPlus, new Collection <UInt32>()
                {
                }, false);

                var aXml = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "a.cpp");

                var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(aXml);
                var variables   = from stmt in globalScope.GetDescendants()
                                  from declaration in stmt.GetExpressions().OfType <VariableDeclaration>()
                                  select declaration;
                var variableA = variables.FirstOrDefault();
                var variableB = variables.LastOrDefault();

                Assert.AreEqual("a", variableA.Name);
                Assert.AreEqual("b", variableB.Name);
                Assert.AreEqual(builtIn, variableA.VariableType.Name);

                var typeOfA = variableA.VariableType.FindMatches().First();
                var typeOfB = variableB.VariableType.FindMatches().First();
                Assert.AreSame(typeOfA, typeOfB);
            }
        }
        public void TestCppBuiltIns_WithDoubleWord()
        {
            // #a.cpp #example: "unsigned int a;"; MODIFIER TYPE a; MODIFIER TYPE b;
            string xmlFormat = @"{0} {1} a; {0} {1} b;";

            foreach (var builtInModifier in new string[] { "unsigned", "signed", "long" })
            {
                foreach (var builtIn in new string[] { "int", "double" })
                {
                    LibSrcMLRunner run   = new LibSrcMLRunner();
                    string         srcML = run.GenerateSrcMLFromString(String.Format(xmlFormat, builtInModifier, builtIn), "a.cpp", Language.CPlusPlus, new Collection <UInt32>()
                    {
                    }, false);

                    var aXml = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "a.cpp");

                    var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(aXml);
                    var variables   = from stmt in globalScope.GetDescendants()
                                      from declaration in stmt.GetExpressions().OfType <VariableDeclaration>()
                                      select declaration;
                    var variableA = variables.FirstOrDefault();
                    var variableB = variables.LastOrDefault();

                    Assert.AreEqual("a", variableA.Name);
                    Assert.AreEqual("b", variableB.Name);
                    Assert.AreEqual(String.Format("{0} {1}", builtInModifier, builtIn), variableA.VariableType.Name, "TODO: Fix compound types");
                    var typeOfA = variableA.VariableType.FindMatches().First();
                    var typeOfB = variableB.VariableType.FindMatches().First();
                    Assert.AreSame(typeOfA, typeOfB);
                }
            }
        }
Exemple #16
0
        public void TestResolveLocalVariable_ParentExpression(Language lang)
        {
            string         xml   = @"int Foo() {
              for(int i = 0; i < bar; i++) {
                printf(i);
              }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var forStmt     = globalScope.GetDescendants <ForStatement>().First();

            Assert.AreEqual(1, forStmt.ChildStatements.Count());

            var iDecl = forStmt.Initializer.GetDescendantsAndSelf <VariableDeclaration>().FirstOrDefault(v => v.Name == "i");

            Assert.IsNotNull(iDecl);
            var iUse = forStmt.ChildStatements[0].Content.GetDescendantsAndSelf <NameUse>().FirstOrDefault(n => n.Name == "i");

            Assert.IsNotNull(iUse);
            Assert.AreSame(iDecl, iUse.FindMatches().FirstOrDefault());
        }
        public void TestRemovePartialClass() {
            string a1Xml = @"public partial class A {
              public int Execute() {
                   return 0;
              }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(a1Xml, "A1.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var a1FileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "A1.cs");
            var beforeScope = CodeParser.ParseFileUnit(a1FileUnit);

            string a2Xml = @"public partial class A {
              private bool Foo() {
                return true;
              }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(a2Xml, "A2.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var a2FileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "A2.cs");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(a2FileUnit));

            Assert.AreEqual(1, afterScope.ChildStatements.Count());
            var typeA = afterScope.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(typeA);
            Assert.AreEqual(2, typeA.ChildStatements.OfType<MethodDefinition>().Count());
            Assert.IsTrue(typeA.ChildStatements.OfType<MethodDefinition>().Any(m => m.Name == "Execute"));
            Assert.IsTrue(typeA.ChildStatements.OfType<MethodDefinition>().Any(m => m.Name == "Foo"));

            afterScope.RemoveFile("A2.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #18
0
        public void TestLocationInMain_Cpp()
        {
            var            xml   = @"#include <iostream>
            char* MyFunction(int foo) {
                if(foo > 0) {
                    return 'Hello world!';
                } else {
                    return 'Goodbye cruel world!';
                }
            }
            
            int main(int argc, char* argv[]) {
                std::cout<<MyFunction(42);
                return 0;
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "function_def.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var fileUnit    = fileSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "function_def.cpp");
            var globalScope = parser[Language.CPlusPlus].ParseFileUnit(fileUnit);

            var main = globalScope.GetDescendants <MethodDefinition>().First(md => md.Name == "main");

            Assert.AreSame(main.ChildStatements[0], globalScope.GetStatementForLocation(new SourceLocation("function_def.cpp", 11, 17)));
        }
Exemple #19
0
        public void TestResolveLocalVariable(Language lang)
        {
            string         xml   = @"int Foo() {
              if(MethodCall()) {
                int bar = 17;
                bar = 42;
              }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var ifStmt      = globalScope.GetDescendants <IfStatement>().First();

            Assert.AreEqual(2, ifStmt.ChildStatements.Count());

            var barDecl = ifStmt.ChildStatements[0].Content.GetDescendantsAndSelf <VariableDeclaration>().FirstOrDefault(v => v.Name == "bar");

            Assert.IsNotNull(barDecl);
            var barUse = ifStmt.ChildStatements[1].Content.GetDescendantsAndSelf <NameUse>().FirstOrDefault(n => n.Name == "bar");

            Assert.IsNotNull(barUse);
            Assert.AreSame(barDecl, barUse.FindMatches().FirstOrDefault());
        }
Exemple #20
0
        public void TestLocationInMethodDefinition_Cpp()
        {
            var            hXml  = @"class Foo {
            public:
                int bar(int);
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(hXml, "Foo.h", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var hUnit       = fileSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.h");
            var globalScope = parser[Language.CPlusPlus].ParseFileUnit(hUnit);

            var            cppXml = @"#include 'Foo.h'
            int Foo::bar(int baz) {
                return baz + 1;
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(cppXml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var cppUnit = fileSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcMLA, "Foo.cpp");

            globalScope = globalScope.Merge(parser[Language.CPlusPlus].ParseFileUnit(cppUnit));

            var bar = globalScope.GetDescendants <MethodDefinition>().First(md => md.Name == "bar");

            Assert.AreEqual(1, bar.ChildStatements.Count);
            Assert.AreEqual(bar.ChildStatements[0], globalScope.GetStatementForLocation(new SourceLocation("Foo.cpp", 3, 17)));
        }
Exemple #21
0
        public void TestLocationInClass_CSharp()
        {
            var            xml   = @"namespace Example {
                class Foo {
                    int bar = 42;
                }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "Foo.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var unit        = fileSetup[Language.CSharp].GetFileUnitForXmlSnippet(srcML, "Foo.cs");
            var globalScope = parser[Language.CSharp].ParseFileUnit(unit);

            var foo        = globalScope.GetDescendants <TypeDefinition>().First(t => t.Name == "Foo");
            var stmt       = foo.ChildStatements[0];
            var stmtActual = globalScope.GetStatementForLocation(new SourceLocation("Foo.cs", 3, 14));

            Assert.AreSame(stmt, stmtActual.ChildStatements[0]);

            var fooActual = globalScope.GetStatementForLocation(new SourceLocation("Foo.cs", 3, 14));

            Assert.AreSame(foo, fooActual);
        }
Exemple #22
0
        public void TestConstructorMerge_Cpp() {
            string header_xml = @"class A { A(); };";

            string impl_xml = @"A::A() { }";

            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(header_xml, "A.h", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var header = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcMLA, "A.h");
            LibSrcMLRunner runE = new LibSrcMLRunner();
            string srcMLE = runE.GenerateSrcMLFromString(impl_xml, "A.cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var implementation = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcMLE, "A.cpp");

            var headerScope = CodeParser[Language.CPlusPlus].ParseFileUnit(header);
            var implementationScope = CodeParser[Language.CPlusPlus].ParseFileUnit(implementation);

            var globalScope = headerScope.Merge(implementationScope);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());

            var typeA = globalScope.ChildStatements.First() as TypeDefinition;
            Assert.AreEqual("A", typeA.Name);
            Assert.AreEqual(1, typeA.ChildStatements.Count());
            Assert.AreEqual("A.h", typeA.PrimaryLocation.SourceFileName);

            var constructor = typeA.ChildStatements.First() as MethodDefinition;
            Assert.That(constructor.IsConstructor);
            Assert.IsFalse(constructor.IsDestructor);
            Assert.IsFalse(constructor.IsPartial);
            Assert.AreEqual(AccessModifier.Private, constructor.Accessibility);
            Assert.AreEqual("A.cpp", constructor.PrimaryLocation.SourceFileName);
        }
        public void TestRemoveMethodFromClass() {
            string cppXml = @"int Foo::Add(int b) {
              return this->a + b;
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(cppXml, "A.cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            var cppFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cpp");

            string hXml = @"class Foo {
              public:
                int a;
                int Add(int b);
            };";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(hXml, "A.h", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            var hFileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLB, "A.h");

            var beforeScope = CodeParser.ParseFileUnit(hFileunit);
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(cppFileUnit));

            Assert.AreEqual(1, afterScope.ChildStatements.Count());
            Assert.IsNotNull(afterScope.ChildStatements.First() as TypeDefinition);

            afterScope.RemoveFile("A.cpp");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
        public void TestDataFileNameMap(bool compressionEnabled) {
            //I'm not exactly sure what this test is testing for, but the way files are named by the library
            //is not playing well with this test. Seems like it wants a specific file extension. Some decision
            //will have to be made about how to handle this problem. Does the library need to generate names differently?
            //Or maybe this test should be different?
            var generator = new DataGenerator();
            var mapping = new DataFileNameMapping(TEST_DIRECTORY, compressionEnabled);
            var sourcePath = @"..\..\TestInputs\function_def.cpp";
            var srcmlPath = @"..\..\TestInputs\function_def.xml";

            var mappedPath = mapping.GetTargetPath(sourcePath);

            var actualExtension = Path.GetExtension(mappedPath);
            var expectedExtension = (compressionEnabled ? XmlSerialization.DEFAULT_COMPRESSED_EXTENSION : XmlSerialization.DEFAULT_EXTENSION);
            StringAssert.AreEqualIgnoringCase(expectedExtension, actualExtension);

            //generator.Generate(srcmlPath, mappedPath);
            LibSrcMLRunner runner = new LibSrcMLRunner();
            runner.GenerateSrcMLFromFile(srcmlPath, mappedPath, Language.CPlusPlus, new Collection<uint> { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, new Dictionary<string, Language> { });


            Assert.That(File.Exists(mappedPath), String.Format("Could not generate {0}", mappedPath));

            var data = XmlSerialization.Load(mappedPath, compressionEnabled);
            Assert.IsNotNull(data, String.Format("Could not load data from {0}. It should {1}be compressed", mappedPath, compressionEnabled ? String.Empty : "not "));
        }
Exemple #25
0
        public void TestRemoveNamespace()
        {
            string         aXml  = @"namespace A {
                class Foo { int bar; }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(aXml, "A.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var aFileunit   = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "A.cs");
            var beforeScope = CodeParser.ParseFileUnit(aFileunit);

            string         bXml   = @"namespace B {
                class Baz { public ulong xyzzy; }
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(bXml, "B.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var bFileunit  = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "B.cs");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(bFileunit));

            Assert.AreEqual(2, afterScope.ChildStatements.OfType <NamespaceDefinition>().Count());

            afterScope.RemoveFile("B.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemple #26
0
        public void TestFileRemovalWithDifferentCase()
        {
            string bXml = @"namespace A { class B { } }";

            string dXml = @"namespace C { class D { } }";

            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(bXml, "B.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var            bUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "B.cs");
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(dXml, "D.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var dUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "D.cs");

            var bScope      = CodeParser.ParseFileUnit(bUnit);
            var dScope      = CodeParser.ParseFileUnit(dUnit);
            var globalScope = bScope.Merge(dScope);

            globalScope.RemoveFile("b.cs");
            Assert.AreEqual(1, globalScope.ChildStatements.Count());
        }
        public void TestRemoveClass_Namespace() {
            string fooXml = @"package com.ABB.Example;
            class Foo {
                private int bar;
                public Foo() { bar = 42; }
                public int GetBar() { return bar; }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(fooXml, "Foo.java", Language.Java, new Collection<UInt32>() { }, false);
            var fooFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.java");
            var beforeScope = CodeParser.ParseFileUnit(fooFileUnit);

            string bazXml = @"package com.ABB.Example;
            class Baz {
                public static int DoWork() { return 0; }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(bazXml, "Baz.java", Language.Java, new Collection<UInt32>() { }, false);
            var bazFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Baz.java");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(bazFileUnit));

            Assert.AreEqual(1, afterScope.ChildStatements.OfType<NamespaceDefinition>().Count());

            afterScope.RemoveFile("Baz.java");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
        public void TestCreateTypeDefinition_ClassInPackage() {
            string xml = @"package A.B.C;
            public class D { }";
            LibSrcMLRunner runD = new LibSrcMLRunner();
            string srcMLA = runD.GenerateSrcMLFromString(xml, "D.java", Language.Java, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "D.java");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            Assert.IsTrue(globalScope.IsGlobal);
            Assert.AreEqual(1, globalScope.ChildStatements.Count());

            var packageA = globalScope.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(packageA);
            Assert.AreEqual("A", packageA.Name);
            Assert.IsFalse(packageA.IsGlobal);
            Assert.AreEqual(1, packageA.ChildStatements.Count());

            var packageAB = packageA.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(packageAB);
            Assert.AreEqual("B", packageAB.Name);
            Assert.IsFalse(packageAB.IsGlobal);
            Assert.AreEqual(1, packageAB.ChildStatements.Count());

            var packageABC = packageAB.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(packageABC);
            Assert.AreEqual("C", packageABC.Name);
            Assert.IsFalse(packageABC.IsGlobal);
            Assert.AreEqual(1, packageABC.ChildStatements.Count());

            var typeD = packageABC.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(typeD, "Type D is not a type definition");
            Assert.AreEqual("D", typeD.Name);
        }
Exemple #29
0
        public void TestGetNamedChildren_Statement(Language lang)
        {
            string         xml   = @"int foo = 17;
            while(bar) {
              MethodCall(foo);
              int foo = 42;
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);

            Assert.AreEqual(2, globalScope.ChildStatements.Count);

            var whileStmt = globalScope.GetDescendants <WhileStatement>().First();

            Assert.AreEqual(2, whileStmt.ChildStatements.Count);
            var fooUse   = whileStmt.ChildStatements[0].Content.GetDescendantsAndSelf <NameUse>().First(n => n.Name == "foo");
            var localFoo = whileStmt.ChildStatements[1].Content.GetDescendantsAndSelf <VariableDeclaration>().First(v => v.Name == "foo");

            var allChildren = whileStmt.GetNamedChildren("foo").ToList();

            Assert.AreEqual(1, allChildren.Count);
            Assert.AreSame(localFoo, allChildren[0]);

            Assert.IsEmpty(whileStmt.GetNamedChildren(fooUse).ToList());
        }
        public void TestCppBuiltIns_WithDoubleWord() {
            // #a.cpp #example: "unsigned int a;"; MODIFIER TYPE a; MODIFIER TYPE b;
            string xmlFormat = @"{0} {1} a; {0} {1} b;";

            foreach(var builtInModifier in new string[] { "unsigned", "signed", "long" }) {
                foreach(var builtIn in new string[] { "int", "double" }) {
                    LibSrcMLRunner run = new LibSrcMLRunner();
                    string srcML = run.GenerateSrcMLFromString(String.Format(xmlFormat, builtInModifier, builtIn), "a.cpp", Language.CPlusPlus, new Collection<UInt32>() { }, false);

                    var aXml = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "a.cpp");

                    var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(aXml);
                    var variables = from stmt in globalScope.GetDescendants()
                                    from declaration in stmt.GetExpressions().OfType<VariableDeclaration>()
                                    select declaration;
                    var variableA = variables.FirstOrDefault();
                    var variableB = variables.LastOrDefault();
                    
                    Assert.AreEqual("a", variableA.Name);
                    Assert.AreEqual("b", variableB.Name);
                    Assert.AreEqual(String.Format("{0} {1}", builtInModifier, builtIn), variableA.VariableType.Name, "TODO: Fix compound types");
                    var typeOfA = variableA.VariableType.FindMatches().First();
                    var typeOfB = variableB.VariableType.FindMatches().First();
                    Assert.AreSame(typeOfA, typeOfB);
                }
            }
        }
Exemple #31
0
        public void TestApplyXPathToSrcMLString()
        {
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString("int main(){int x;}", "input.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_MODIFIER
            }, false);

            Assert.IsTrue(File.Exists("function_def.xml"));
            Assert.IsTrue(File.Exists("Test.xsl"));

            string xslSrcML = run.ApplyXPathToSrcMLString(srcML, "//src:unit");

            XDocument srcMLDoc = XDocument.Parse(xslSrcML);

            Assert.IsNotNull(srcMLDoc);

            XmlReader           read             = srcMLDoc.CreateReader();
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(read.NameTable);

            namespaceManager.AddNamespace("src", "http://www.srcML.org/srcML/src");

            var persist = srcMLDoc.XPathSelectElement("//src:test", namespaceManager);

            Assert.IsNotNull(persist);
            Assert.AreEqual(persist.Value, "TestPassed");
        }
        public void TestDataFileNameMap(bool compressionEnabled)
        {
            //I'm not exactly sure what this test is testing for, but the way files are named by the library
            //is not playing well with this test. Seems like it wants a specific file extension. Some decision
            //will have to be made about how to handle this problem. Does the library need to generate names differently?
            //Or maybe this test should be different?
            var generator  = new DataGenerator();
            var mapping    = new DataFileNameMapping(TEST_DIRECTORY, compressionEnabled);
            var sourcePath = @"..\..\TestInputs\function_def.cpp";
            var srcmlPath  = @"..\..\TestInputs\function_def.xml";

            var mappedPath = mapping.GetTargetPath(sourcePath);

            var actualExtension   = Path.GetExtension(mappedPath);
            var expectedExtension = (compressionEnabled ? XmlSerialization.DEFAULT_COMPRESSED_EXTENSION : XmlSerialization.DEFAULT_EXTENSION);

            StringAssert.AreEqualIgnoringCase(expectedExtension, actualExtension);

            //generator.Generate(srcmlPath, mappedPath);
            LibSrcMLRunner runner = new LibSrcMLRunner();

            runner.GenerateSrcMLFromFile(srcmlPath, mappedPath, Language.CPlusPlus, new Collection <uint> {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, new Dictionary <string, Language> {
            });


            Assert.That(File.Exists(mappedPath), String.Format("Could not generate {0}", mappedPath));

            var data = XmlSerialization.Load(mappedPath, compressionEnabled);

            Assert.IsNotNull(data, String.Format("Could not load data from {0}. It should {1}be compressed", mappedPath, compressionEnabled ? String.Empty : "not "));
        }
Exemple #33
0
        public void TestGenerateSrcMLFromFile()
        {
            LibSrcMLRunner run = new LibSrcMLRunner();

            try {
                run.GenerateSrcMLFromFile(Path.Combine(TestInputPath, "input.cpp"), "output", Language.CPlusPlus, new List <UInt32>()
                {
                    LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_MODIFIER
                }, new Dictionary <string, Language>()
                {
                });

                Assert.True(File.Exists("output0.cpp.xml"));
                SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml");
                Assert.IsNotNull(srcFile);

                var files = srcFile.FileUnits.ToList();
                Assert.AreEqual(1, files.Count());

                string file = Path.Combine(TestInputPath, "input.cpp");
                var    f1   = (from ele in files
                               where ele.Attribute("filename").Value == file
                               select ele);
                Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value);
            }
            catch (SrcMLException e) {
                throw new SrcMLException(e.Message, e);
            }
        }
Exemple #34
0
        public void TestApplyXsltToSrcMLFile()
        {
            LibSrcMLRunner run = new LibSrcMLRunner();

            Assert.IsTrue(File.Exists("function_def.xml"));
            Assert.IsTrue(File.Exists("Test.xsl"));
            run.ApplyXsltToSrcMLFile("function_def.xml", "Test.xsl", "o.cpp.xml");

            SrcMLFile srcFile = new SrcMLFile("o.cpp.xml.xslout");

            Assert.IsNotNull(srcFile);

            var files = srcFile.FileUnits.ToList();

            Assert.AreEqual(1, files.Count());

            XmlReader           read             = srcFile.GetXDocument().CreateReader();
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(read.NameTable);

            namespaceManager.AddNamespace("src", "http://www.srcML.org/srcML/src");

            var persist = srcFile.GetXDocument().XPathSelectElement("//src:test", namespaceManager);

            Assert.IsNotNull(persist);
            Assert.AreEqual(persist.Value, "TestPassed");
        }
        public void TestGetCallsTo_Masking()
        {
            var            xml   = @"void foo() { printf('Global foo'); } 
            class Bar {
            public:
              void foo() { printf('Bar::foo'); }
              void baz() { foo(); }
            };";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope     = CodeParser[Language.CPlusPlus].ParseFileUnit(xmlElement);
            var globalFooMethod = globalScope.GetNamedChildren <MethodDefinition>("foo").First();
            var bar             = globalScope.GetNamedChildren <TypeDefinition>("Bar").First();
            var barFooMethod    = bar.GetNamedChildren <MethodDefinition>("foo").First();
            var bazMethod       = bar.GetNamedChildren <MethodDefinition>("baz").First();

            Assert.That(bazMethod.ContainsCallTo(barFooMethod));
            Assert.IsFalse(bazMethod.ContainsCallTo(globalFooMethod));
            var fooCalls = bazMethod.GetCallsTo(barFooMethod, true).ToList();

            Assert.AreEqual(1, fooCalls.Count);
            var expectedFooCall = bazMethod.FindExpressions <MethodCall>(true).First(mc => mc.Name == "foo");

            Assert.AreSame(expectedFooCall, fooCalls[0]);

            Assert.IsEmpty(globalFooMethod.GetCallsToSelf());
            Assert.AreEqual(1, barFooMethod.GetCallsToSelf().Count());
        }
Exemple #36
0
        public void TestGenerateSrcMLFromString()
        {
            LibSrcMLRunner run = new LibSrcMLRunner();

            try {
                string b = run.GenerateSrcMLFromString("int main(){int x;}", "input.cpp", Language.CPlusPlus, new Collection <UInt32>()
                {
                    LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_MODIFIER
                }, false);

                Assert.False(String.IsNullOrEmpty(b));

                XDocument doc   = XDocument.Parse(b);
                var       units = from unit in doc.Descendants(XName.Get("unit", "http://www.srcML.org/srcML/src"))
                                  where unit.Attribute("filename") != null
                                  select unit;

                string file = "input.cpp";
                var    f1   = (from ele in units
                               where ele.Attribute("filename").Value == file
                               select ele);
                Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value);
            }
            catch (SrcMLException e) {
                throw e;
            }
        }
        public void TestGetCallsTo_Simple()
        {
            string         xml   = @"void foo() {
              printf('Hello');
            }
            
            int main() {
              foo();
              return 0;
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(xmlElement);
            var fooMethod   = globalScope.GetNamedChildren <MethodDefinition>("foo").First();
            var mainMethod  = globalScope.GetNamedChildren <MethodDefinition>("main").First();

            Assert.That(mainMethod.ContainsCallTo(fooMethod));
            var fooCalls = mainMethod.GetCallsTo(fooMethod, true).ToList();

            Assert.AreEqual(1, fooCalls.Count);
            var expectedFooCall = mainMethod.FindExpressions <MethodCall>(true).First(mc => mc.Name == "foo");

            Assert.AreSame(expectedFooCall, fooCalls[0]);

            var callsToFoo = fooMethod.GetCallsToSelf().ToList();

            Assert.AreEqual(1, callsToFoo.Count);
            Assert.AreSame(expectedFooCall, callsToFoo[0]);
        }
        public void TestGetCallsTo_NonRecursive()
        {
            string         xml   = @"int Qux() { return 42; }
            int Xyzzy() { return 17; }
            
            void foo() {
              if(Qux()) {
                print(Xyzzy());
              }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var xmlElement = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(xmlElement);
            var quxMethod   = globalScope.GetNamedChildren <MethodDefinition>("Qux").First();
            var xyzzyMethod = globalScope.GetNamedChildren <MethodDefinition>("Xyzzy").First();
            var ifStmt      = globalScope.GetDescendants <IfStatement>().First();

            Assert.That(ifStmt.ContainsCallTo(quxMethod));
            Assert.That(ifStmt.ContainsCallTo(xyzzyMethod));

            Assert.AreEqual(1, ifStmt.GetCallsTo(quxMethod, false).Count());
            Assert.AreEqual(0, ifStmt.GetCallsTo(xyzzyMethod, false).Count());
        }
        public void TestCppBuiltIns_WithSingleWord() {
            // #a.cpp TYPE a; TYPE b;

            string xmlFormat = @"{0} a; {0} b;";

            foreach(var builtIn in new string[] { "char", "short", "int", "long", "bool", "float", "double", "wchar_t" }) {
                LibSrcMLRunner run = new LibSrcMLRunner();
                string srcML = run.GenerateSrcMLFromString(String.Format(xmlFormat, builtIn), "a.cpp", Language.CPlusPlus, new Collection<UInt32>() { }, false);

                var aXml = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "a.cpp");

                var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(aXml);
                var variables = from stmt in globalScope.GetDescendants()
                                from declaration in stmt.GetExpressions().OfType<VariableDeclaration>()
                                select declaration;
                var variableA = variables.FirstOrDefault();
                var variableB = variables.LastOrDefault();

                Assert.AreEqual("a", variableA.Name);
                Assert.AreEqual("b", variableB.Name);
                Assert.AreEqual(builtIn, variableA.VariableType.Name);

                var typeOfA = variableA.VariableType.FindMatches().First();
                var typeOfB = variableB.VariableType.FindMatches().First();
                Assert.AreSame(typeOfA, typeOfB);
            }
        }
Exemple #40
0
        public void TestRemoveClass_Global()
        {
            string         fooXml = @"class Foo {
                private int bar;
                public Foo() { bar = 42; }
                public int GetBar() { return bar; }
            }";
            LibSrcMLRunner run    = new LibSrcMLRunner();
            string         srcML  = run.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var fooFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.cs");
            var beforeScope = CodeParser.ParseFileUnit(fooFileUnit);

            string         bazXml = @"class Baz {
              public static int DoWork() { return 0; }
            }";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(bazXml, "Baz.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var bazFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Baz.cs");
            var afterScope  = beforeScope.Merge(CodeParser.ParseFileUnit(bazFileUnit));

            Assert.AreEqual(0, afterScope.ChildStatements.OfType <NamespaceDefinition>().Count());
            Assert.AreEqual(2, afterScope.ChildStatements.OfType <TypeDefinition>().Count());

            afterScope.RemoveFile("Baz.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
        public void TestGetCallsTo_Multiple()
        {
            string xml = @"void star() { }
            
            void bar() { star(); }
            
            void foo() {
                bar();
                if(0) bar();
            }";

            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var unit = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(unit);

            var methodFoo  = globalScope.GetDescendants <MethodDefinition>().First(md => md.Name == "foo");
            var methodBar  = globalScope.GetDescendants <MethodDefinition>().First(md => md.Name == "bar");
            var methodStar = globalScope.GetDescendants <MethodDefinition>().First(md => md.Name == "star");

            Assert.That(methodFoo.ContainsCallTo(methodBar));
            Assert.AreEqual(2, methodFoo.GetCallsTo(methodBar, true).Count());

            Assert.That(methodBar.ContainsCallTo(methodStar));
            Assert.AreEqual(1, methodBar.GetCallsTo(methodStar, true).Count());

            Assert.IsFalse(methodFoo.ContainsCallTo(methodStar));
            Assert.IsFalse(methodBar.ContainsCallTo(methodFoo));
            Assert.IsFalse(methodStar.ContainsCallTo(methodFoo));
            Assert.IsFalse(methodStar.ContainsCallTo(methodBar));
        }
        public void TestGetCallsTo_Multiple() {

            string xml = @"void star() { }
            
            void bar() { star(); }
            
            void foo() {
                bar();
                if(0) bar();
            }";

            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "Foo.cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var unit = FileUnitSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Foo.cpp");

            var globalScope = CodeParser[Language.CPlusPlus].ParseFileUnit(unit);

            var methodFoo = globalScope.GetDescendants<MethodDefinition>().First(md => md.Name == "foo");
            var methodBar = globalScope.GetDescendants<MethodDefinition>().First(md => md.Name == "bar");
            var methodStar = globalScope.GetDescendants<MethodDefinition>().First(md => md.Name == "star");

            Assert.That(methodFoo.ContainsCallTo(methodBar));
            Assert.AreEqual(2, methodFoo.GetCallsTo(methodBar, true).Count());

            Assert.That(methodBar.ContainsCallTo(methodStar));
            Assert.AreEqual(1, methodBar.GetCallsTo(methodStar, true).Count());

            Assert.IsFalse(methodFoo.ContainsCallTo(methodStar));
            Assert.IsFalse(methodBar.ContainsCallTo(methodFoo));
            Assert.IsFalse(methodStar.ContainsCallTo(methodFoo));
            Assert.IsFalse(methodStar.ContainsCallTo(methodBar));
        }
 public void TestGlobalStatement() {
     var xml = @"char* bar = 'Hello, world!';
     int foo = 42;";
     LibSrcMLRunner run = new LibSrcMLRunner();
     string srcML = run.GenerateSrcMLFromString(xml, "Example.cpp", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
     var unit = fileSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "Example.cpp");
     var globalScope = parser[Language.CPlusPlus].ParseFileUnit(unit);
     var actual = globalScope.GetStatementForLocation(new SourceLocation("Example.cpp", 2, 5));
     Assert.AreSame(globalScope.ChildStatements[0], actual);
 }
 public void TestContains_Reflexive() {
     var xml = @"class Foo {
         int Bar(){return 0;}
     }";
     LibSrcMLRunner run = new LibSrcMLRunner();
     string srcML = run.GenerateSrcMLFromString(xml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { }, false);
     var classElement = fileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.cs").Descendants(SRC.Class).First();
     var classLoc = new SrcMLLocation(classElement, "Foo.cs");
     Assert.IsTrue(classLoc.Contains(classLoc));
 }
        public void TestCreateAliasesForFiles_ImportClass() {
            string xml = @"import x.y.z;";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.java", Language.Java, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.java");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            Assert.AreEqual(1, globalScope.ChildStatements.Count);

            var actual = globalScope.ChildStatements[0] as AliasStatement;
            Assert.IsNotNull(actual);
            Assert.AreEqual("z", actual.AliasName);
            Assert.AreEqual("x . y . z", actual.Target.ToString());
        }
 public void TestContains_TwoLevel() {
     var xml = @"namespace Example {
         class Foo {
             int Bar(){return 0;}
         }
     }";
     LibSrcMLRunner run = new LibSrcMLRunner();
     string srcML = run.GenerateSrcMLFromString(xml, "Example.cs", Language.CSharp, new Collection<UInt32>() { }, false);
     var namespaceElement = fileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Example.cs").Element(SRC.Namespace);
     var methodElement = namespaceElement.Descendants(SRC.Function).First();
     var namespaceLoc = new SrcMLLocation(namespaceElement, "Example.cs");
     var methodLoc = new SrcMLLocation(methodElement, "Example.cs");
     Assert.IsTrue(namespaceLoc.Contains(methodLoc));
 }
        public void TestCreateAliasesForFiles_ImportNamespace() {
            string xml = @"import x . /*test */ y  /*test */ . z .* /*test*/;";
            //<import>import <name><name>x</name> <op:operator>.</op:operator> <comment type=""block"">/*test */</comment> <name>y</name>  <comment type=""block"">/*test */</comment> <op:operator>.</op:operator> <name>z</name></name> .* <comment type=""block"">/*test*/</comment>;</import>";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.java", Language.Java, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.java");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            Assert.AreEqual(1, globalScope.ChildStatements.Count);

            var actual = globalScope.ChildStatements[0] as ImportStatement;
            Assert.IsNotNull(actual);
            Assert.AreEqual("x . y . z", actual.ImportedNamespace.ToString());
        }
 public void TestContains_Sibling() {
     var xml = @"class Foo {
         string Bar(){
             string a = 'Hello, world!';
             return a;
         }
         int Baz(){ return 0; }
     }";
     LibSrcMLRunner run = new LibSrcMLRunner();
     string srcML = run.GenerateSrcMLFromString(xml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { }, false);
     var methodElement = fileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.cs").Descendants(SRC.Function).First();
     var declElement = methodElement.Descendants(SRC.DeclarationStatement).First();
     var methodLoc = new SrcMLLocation(methodElement, "Foo.cs");
     var declLoc = new SrcMLLocation(declElement, "Foo.cs");
     Assert.IsTrue(methodLoc.Contains(declLoc));
 }
        public void TestCreateTypeDefinitions_ClassDeclaration() {
            string xml = @"class A;";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.h", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.h");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            var actual = globalScope.ChildStatements.First() as TypeDefinition;

            Assert.IsNotNull(actual);
            Assert.AreEqual("A", actual.Name);
            Assert.AreEqual(TypeKind.Class, actual.Kind);
            Assert.That(globalScope.IsGlobal);
            Assert.AreSame(globalScope, actual.ParentStatement);
        }
        public void TestRoundTrip(string sourceFileName, bool compressOutput) {
            var sourceFilePath = Path.Combine(TestInputPath, sourceFileName);
            var destFilePath = Path.Combine(TestInputPath, DefaultInputName);

            LibSrcMLRunner runner = new LibSrcMLRunner();
            runner.GenerateSrcMLFromFile(sourceFilePath, destFilePath + ".cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, new Dictionary<string, Language>() { });
            Assert.That(File.Exists(destFilePath + ".cpp0.xml"));

            var fileUnit = SrcMLElement.Load(destFilePath + ".cpp0.xml");
            var dataGenerator = new DataGenerator();
            var nsd = dataGenerator.Parse(fileUnit.Element(SRC.Unit)) as NamespaceDefinition;

            XmlSerialization.WriteElement(nsd, DefaultOutputName, compressOutput);
            var nsdFromFile = XmlSerialization.Load(DefaultOutputName, compressOutput) as NamespaceDefinition;
            DataAssert.StatementsAreEqual(nsd, nsdFromFile);
        }
Exemple #51
0
        public void TestTwoVariableDeclarations(Language lang) {
            int a, b;
            string xml = @"int a,b;";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.cpp", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var testUnit = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.cpp");

            var globalScope = codeParsers[lang].ParseFileUnit(testUnit);

            var declStmt = globalScope.ChildStatements.First();
            var varDecls = declStmt.Content.Components.OfType<VariableDeclaration>().ToList();

            Assert.AreEqual(2, varDecls.Count);
            Assert.AreEqual("a", varDecls[0].Name);
            Assert.AreEqual("int", varDecls[0].VariableType.Name);
            Assert.AreEqual("b", varDecls[1].Name);
            Assert.AreSame(varDecls[0].VariableType, varDecls[1].VariableType);
        }
        public void TestLocationInClass_CSharp() {
            var xml = @"namespace Example {
                class Foo {
                    int bar = 42;
                }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var unit = fileSetup[Language.CSharp].GetFileUnitForXmlSnippet(srcML, "Foo.cs");
            var globalScope = parser[Language.CSharp].ParseFileUnit(unit);

            var foo = globalScope.GetDescendants<TypeDefinition>().First(t => t.Name == "Foo");
            var stmt = foo.ChildStatements[0];
            var stmtActual = globalScope.GetStatementForLocation(new SourceLocation("Foo.cs", 3, 14));
            Assert.AreSame(stmt, stmtActual.ChildStatements[0]);

            var fooActual = globalScope.GetStatementForLocation(new SourceLocation("Foo.cs", 3, 14));
            Assert.AreSame(foo, fooActual);
        }
        public void TestLocationInMain_Cpp() {
            var xml = @"#include <iostream>
            char* MyFunction(int foo) {
                if(foo > 0) {
                    return 'Hello world!';
                } else {
                    return 'Goodbye cruel world!';
                }
            }
            
            int main(int argc, char* argv[]) {
                std::cout<<MyFunction(42);
                return 0;
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "function_def.cpp", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fileUnit = fileSetup[Language.CPlusPlus].GetFileUnitForXmlSnippet(srcML, "function_def.cpp");
            var globalScope = parser[Language.CPlusPlus].ParseFileUnit(fileUnit);

            var main = globalScope.GetDescendants<MethodDefinition>().First(md => md.Name == "main");
            Assert.AreSame(main.ChildStatements[0], globalScope.GetStatementForLocation(new SourceLocation("function_def.cpp", 11, 17)));
        }
        public void TestRemoveNamespace() {
            string aXml = @"namespace A {
                class Foo { int bar; }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(aXml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var aFileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "A.cs");
            var beforeScope = CodeParser.ParseFileUnit(aFileunit);

            string bXml = @"namespace B {
                class Baz { public ulong xyzzy; }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(bXml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bFileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "B.cs");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(bFileunit));

            Assert.AreEqual(2, afterScope.ChildStatements.OfType<NamespaceDefinition>().Count());

            afterScope.RemoveFile("B.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
        public void TestClassWithDeclaredVariable() {
            string xml = @"class A {
                 int a;
             };";

            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.h", Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.h");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            Assert.IsTrue(globalScope.IsGlobal);

            var classA = globalScope.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(classA);
            Assert.AreEqual("A", classA.Name);
            Assert.AreEqual(1, classA.ChildStatements.Count);

            var fieldStmt = classA.ChildStatements.First();
            Assert.IsNotNull(fieldStmt);
            var field = fieldStmt.Content as VariableDeclaration;
            Assert.IsNotNull(field);
            Assert.AreEqual("a", field.Name);
            Assert.AreEqual("int", field.VariableType.Name);
        }
        public void TestGetNamedChildren_TryStatement(Language lang) {
            string xml = @"int foo = 17;
            try {
              int foo = 42;
              MethodCall(foo);
            } finally {
              MethodCall2(foo);
              int foo = 101;
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            Assert.AreEqual(2, globalScope.ChildStatements.Count);

            var tryStatement = globalScope.GetDescendants<TryStatement>().First();
            Assert.AreEqual(2, tryStatement.ChildStatements.Count);
            var tryFoo = tryStatement.ChildStatements[0].Content.GetDescendantsAndSelf<VariableDeclaration>().First(v => v.Name == "foo");
            var tryFooUse = tryStatement.ChildStatements[1].Content.GetDescendantsAndSelf<NameUse>().First(n => n.Name == "foo");

            Assert.AreEqual(2, tryStatement.FinallyStatements.Count);
            var finallyFooUse = tryStatement.FinallyStatements[0].Content.GetDescendantsAndSelf<NameUse>().First(n => n.Name == "foo");
            var finallyFoo = tryStatement.FinallyStatements[1].Content.GetDescendantsAndSelf<VariableDeclaration>().First(v => v.Name == "foo");

            var allChildren = tryStatement.GetNamedChildren("foo").ToList();
            Assert.AreEqual(2, allChildren.Count);
            Assert.AreSame(tryFoo, allChildren[0]);
            Assert.AreSame(finallyFoo, allChildren[1]);

            var tryMatches = tryStatement.GetNamedChildren(tryFooUse).ToList();
            Assert.AreEqual(1, tryMatches.Count);
            Assert.AreSame(tryFoo, tryMatches[0]);

            Assert.IsEmpty(tryStatement.GetNamedChildren(finallyFooUse).ToList());
        }
Exemple #57
0
        public void TestResolveLocalVariable_Parameter(Language lang) {
            string xml = @"int Foo(int num, bool option) {
              if(option) {
                printf(num);
              }
              return 0;
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "A.cpp", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "A.cpp");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var foo = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "Foo");
            Assert.AreEqual(2, foo.Parameters.Count);
            var numDecl = foo.Parameters[0];
            Assert.IsNotNull(numDecl);
            var optionDecl = foo.Parameters[1];
            Assert.IsNotNull(optionDecl);

            var optionUse = foo.GetDescendants().SelectMany(s => s.GetExpressions()).SelectMany(e => e.GetDescendantsAndSelf<NameUse>()).FirstOrDefault(n => n.Name == "option");
            Assert.IsNotNull(optionUse);
            Assert.AreSame(optionDecl, optionUse.FindMatches().FirstOrDefault());

            var numUse = foo.GetDescendants().SelectMany(s => s.GetExpressions()).SelectMany(e => e.GetDescendantsAndSelf<NameUse>()).FirstOrDefault(n => n.Name == "num");
            Assert.IsNotNull(numUse);
            Assert.AreSame(numDecl, numUse.FindMatches().FirstOrDefault());
        }
Exemple #58
0
        public void TestResolveLocalVariable_ParentExpression(Language lang) {
            string xml = @"int Foo() {
              for(int i = 0; i < bar; i++) {
                printf(i);
              }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var forStmt = globalScope.GetDescendants<ForStatement>().First();
            Assert.AreEqual(1, forStmt.ChildStatements.Count());

            var iDecl = forStmt.Initializer.GetDescendantsAndSelf<VariableDeclaration>().FirstOrDefault(v => v.Name == "i");
            Assert.IsNotNull(iDecl);
            var iUse = forStmt.ChildStatements[0].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(n => n.Name == "i");
            Assert.IsNotNull(iUse);
            Assert.AreSame(iDecl, iUse.FindMatches().FirstOrDefault());
        }
Exemple #59
0
        public void TestResolveLocalVariable(Language lang) {
            string xml = @"int Foo() {
              if(MethodCall()) {
                int bar = 17;
                bar = 42;
              }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            var ifStmt = globalScope.GetDescendants<IfStatement>().First();
            Assert.AreEqual(2, ifStmt.ChildStatements.Count());

            var barDecl = ifStmt.ChildStatements[0].Content.GetDescendantsAndSelf<VariableDeclaration>().FirstOrDefault(v => v.Name == "bar");
            Assert.IsNotNull(barDecl);
            var barUse = ifStmt.ChildStatements[1].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(n => n.Name == "bar");
            Assert.IsNotNull(barUse);
            Assert.AreSame(barDecl, barUse.FindMatches().FirstOrDefault());
        }
Exemple #60
0
        public void TestGetChildren_Expressions(Language lang) {
            string xml = @"Foo f = (bar + baz(qux(17))).Xyzzy();";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "test.code", lang, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup[lang].GetFileUnitForXmlSnippet(srcML, "test.code");

            var globalScope = codeParsers[lang].ParseFileUnit(xmlElement);
            Assert.AreEqual(1, globalScope.ChildStatements.Count);
            Assert.AreEqual(3, globalScope.ChildStatements[0].Content.GetDescendantsAndSelf<MethodCall>().Count());
        }