Beispiel #1
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);
        }
        public void TestRemoveClass_Namespace()
        {
            ////Foo.java
            //package com.ABB.Example;
            //class Foo {
            //    private int bar;
            //    public Foo() { bar = 42; }
            //    public int GetBar() { return bar; }
            //}
            string fooXml      = @"<package>package <name>com</name>.<name>ABB</name>.<name>Example</name>;</package>
<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
            //package com.ABB.Example;
            //class Baz {
            //    public static int DoWork() { return 0; }
            //}
            string bazXml      = @"<decl_stmt><decl><type><name>package</name></type> <name><name>com</name><op:operator>.</op:operator><name>ABB</name><op:operator>.</op:operator><name>Example</name></name></decl>;</decl_stmt>
<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(1, afterScope.ChildStatements.OfType <NamespaceDefinition>().Count());

            afterScope.RemoveFile("Baz.java");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Beispiel #3
0
        public void TestRemovePartialMethod_Implementation()
        {
            ////A1.cs
            //public partial class A {
            //    public partial int Foo();
            //}
            string a1Xml       = @"<class><specifier>public</specifier> <specifier>partial</specifier> class <name>A</name> <block>{
    <function_decl><type><specifier>public</specifier> <specifier>partial</specifier> <name>int</name></type> <name>Foo</name><parameter_list>()</parameter_list>;</function_decl>
}</block></class>";
            var    a1FileUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(a1Xml, "A1.cs");
            var    beforeScope = CodeParser.ParseFileUnit(a1FileUnit);
            ////A2.cs
            //public partial class A {
            //    public partial int Foo() { return 42; }
            //}
            string a2Xml      = @"<class><specifier>public</specifier> <specifier>partial</specifier> class <name>A</name> <block>{
    <function><type><specifier>public</specifier> <specifier>partial</specifier> <name>int</name></type> <name>Foo</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><lit:literal type=""number"">42</lit:literal></expr>;</return> }</block></function>
}</block></class>";
            var    a2FileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(a2Xml, "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(1, typeA.ChildStatements.OfType <MethodDefinition>().Count());
            var foo = typeA.ChildStatements.First() as MethodDefinition;

            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);

            afterScope.RemoveFile("A2.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
        public void TestRemovePartOfNamespace()
        {
            ////A1.cpp
            //namespace A {
            //	int Foo(){ return 0;}
            //}
            string a1Xml       = @"<namespace>namespace <name>A</name> <block>{
    <function><type><name>int</name></type> <name>Foo</name><parameter_list>()</parameter_list><block>{ <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>}</block></function>
}</block></namespace>";
            var    a1FileUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(a1Xml, "A1.cpp");
            var    beforeScope = CodeParser.ParseFileUnit(a1FileUnit);

            ////A2.cpp
            //namespace A {
            //    char* Bar(){return "Hello, World!";}
            //}
            string a2Xml      = @"<namespace>namespace <name>A</name> <block>{
    <function><type><name>char</name><type:modifier>*</type:modifier></type> <name>Bar</name><parameter_list>()</parameter_list><block>{<return>return <expr><lit:literal type=""string"">""Hello, World!""</lit:literal></expr>;</return>}</block></function>
}</block></namespace>";
            var    a2Fileunit = FileUnitSetup.GetFileUnitForXmlSnippet(a2Xml, "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);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public void TestRemoveNamespace()
        {
            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.DifferentExample;
            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));

            var comDotAbb = afterScope.ChildStatements.OfType <NamespaceDefinition>().First().ChildStatements.OfType <NamespaceDefinition>().First();

            Assert.AreEqual("com.ABB", comDotAbb.GetFullName());
            Assert.AreEqual(2, comDotAbb.ChildStatements.OfType <NamespaceDefinition>().Count());

            afterScope.RemoveFile("Baz.java");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Beispiel #8
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);
        }
