public void TestContains_NoSibling()
        {
            ////Foo.cs
            //class Foo {
            //    int Bar(){return 0;}
            //}
            var xml           = @"<class pos:line=""1"" pos:column=""1"">class <name pos:line=""1"" pos:column=""7"">Foo</name> <block pos:line=""1"" pos:column=""11"">{
    <function><type><name pos:line=""2"" pos:column=""5"">int</name></type> <name pos:line=""2"" pos:column=""9"">Bar</name><parameter_list pos:line=""2"" pos:column=""12"">()</parameter_list><block pos:line=""2"" pos:column=""14"">{<return pos:line=""2"" pos:column=""15"">return <expr><lit:literal type=""number"" pos:line=""2"" pos:column=""22"">0</lit:literal></expr>;</return>}</block></function>
}</block></class>";
            var classElement  = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "Foo.cs").Descendants(SRC.Class).First();
            var methodElement = classElement.Descendants(SRC.Function).First();
            var classLoc      = new SrcMLLocation(classElement, "Foo.cs");
            var methodLoc     = new SrcMLLocation(methodElement, "Foo.cs");

            Assert.IsTrue(classLoc.Contains(methodLoc));
        }
Exemplo n.º 2
0
        public void TestContains_NoSibling()
        {
            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 methodElement = classElement.Descendants(SRC.Function).First();
            var classLoc      = new SrcMLLocation(classElement, "Foo.cs");
            var methodLoc     = new SrcMLLocation(methodElement, "Foo.cs");

            Assert.IsTrue(classLoc.Contains(methodLoc));
        }
        public void TestRemoveClass_Global()
        {
            ////Foo.java
            //class Foo {
            //    private int bar;
            //    public Foo() { bar = 42; }
            //    public int GetBar() { return bar; }
            //}
            string fooXml      = @"<class>class <name>Foo</name> <block>{
    <decl_stmt><decl><type><specifier>private</specifier> <name>int</name></type> <name>bar</name></decl>;</decl_stmt>
    <constructor><specifier>public</specifier> <name>Foo</name><parameter_list>()</parameter_list> <block>{ <expr_stmt><expr><name>bar</name> <op:operator>=</op:operator> <lit:literal type=""number"">42</lit:literal></expr>;</expr_stmt> }</block></constructor>
    <function><type><specifier>public</specifier> <name>int</name></type> <name>GetBar</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><name>bar</name></expr>;</return> }</block></function>
}</block></class>";
            var    fooFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(fooXml, "Foo.java");
            var    beforeScope = CodeParser.ParseFileUnit(fooFileUnit);
            ////Baz.java
            //class Baz {
            //    public static int DoWork() { return 0; }
            //}
            string bazXml      = @"<class>class <name>Baz</name> <block>{
    <function><type><specifier>public</specifier> <specifier>static</specifier> <name>int</name></type> <name>DoWork</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return> }</block></function>
}</block></class>";
            var    bazFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(bazXml, "Baz.java");
            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.java");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemplo n.º 4
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.java", Language.Java, new Collection <UInt32>()
            {
            }, false);
            var fooFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "Foo.java");
            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.java", Language.Java, new Collection <UInt32>()
            {
            }, false);
            var bazFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "Baz.java");
            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.java");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Exemplo n.º 5
0
        public void TestCreateAliasesForFiles_ImportClass()
        {
            //import x.y.z;
            string xml = @"<import>import <name>x</name>.<name>y</name>.<name>z</name>;</import>";

            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(xml, "A.java");

            var actual = codeParser.ParseAliasElement(xmlElement.Element(SRC.Import), new ParserContext(xmlElement));

            Assert.AreEqual("x", actual.ImportedNamespace.Name);
            Assert.AreEqual("y", actual.ImportedNamespace.ChildScopeUse.Name);
            Assert.IsNull(actual.ImportedNamespace.ChildScopeUse.ChildScopeUse);

            Assert.AreEqual("z", actual.ImportedNamedScope.Name);
            Assert.IsFalse(actual.IsNamespaceImport);
        }
        public void TestClassWithDeclaredVariable()
        {
            //class A {
            //    int a;
            //};
            string xml = @"<class>class <name>A</name> <block>{<private type=""default"">
    <decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt>
</private>}</block>;</class>";

            var globalScope = codeParser.ParseFileUnit(fileSetup.GetFileUnitForXmlSnippet(xml, "A.h"));

            var classA = globalScope.ChildScopes.First() as ITypeDefinition;

            Assert.AreEqual("A", classA.Name);
            Assert.AreEqual(1, classA.DeclaredVariables.Count());
        }
        public void TestCreateAliasesForFiles_ImportClass()
        {
            //import x.y.z;
            string   xml        = @"<import>import <name><name>x</name><op:operator>.</op:operator><name>y</name><op:operator>.</op:operator><name>z</name></name>;</import>";
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(xml, "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());
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        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 TestRemoveClassDefinition()
        {
            ////A.cpp
            //int Foo::Add(int b) {
            //  return this->a + b;
            //}
            string cppXml      = @"<function><type><name>int</name></type> <name><name>Foo</name><op:operator>::</op:operator><name>Add</name></name><parameter_list>(<param><decl><type><name>int</name></type> <name>b</name></decl></param>)</parameter_list> <block>{
  <return>return <expr><name>this</name><op:operator>-&gt;</op:operator><name>a</name> <op:operator>+</op:operator> <name>b</name></expr>;</return>
}</block></function>";
            var    cppFileunit = FileUnitSetup.GetFileUnitForXmlSnippet(cppXml, "A.cpp");
            var    beforeScope = CodeParser.ParseFileUnit(cppFileunit);
            ////A.h
            //class Foo {
            //  public:
            //    int a;
            //    int Add(int b);
            //};
            string hXml       = @"<class>class <name>Foo</name> <block>{<private type=""default"">
  </private><public>public:
    <decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt>
    <function_decl><type><name>int</name></type> <name>Add</name><parameter_list>(<param><decl><type><name>int</name></type> <name>b</name></decl></param>)</parameter_list>;</function_decl>
</public>}</block>;</class>";
            var    hFileunit  = FileUnitSetup.GetFileUnitForXmlSnippet(hXml, "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);
        }