Beispiel #9
0
        public void TestRemoveNamespace()
        {
            ////A.cs
            //namespace A {
            //    class Foo { int bar; }
            //}
            string aXml        = @"<namespace>namespace <name>A</name> <block>{
    <class>class <name>Foo</name> <block>{ <decl_stmt><decl><type><name>int</name></type> <name>bar</name></decl>;</decl_stmt> }</block></class>
}</block></namespace>";
            var    aFileunit   = FileUnitSetup.GetFileUnitForXmlSnippet(aXml, "A.cs");
            var    beforeScope = CodeParser.ParseFileUnit(aFileunit);
            ////B.cs
            //namespace B {
            //    class Baz { public ulong xyzzy; }
            //}
            string bXml       = @"<namespace>namespace <name>B</name> <block>{
    <class>class <name>Baz</name> <block>{ <decl_stmt><decl><type><specifier>public</specifier> <name>ulong</name></type> <name>xyzzy</name></decl>;</decl_stmt> }</block></class>
}</block></namespace>";
            var    bFileunit  = FileUnitSetup.GetFileUnitForXmlSnippet(bXml, "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);
        }
Beispiel #10
0
        public void TestRoundTripWithDefaultExtension(string sourceFileName, bool useCompression)
        {
            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;

            string outputFileName = Path.ChangeExtension(DefaultOutputName, useCompression ? XmlSerialization.DEFAULT_COMPRESSED_EXTENSION : XmlSerialization.DEFAULT_EXTENSION);

            XmlSerialization.WriteElement(nsd, outputFileName);
            var nsdFromFile = XmlSerialization.Load(outputFileName) as NamespaceDefinition;

            DataAssert.StatementsAreEqual(nsd, nsdFromFile);
        }
        public void TestRemoveMethodFromClass()
        {
            ////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");

            ////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 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);
        }
Beispiel #12
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);
        }
        public void TestHeaderRemovalWithNamespaceAndClass()
        {
            //Foo.h
            //namespace A {
            //	class Foo {
            //		public:
            //			int Bar(int b);
            //	};
            //}
            string hXml = @"<namespace>namespace <name>A</name> <block>{
	<class>class <name>Foo</name> <block>{<private type=""default"">
		</private><public>public:
			<function_decl><type><name>int</name></type> <name>Bar</name><parameter_list>(<param><decl><type><name>int</name></type> <name>b</name></decl></param>)</parameter_list>;</function_decl>
	</public>}</block>;</class>
}</block></namespace>";

            //Foo.cpp
            //int A::Foo::Bar(int b) { }
            string cppXml = @"<function><type><name>int</name></type> <name><name>A</name><op:operator>::</op:operator><name>Foo</name><op:operator>::</op:operator><name>Bar</name></name><parameter_list>(<param><decl><type><name>int</name></type> <name>b</name></decl></param>)</parameter_list> <block>{ }</block></function>";

            var hFileUnit   = FileUnitSetup.GetFileUnitForXmlSnippet(hXml, "Foo.h");
            var cppFileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(cppXml, "Foo.cpp");

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

            afterScope.RemoveFile("Foo.h");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Beispiel #14
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);
        }
Beispiel #15
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);
        }
        public void TestRoundTrip(string sourceFileName, bool compressOutput)
        {
            var sourceFilePath = Path.Combine(TestInputPath, sourceFileName);

            var srcMLGenerator = new SrcMLGenerator("SrcML");
            var dataGenerator  = new DataGenerator();

            Assert.That(srcMLGenerator.Generate(sourceFilePath, DefaultInputName));
            var fileUnit = SrcMLElement.Load(DefaultInputName);
            var nsd      = dataGenerator.Parse(fileUnit) as NamespaceDefinition;

            XmlSerialization.WriteElement(nsd, DefaultOutputName, compressOutput);
            var nsdFromFile = XmlSerialization.Load(DefaultOutputName, compressOutput) as NamespaceDefinition;

            DataAssert.StatementsAreEqual(nsd, nsdFromFile);
        }
        public void TestRoundTripWithDefaultExtension(string sourceFileName, bool useCompression)
        {
            var sourceFilePath = Path.Combine(TestInputPath, sourceFileName);

            var srcMLGenerator = new SrcMLGenerator("SrcML");
            var dataGenerator  = new DataGenerator();

            Assert.That(srcMLGenerator.Generate(sourceFilePath, DefaultInputName));
            var fileUnit = SrcMLElement.Load(DefaultInputName);
            var nsd      = dataGenerator.Parse(fileUnit) as NamespaceDefinition;

            string outputFileName = Path.ChangeExtension(DefaultOutputName, useCompression ? XmlSerialization.DEFAULT_COMPRESSED_EXTENSION : XmlSerialization.DEFAULT_EXTENSION);

            XmlSerialization.WriteElement(nsd, outputFileName);
            var nsdFromFile = XmlSerialization.Load(outputFileName) as NamespaceDefinition;

            DataAssert.StatementsAreEqual(nsd, nsdFromFile);
        }
Beispiel #18
0
        public void TestTestHelper()
        {
            string         xml    = @"class Foo {
              public:
                int a;
                int Add(int b);
            };";
            LibSrcMLRunner runA   = new LibSrcMLRunner();
            string         srcMLA = runA.GenerateSrcMLFromString(xml, "A.h", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);

            var fileunit = FileUnitSetup.GetFileUnitForXmlSnippet(srcMLA, "A.h");
            var scope1   = CodeParser.ParseFileUnit(fileunit);
            var scope2   = CodeParser.ParseFileUnit(fileunit);

            DataAssert.StatementsAreEqual(scope1, scope2);
        }
        public void TestTestHelper()
        {
            ////A.h
            //class Foo {
            //  public:
            //    int a;
            //    int Add(int b);
            //};
            string xml      = @"<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    fileunit = FileUnitSetup.GetFileUnitForXmlSnippet(xml, "A.h");
            var    scope1   = CodeParser.ParseFileUnit(fileunit);
            var    scope2   = CodeParser.ParseFileUnit(fileunit);

            DataAssert.StatementsAreEqual(scope1, scope2);
        }
        public void TestRemoveMethodFromGlobal()
        {
            ////Foo.cpp
            //int Foo() { return 0; }
            string fooXml      = @"<function><type><name>int</name></type> <name>Foo</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return> }</block></function>";
            var    fileunitFoo = FileUnitSetup.GetFileUnitForXmlSnippet(fooXml, "Foo.cpp");
            var    beforeScope = CodeParser.ParseFileUnit(fileunitFoo);

            ////Baz.cpp
            //char* Baz() { return "Hello, World!"; }
            string bazXml      = "<function><type><name>char</name><type:modifier>*</type:modifier></type> <name>Baz</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><lit:literal type=\"string\">\"Hello, World!\"</lit:literal></expr>;</return> }</block></function>";
            var    fileunitBaz = FileUnitSetup.GetFileUnitForXmlSnippet(bazXml, "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);
        }
Beispiel #21
0
        public void TestRemovePartialClass()
        {
            ////A1.cs
            //public partial class A {
            //    public int Execute() {
            //        return 0;
            //    }
            //}
            string a1Xml       = @"<class><specifier>public</specifier> <specifier>partial</specifier> class <name>A</name> <block>{
    <function><type><specifier>public</specifier> <name>int</name></type> <name>Execute</name><parameter_list>()</parameter_list> <block>{
        <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>
    }</block></function>
}</block></class>";
            var    a1FileUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(a1Xml, "A1.cs");
            var    beforeScope = CodeParser.ParseFileUnit(a1FileUnit);
            ////A2.cs
            //public partial class A {
            //    private bool Foo() {
            //        return true;
            //    }
            //}
            string a2Xml      = @"<class><specifier>public</specifier> <specifier>partial</specifier> class <name>A</name> <block>{
    <function><type><specifier>private</specifier> <name>bool</name></type> <name>Foo</name><parameter_list>()</parameter_list> <block>{
        <return>return <expr><lit:literal type=""boolean"">true</lit:literal></expr>;</return>
    }</block></function>
}</block></class>";
            var    a2FileUnit = FileUnitSetup.GetFileUnitForXmlSnippet(a2Xml, "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);
        }
        public void TestRemoveMethodDeclaration_Global()
        {
            ////Foo.cpp
            //int Foo(char bar) { return 0; }
            string defXml      = "<function><type><name>int</name></type> <name>Foo</name><parameter_list>(<param><decl><type><name>char</name></type> <name>bar</name></decl></param>)</parameter_list> <block>{ <return>return <expr><lit:literal type=\"number\">0</lit:literal></expr>;</return> }</block></function>";
            var    fileUnitDef = FileUnitSetup.GetFileUnitForXmlSnippet(defXml, "Foo.cpp");
            var    beforeScope = CodeParser.ParseFileUnit(fileUnitDef);

            ////Foo.h
            //int Foo(char bar);
            string declXml      = "<function_decl><type><name>int</name></type> <name>Foo</name><parameter_list>(<param><decl><type><name>char</name></type> <name>bar</name></decl></param>)</parameter_list>;</function_decl>";
            var    fileunitDecl = FileUnitSetup.GetFileUnitForXmlSnippet(declXml, "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);
        }
Beispiel #23
0
        public void TestRemovePartialMethod_Declaration()
        {
            string         a2Xml = @"public partial class A {
              public partial int Foo() { return 42; }
            }";
            LibSrcMLRunner run   = new LibSrcMLRunner();
            string         srcML = run.GenerateSrcMLFromString(a2Xml, "A2.cs", Language.CSharp, new Collection <UInt32>()
            {
                LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION
            }, false);
            var a2FileUnit  = FileUnitSetup.GetFileUnitForXmlSnippet(srcML, "A2.cs");
            var beforeScope = CodeParser.ParseFileUnit(a2FileUnit);

            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.GetFileUnitForXmlSnippet(srcMLA, "A1.cs");
            var afterScope = beforeScope.Merge(CodeParser.ParseFileUnit(a1FileUnit));

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

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

            Assert.That(foo.IsPartial);

            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);

            afterScope.RemoveFile("A1.cs");

            DataAssert.StatementsAreEqual(beforeScope, afterScope);
        }
Beispiel #24
0
        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);
        }
Beispiel #25
0
        public void TestSerialization(RealWorldTestProject testData)
        {
            using (var project = new DataProject <NullWorkingSet>(testData.DataDirectory, testData.FullPath, "SrcML")) {
                string unknownLogPath = Path.Combine(project.StoragePath, "unknown.log");

                using (var unknownLog = new StreamWriter(unknownLogPath)) {
                    project.UnknownLog = unknownLog;
                    project.UpdateAsync().Wait();

                    long       count  = 0;
                    TextWriter output = StreamWriter.Synchronized(Console.Out),
                               error  = StreamWriter.Synchronized(Console.Error);

                    long parseElapsed = 0, deserializedElapsed = 0, compareElapsed = 0;
                    output.WriteLine("{0,-12} {1,-12} {2,-12} {3,-12}", "# Files", "Parse", "Deserialize", "Comparison");
                    Parallel.ForEach(project.Data.GetFiles(), (sourcePath) => {
                        DateTime start, end;
                        NamespaceDefinition data;
                        NamespaceDefinition serializedData;
                        try {
                            start        = DateTime.Now;
                            var fileUnit = project.SourceArchive.GetXElementForSourceFile(sourcePath);
                            data         = project.Data.Generator.Parse(fileUnit);
                            end          = DateTime.Now;
                            Interlocked.Add(ref parseElapsed, (end - start).Ticks);
                        } catch (Exception ex) {
                            Console.Error.WriteLine(ex);
                            data = null;
                        }

                        try {
                            start          = DateTime.Now;
                            serializedData = project.Data.GetData(sourcePath);
                            end            = DateTime.Now;
                            Interlocked.Add(ref deserializedElapsed, (end - start).Ticks);
                        } catch (Exception ex) {
                            error.WriteLine(ex);
                            serializedData = null;
                        }

                        Assert.IsNotNull(data);
                        Assert.IsNotNull(serializedData);
                        start = DateTime.Now;
                        DataAssert.StatementsAreEqual(data, serializedData);
                        end = DateTime.Now;
                        Interlocked.Add(ref compareElapsed, (end - start).Ticks);

                        if (Interlocked.Increment(ref count) % 25 == 0)
                        {
                            output.WriteLine("{0,12:N0} {1,12:ss\\.fff} {2,12:ss\\.fff} {3,12:ss\\.fff}", count,
                                             new TimeSpan(parseElapsed),
                                             new TimeSpan(deserializedElapsed),
                                             new TimeSpan(compareElapsed));
                        }
                    });

                    Console.WriteLine("Project: {0} {1}", testData.ProjectName, testData.Version);
                    Console.WriteLine("{0,-15} {1,11:N0}", "# Files", count);
                    Console.WriteLine("{0,-15} {1:g}", "Parsing", new TimeSpan(parseElapsed));
                    Console.WriteLine("{0,-15} {1:g}", "Deserializing", new TimeSpan(deserializedElapsed));
                    Console.WriteLine("{0,-15} {1:g}", "Comparing", new TimeSpan(compareElapsed));
                    Console.WriteLine("{0,-15} {1:g}", "Total", new TimeSpan(parseElapsed + deserializedElapsed + compareElapsed));
                }
            }
        }