public void TestCallToGenericMethod() {
            //namespace A {
            //    public class B {
            //        void Foo<T>(T t) { }
            //        void Bar() { Foo(this); }
            //    }
            //}
            var code = @"namespace A {
    public class B {
        void Foo<T>(T t) { }
        void Bar() { Foo(this); }
    }
}";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(code, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            var unit = fileSetup.GetFileUnitForXmlSnippet(srcML, "A.cs");
            var globalScope = codeParser.ParseFileUnit(unit);

            var foo = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(md => md.Name == "Foo");
            var bar = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(md => md.Name == "Bar");
            Assert.IsNotNull(foo);
            Assert.IsNotNull(bar);

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

            Assert.AreSame(foo, callToFoo.FindMatches().FirstOrDefault());
        }
示例#2
0
        public string ApplyXPathToSrcMLString(string srcMLBuff, string xPathFile)
        {
            //Function sould take a srcML buffer string and then pass it into C++ to apply xsltFile to it.
            //C++ will return buffer with transformed srcML
            IntPtr buff   = LibSrcMLRunner.SrcMLApplyXPathMtM(Marshal.StringToHGlobalAnsi(srcMLBuff), Marshal.StringToHGlobalAnsi(xPathFile));
            IntPtr docptr = Marshal.ReadIntPtr(buff);
            string docstr = Marshal.PtrToStringAnsi(docptr);

            return(docstr);
        }
示例#3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="srcMLFile"></param>
 /// <param name="xsltFile"></param>
 /// <param name="outputFile"></param>
 /// <returns></returns>
 public int ApplyXsltToSrcMLFile(string srcMLFile, string xsltFile, string outputFile)
 {
     if (File.Exists(srcMLFile) && File.Exists(xsltFile))
     {
         LibSrcMLRunner.SrcMLApplyXsltFtF(Marshal.StringToHGlobalAnsi(srcMLFile), Marshal.StringToHGlobalAnsi(xsltFile), Marshal.StringToHGlobalAnsi(outputFile));
     }
     return(0);
     //Function should take a srcML file that as xsltFile applied to it within the C++ module. Nothing is returned
     //As the results are stored in outputFile
 }
        public void TestNamespace() {
            //namespace A { 
            //  public class foo { }
            //}
            var xml = @"namespace A { 
                public class foo { }
            }";
            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcML = run.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            var unit = fileSetup.GetFileUnitForXmlSnippet(srcML, "A.cs");
            var globalScope = codeParser.ParseFileUnit(unit);
            Assert.IsTrue(globalScope.IsGlobal);

            var actual = globalScope.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(actual);
            Assert.AreEqual("A", actual.Name);
            Assert.AreEqual(1, actual.ChildStatements.Count);
        }
        public void TestUsingBlock_Expression() {
            //using(bar = new Foo()) { ; }
            string xml = @"using(bar = new Foo()) { ; }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

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

            var actual = globalScope.ChildStatements[0] as UsingBlockStatement;
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.ChildStatements.Count);
            var init = actual.Initializer;
            Assert.IsNotNull(actual.Initializer);
            Assert.AreEqual(4, init.Components.Count);
            var bar = init.Components[0] as NameUse;
            Assert.IsNotNull(bar);
            Assert.AreEqual("bar", bar.Name);
            var equals = init.Components[1] as OperatorUse;
            Assert.IsNotNull(equals);
            Assert.AreEqual("=", equals.Text);
            var newOp = init.Components[2] as OperatorUse;
            Assert.IsNotNull(newOp);
            Assert.AreEqual("new", newOp.Text);
            var foo = init.Components[3] as MethodCall;
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual(0, foo.Arguments.Count);
        }
        public void TestVariableDeclaredInCallingObjectWithParentClass() {
            //class A { B b; }
            string axml = @"class A { B b; }";

            //class B { void Foo() { } }
            string bxml = @"class B { void Foo() { } }";

            //class C : A { }
            string cxml = @"class C : A { }";

            //class D {
            //  C c;
            //  void Bar() { c.b.Foo(); }
            //}
            string dxml = @"class D {
                C c;
                void Bar() { c.b.Foo(); }
            }";

            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(axml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var aUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(bxml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");
            LibSrcMLRunner runC = new LibSrcMLRunner();
            string srcMLC = runC.GenerateSrcMLFromString(cxml, "C.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var cUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLC, "C.cs");
            LibSrcMLRunner runD = new LibSrcMLRunner();
            string srcMLD = runD.GenerateSrcMLFromString(dxml, "D.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var dUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLD, "D.cs");

            var globalScope = codeParser.ParseFileUnit(aUnit);
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(bUnit));
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(cUnit));
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(dUnit));

            var typeA = globalScope.GetNamedChildren<TypeDefinition>("A").FirstOrDefault();
            var typeB = globalScope.GetNamedChildren<TypeDefinition>("B").FirstOrDefault();
            var typeC = globalScope.GetNamedChildren<TypeDefinition>("C").FirstOrDefault();
            var typeD = globalScope.GetNamedChildren<TypeDefinition>("D").FirstOrDefault();

            Assert.IsNotNull(typeA, "could not find class A");
            Assert.IsNotNull(typeB, "could not find class B");
            Assert.IsNotNull(typeC, "could not find class C");
            Assert.IsNotNull(typeD, "could not find class D");

            var adotB = typeA.GetNamedChildren<VariableDeclaration>("b").FirstOrDefault();
            Assert.IsNotNull(adotB, "could not find variable A.b");
            Assert.AreEqual("b", adotB.Name);

            var bDotFoo = typeB.GetNamedChildren<MethodDefinition>("Foo").FirstOrDefault();
            Assert.IsNotNull(bDotFoo, "could not method B.Foo()");

            var dDotBar = typeD.GetNamedChildren<MethodDefinition>("Bar").FirstOrDefault();
            Assert.IsNotNull(dDotBar, "could not find method D.Bar()");

            var callToFoo = dDotBar.FindExpressions<MethodCall>(true).FirstOrDefault();
            Assert.IsNotNull(callToFoo, "could not find any method calls in D.Bar()");

            Assert.AreEqual(bDotFoo, callToFoo.FindMatches().FirstOrDefault());
        }
        public void TestTypeUseForOtherNamespace() {
            //namespace A.B {
            //    class C {
            //        int Foo() { }
            //    }
            //}
            string cxml = @"namespace A.B {
                class C {
                    int Foo() { }
                }
            }";

            //using A.B;
            //namespace D {
            //    class E {
            //        void main() {
            //            C c = new C();
            //            c.Foo();
            //        }
            //    }
            //}
            string exml = @"using A.B;
            namespace D {
                class E {
                    void main() {
                        C c = new C();
                        c.Foo();
                    }
                }
            }";

            LibSrcMLRunner run = new LibSrcMLRunner();
            string srcMLC = run.GenerateSrcMLFromString(cxml, "C.cpp", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var cUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLC, "C.cpp");
            LibSrcMLRunner runE = new LibSrcMLRunner();
            string srcMLE = runE.GenerateSrcMLFromString(exml, "E.cpp", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var eUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLE, "E.cpp");

            NamespaceDefinition globalScope = codeParser.ParseFileUnit(cUnit);
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(eUnit)) as NamespaceDefinition;

            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.ChildStatements.First() as MethodDefinition;
            Assert.IsNotNull(mainMethod, "is not a method definition");
            Assert.AreEqual("main", mainMethod.Name);

            var fooMethod = typeC.GetNamedChildren<MethodDefinition>("Foo").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());
        }
示例#8
0
        public void TestGenerateSrcMLFromStrings() {
            List<String> bufferList = new List<String>();
            List<String> fileList = new List<String>();

            String str = "int main(){int c; c = 0; ++c;}";
            String str2 = "int foo(){int c; c = 0; ++c;}";

            fileList.Add("input.cpp");
            fileList.Add("input2.cpp");
            bufferList.Add(str);
            bufferList.Add(str2);

            LibSrcMLRunner run = new LibSrcMLRunner();

            try {
                List<string> b = run.GenerateSrcMLFromStrings(bufferList, fileList, Language.CPlusPlus, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_MODIFIER }, false).ToList<string>();
                Assert.True(Convert.ToBoolean(b.Count));
                XDocument doc = XDocument.Parse(b.ElementAt(0));
                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);

                string file2 = "input2.cpp";
                var f2 = (from ele in units
                          where ele.Attribute("filename").Value == file2
                          select ele);
                Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value);
            }
            catch (SrcMLException e) {
                throw e;
            }
        }
        public void TestDeclarationWithTypeVarFromConstructor() {
            // B.cs namespace A { class B { public B() { }; } }
            string bxml = @"namespace A { class B { public B() { }; } }";
            // C.cs namespace A { class C { void main() { var b = new B(); } } }
            string cxml = @"namespace A { class C { void main() { var b = new B(); } } }";

            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(bxml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");
            LibSrcMLRunner runC = new LibSrcMLRunner();
            string srcMLC = runC.GenerateSrcMLFromString(cxml, "C.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var cUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLC, "C.cs");
            var bScope = codeParser.ParseFileUnit(bUnit);
            var cScope = codeParser.ParseFileUnit(cUnit);
            var globalScope = bScope.Merge(cScope);

            var typeB = globalScope.GetDescendants<TypeDefinition>().FirstOrDefault(t => t.Name == "B");
            Assert.IsNotNull(typeB);
            var main = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(m => m.Name == "main");
            Assert.IsNotNull(main);

            Assert.AreEqual(1, main.ChildStatements.Count);
            var varDecl = main.ChildStatements[0].Content.GetDescendantsAndSelf<VariableDeclaration>().FirstOrDefault();
            Assert.IsNotNull(varDecl);

            Assert.AreSame(typeB, varDecl.ResolveType().FirstOrDefault());
        }
示例#10
0
        public void TestCreateTypeDefinition_Struct() {
            ////Foo.cs
            //public struct Foo {
            //    public int bar;
            //}
            string fooXml = @"public struct Foo {
                public int bar;
            }";
            LibSrcMLRunner runo = new LibSrcMLRunner();
            string srcMLo = runo.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fooFileUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLo, "Foo.cs");
            var globalScope = codeParser.ParseFileUnit(fooFileUnit);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            var foo = globalScope.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual(TypeKind.Struct, foo.Kind);
            Assert.AreEqual(1, foo.ChildStatements.Count());
        }
示例#11
0
        public void TestCreateTypeDefinition_NestedCompoundNamespace() {
            ////Foo.cs
            //namespace Watermelon {
            //    namespace Example.Level2.Level3 {
            //        public class Foo {
            //            public int bar;
            //        }
            //    }
            //}
            string fooXml = @"namespace Watermelon {
                namespace Example.Level2.Level3 {
                    public class Foo {
                        public int bar;
                    }
                }
            }";
            LibSrcMLRunner runo = new LibSrcMLRunner();
            string srcMLo = runo.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fooFileUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLo, "Foo.cs");
            var globalScope = codeParser.ParseFileUnit(fooFileUnit);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            var watermelon = globalScope.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(watermelon);
            Assert.AreEqual("Watermelon", watermelon.Name);
            Assert.AreEqual(1, watermelon.ChildStatements.Count());
            var example = watermelon.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(example);
            Assert.AreEqual("Example", example.Name);
            Assert.AreEqual(1, example.ChildStatements.Count());
            var level2 = example.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(level2);
            Assert.AreEqual("Level2", level2.Name);
            Assert.AreEqual(1, level2.ChildStatements.Count());
            var level3 = level2.ChildStatements.First() as NamespaceDefinition;
            Assert.IsNotNull(level3);
            Assert.AreEqual("Level3", level3.Name);
            Assert.AreEqual(1, level3.ChildStatements.Count());
            var foo = level3.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual(1, foo.ChildStatements.Count());
        }
示例#12
0
        public void TestGetImports() {
            //B.cs
            //namespace x.y.z {}
            string xmlB = @"namespace x.y.z {}";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(xmlB, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementB = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");
            //A.cs
            //using x.y.z;
            //foo = 17;
            string xmlA = @"using x.y.z;
foo = 17;";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xmlA, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementA = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

            var scopeA = codeParser.ParseFileUnit(xmlElementA);
            var scopeB = codeParser.ParseFileUnit(xmlElementB);
            var globalScope = scopeA.Merge(scopeB);
            Assert.AreEqual(3, globalScope.ChildStatements.Count);
            var foo = globalScope.ChildStatements[1].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(n => n.Name == "foo");
            Assert.IsNotNull(foo);
            var imports = foo.GetImports().ToList();
            Assert.AreEqual(1, imports.Count);
            Assert.AreEqual("x . y . z", imports[0].ImportedNamespace.ToString());

            var nsd = globalScope.GetDescendants<NamespaceDefinition>().FirstOrDefault(ns => ns.Name == "z");
            Assert.IsNotNull(nsd);
            var zUse = imports[0].ImportedNamespace.GetDescendantsAndSelf<NameUse>().LastOrDefault();
            Assert.IsNotNull(zUse);
            Assert.AreEqual("z", zUse.Name);
            Assert.AreSame(nsd, zUse.FindMatches().First());
        }
示例#13
0
        public void TestCreateAliasesForFiles_UsingAlias() {
            // using x = Foo.Bar.Baz;
            string xml = @"using x = Foo.Bar.Baz;";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

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

            var actual = globalScope.ChildStatements[0] as AliasStatement;
            Assert.IsNotNull(actual);
            Assert.AreEqual("x", actual.AliasName);
            Assert.AreEqual("Foo . Bar . Baz", actual.Target.ToString());
        }
示例#14
0
        public void TestConstructorWithThisKeyword() {
            // B.cs
            //namespace A {
            //    class B {
            //        public B() : this(0) { }
            //        public B(int i) { }
            //    }
            //}

            string bxml = @"namespace A {
    class B {
        public B() : this(0) { }
        public B(int i) { }
    }
}";

            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(bxml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");

            var globalScope = codeParser.ParseFileUnit(bUnit);

            var oneArgumentConstructor = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "B" && m.Parameters.Count == 1);
            var defaultConstructor = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "B" && m.Parameters.Count == 0);
            Assert.AreEqual(1, defaultConstructor.ConstructorInitializers.Count);

            var methodCall = defaultConstructor.ConstructorInitializers[0];
            Assert.IsNotNull(methodCall);
            Assert.That(methodCall.IsConstructor);
            Assert.That(methodCall.IsConstructorInitializer);
            Assert.AreEqual("this", methodCall.Name);
            Assert.AreSame(oneArgumentConstructor, methodCall.FindMatches().FirstOrDefault());
        }
示例#15
0
        public void TestConstructorWithBaseKeyword() {
            // B.cs namespace A { class B { public B() { } } }
            string bxml = @"namespace A { class B { public B() { } } }";
            // C.cs namespace A { class C : B { public C() : base() { } } }
            string cxml = @"namespace A { class C : B { public C() : base() { } } }";

            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(bxml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");
            LibSrcMLRunner runC = new LibSrcMLRunner();
            string srcMLC = runC.GenerateSrcMLFromString(cxml, "C.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var cUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLC, "C.cs");

            var bScope = codeParser.ParseFileUnit(bUnit);
            var cScope = codeParser.ParseFileUnit(cUnit);
            var globalScope = bScope.Merge(cScope);

            var bConstructor = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "B" && m.IsConstructor);
            var cConstructor = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "C" && m.IsConstructor);
            Assert.AreEqual(1, cConstructor.ConstructorInitializers.Count);

            var methodCall = cConstructor.ConstructorInitializers[0];
            Assert.IsNotNull(methodCall);
            Assert.That(methodCall.IsConstructor);
            Assert.That(methodCall.IsConstructorInitializer);
            Assert.AreEqual("base", methodCall.Name);
            Assert.AreSame(bConstructor, methodCall.FindMatches().FirstOrDefault());
        }
示例#16
0
        public void TestCallConstructor() {
            //class Foo {
            //  public Foo() { }
            //}
            //class Bar {
            //  Foo myFoo = new Foo();
            //}
            string xml = @"class Foo {
                           public Foo() { }
                         }
                         class Bar {
                           Foo myFoo = new Foo();
                         }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

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

            var fooConstructor = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(m => m.Name == "Foo");
            Assert.IsNotNull(fooConstructor);
            var fooCall = globalScope.ChildStatements[1].ChildStatements[0].Content.GetDescendantsAndSelf<MethodCall>().FirstOrDefault(mc => mc.Name == "Foo");
            Assert.IsNotNull(fooCall);
            Assert.AreSame(fooConstructor, fooCall.FindMatches().First());
        }
示例#17
0
        public void TestLockStatement() {
            //lock(myVar) {
            //    myVar.DoFoo();
            //}
            string xml = @"lock(myVar) {
                myVar.DoFoo();
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            var globalScope = codeParser.ParseFileUnit(xmlElement);

            var lockStmt = globalScope.ChildStatements.First() as LockStatement;
            Assert.IsNotNull(lockStmt);
            Assert.AreEqual(1, lockStmt.ChildStatements.Count);

            var lockVar = lockStmt.LockExpression as NameUse;
            Assert.IsNotNull(lockVar);
            Assert.AreEqual("myVar", lockVar.Name);
        }
示例#18
0
        public void TestCreateTypeDefinition_Class() {
            ////Foo.cs
            //public class Foo {
            //    public int bar;
            //}
            string fooXml = @"public class Foo {
                public int bar;
            }";
            LibSrcMLRunner runo = new LibSrcMLRunner();
            string srcMLo = runo.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fooFileUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLo, "Foo.cs");
            var globalScope = codeParser.ParseFileUnit(fooFileUnit);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            var foo = globalScope.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual(TypeKind.Class, foo.Kind);
            Assert.AreEqual(1, foo.ChildStatements.Count());

            var bar = foo.ChildStatements[0].Content as VariableDeclaration;
            Assert.IsNotNull(bar);
            Assert.AreEqual("bar", bar.Name);
            Assert.AreEqual("int", bar.VariableType.Name);
            Assert.AreEqual(AccessModifier.Public, bar.Accessibility);
        }
示例#19
0
        public void TestCreateTypeDefinition_ClassWithQualifiedParent() {
            ////Foo.cs
            //public class Foo : Baz, System.IDisposable {
            //    public int bar;
            //}
            string fooXml = @"public class Foo : Baz, System.IDisposable {
                public int bar;
            }";
            LibSrcMLRunner runo = new LibSrcMLRunner();
            string srcMLo = runo.GenerateSrcMLFromString(fooXml, "Foo.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var fooFileUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLo, "Foo.cs");
            var globalScope = codeParser.ParseFileUnit(fooFileUnit);

            Assert.AreEqual(1, globalScope.ChildStatements.Count());
            var foo = globalScope.ChildStatements.First() as TypeDefinition;
            Assert.IsNotNull(foo);
            Assert.AreEqual("Foo", foo.Name);
            Assert.AreEqual(TypeKind.Class, foo.Kind);
            Assert.AreEqual(1, foo.ChildStatements.Count());
            Assert.AreEqual(2, foo.ParentTypeNames.Count);
            Assert.AreEqual("Baz", foo.ParentTypeNames[0].Name);
            Assert.AreEqual("IDisposable", foo.ParentTypeNames[1].Name);
            Assert.AreEqual("System", foo.ParentTypeNames[1].Prefix.Names.First().Name);
        }
示例#20
0
        public void TestGetImports_SeparateFiles() {
            //A.cs
            //using x.y.z;
            //Foo = 17;
            string xmlA = @"using x.y.z;
            Foo = 17;";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xmlA, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementA = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            //B.cs
            //using a.b.howdy;
            //Bar();
            string xmlB = @"using a.b.howdy;
            Bar();";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(xmlB, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementB = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");

            var scopeA = codeParser.ParseFileUnit(xmlElementA);
            var scopeB = codeParser.ParseFileUnit(xmlElementB);
            var globalScope = scopeA.Merge(scopeB);
            Assert.AreEqual(4, globalScope.ChildStatements.Count);

            var foo = globalScope.ChildStatements[1].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(nu => nu.Name == "Foo");
            Assert.IsNotNull(foo);
            var fooImports = foo.GetImports().ToList();
            Assert.AreEqual(1, fooImports.Count);
            Assert.AreEqual("x . y . z", fooImports[0].ImportedNamespace.ToString());

            var bar = globalScope.ChildStatements[3].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(nu => nu.Name == "Bar");
            Assert.IsNotNull(bar);
            var barImports = bar.GetImports().ToList();
            Assert.AreEqual(1, barImports.Count);
            Assert.AreEqual("a . b . howdy", barImports[0].ImportedNamespace.ToString());
        }
示例#21
0
        public void TestCallToGrandparent() {
            //namespace A {
            //    public class B { public void Foo() { } }
            //    public class C : B { }
            //    public class D : C { public void Bar() { Foo() } }
            //}
            var xml = @"namespace A {
    public class B { public void Foo() { } }
    public class C : B { }
    public class D : C { public void Bar() { Foo() } }
}";

            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var unit = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            var scope = codeParser.ParseFileUnit(unit);

            var bDotFoo = scope.GetDescendants<MethodDefinition>().FirstOrDefault(m => m.Name == "Foo");
            var dDotBar = scope.GetDescendants<MethodDefinition>().FirstOrDefault(m => m.Name == "Bar");
            Assert.IsNotNull(bDotFoo);
            Assert.IsNotNull(dDotBar);

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

            Assert.AreSame(bDotFoo, callToFoo.FindMatches().FirstOrDefault());
        }
示例#22
0
        public void TestGetAliases_NestedUsingAlias() {
            //A.cs
            //namespace bar {
            //  class baz {}
            //}
            string xmlA = @"namespace bar {
              class baz {}
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xmlA, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementA = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            //B.cs
            //using x.y.z;
            //if(bar) {
            //  using x = bar.baz;
            //  foo = 17;
            //}
            string xmlB = @"using x.y.z;
            if(bar) {
              using x = bar.baz;
              foo = 17;
            }";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runA.GenerateSrcMLFromString(xmlB, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementB = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "A.cs");

            var scopeA = codeParser.ParseFileUnit(xmlElementA);
            var scopeB = codeParser.ParseFileUnit(xmlElementB);
            var globalScope = scopeA.Merge(scopeB);
            var foo = globalScope.ChildStatements[2].ChildStatements[1].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(n => n.Name == "foo");
            Assert.IsNotNull(foo);
            var aliases = foo.GetAliases().ToList();
            Assert.AreEqual(1, aliases.Count);
            Assert.AreEqual("bar . baz", aliases[0].Target.ToString());
            Assert.AreEqual("x", aliases[0].AliasName);
            var imports = foo.GetImports().ToList();
            Assert.AreEqual(1, imports.Count);
            Assert.AreEqual("x . y . z", imports[0].ImportedNamespace.ToString());

            var baz = globalScope.GetDescendants<TypeDefinition>().FirstOrDefault(ns => ns.Name == "baz");
            Assert.IsNotNull(baz);
            var bazUse = aliases[0].Target.GetDescendantsAndSelf<NameUse>().LastOrDefault(nu => nu.Name == "baz");
            Assert.IsNotNull(bazUse);
            Assert.AreSame(baz, bazUse.FindMatches().First());
        }
示例#23
0
        public void TestCreateTypeDefinitions_InnerClassWithNamespace() {
            ////A.cs
            //namespace Foo {
            //    class A {
            //        class B {}
            //    }
            //}
            string xml = @"namespace Foo {
                class A {
                    class B {}
                }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            var globalScope = codeParser.ParseFileUnit(xmlElement);

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

            Assert.AreEqual("A", typeA.Name);
            Assert.AreEqual("Foo", typeA.GetAncestors<NamespaceDefinition>().First().GetFullName());
            Assert.AreEqual("Foo.A", typeA.GetFullName());

            Assert.AreEqual("B", typeB.Name);
            Assert.AreEqual("Foo", typeB.GetAncestors<NamespaceDefinition>().First().GetFullName());
            Assert.AreEqual("Foo.A.B", typeB.GetFullName());
        }
示例#24
0
        public void TestImport_NameResolution() {
            //A.cs
            //using Foo.Bar;
            //
            //namespace A {
            //  public class Robot {
            //    public Baz GetThingy() { return new Baz(); }
            //  }
            //}
            string xmlA = @"using Foo.Bar;
            namespace A {
              public class Robot {
                public Baz GetThingy() { return new Baz(); }
              }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xmlA, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementA = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            //B.cs
            //namespace Foo.Bar {
            //  public class Baz {
            //    public Baz() { }
            //  }
            //}
            string xmlB = @"namespace Foo.Bar {
              public class Baz {
                public Baz() { }
              }
            }";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(xmlB, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementB = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");

            var scopeA = codeParser.ParseFileUnit(xmlElementA);
            var scopeB = codeParser.ParseFileUnit(xmlElementB);
            var globalScope = scopeA.Merge(scopeB);
            Assert.AreEqual(3, globalScope.ChildStatements.Count);

            var baz = globalScope.GetDescendants<TypeDefinition>().FirstOrDefault(t => t.Name == "Baz");
            Assert.IsNotNull(baz);

            var thingy = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(m => m.Name == "GetThingy");
            Assert.IsNotNull(thingy);
            var thingyTypes = thingy.ReturnType.FindMatches().ToList();
            Assert.AreEqual(1, thingyTypes.Count);
            Assert.AreSame(baz, thingyTypes[0]);

            var bazDef = baz.GetNamedChildren<MethodDefinition>("Baz").First();
            var bazCall = thingy.ChildStatements[0].Content.GetDescendantsAndSelf<MethodCall>().FirstOrDefault(mc => mc.Name == "Baz");
            Assert.IsNotNull(bazCall);
            Assert.AreSame(bazDef, bazCall.FindMatches().FirstOrDefault());
        }
示例#25
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;
            }
        }
示例#26
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");
            
        }
示例#27
0
        public void TestGenerateSrcMLFromFiles() {
            LibSrcMLRunner run = new LibSrcMLRunner();
            List<string> fileList = new List<string>() { Path.Combine(TestInputPath, "input.cpp"), Path.Combine(TestInputPath, "input2.cpp") };
            try {
                run.GenerateSrcMLFromFiles(fileList, "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(2, 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);

                string file2 = Path.Combine(TestInputPath, "input2.cpp");
                var f2 = (from ele in files
                          where ele.Attribute("filename").Value == file2
                          select ele);
                Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value);
            }
            catch (SrcMLException e) {
                throw e;
            }
        }
示例#28
0
        public void TestResolveArrayVariable_Property() {
            //class Foo {
            //  Collection<int> Parameters { get; set; }
            //  void DoWork() {
            //    printf(Parameters[0]);
            //  }
            //}
            string xml = @"class Foo {
              Collection<int> Parameters { get; set; }
              void DoWork() {
                printf(Parameters[0]);
              }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            var paramDecl = globalScope.GetDescendants<PropertyDefinition>().First(p => p.Name == "Parameters");
            Assert.IsNotNull(paramDecl);
            var doWork = globalScope.GetDescendants<MethodDefinition>().First(m => m.Name == "DoWork");
            Assert.AreEqual(1, doWork.ChildStatements.Count);
            var paramUse = doWork.ChildStatements[0].Content.GetDescendantsAndSelf<NameUse>().FirstOrDefault(n => n.Name == "Parameters");
            Assert.IsNotNull(paramUse);
            Assert.AreSame(paramDecl, paramUse.FindMatches().FirstOrDefault());
        }
示例#29
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");
        }
示例#30
0
        public void TestAlias_NameResolution() {
            string xmlA = @"namespace Foo.Bar {
              public class Baz {
                public static void DoTheThing() { };
              }
            }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xmlA, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);

            XElement xmlElementA = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            string xmlB = @"using Baz = Foo.Bar.Baz;
            namespace A {
              public class B {
                public B() {
                  Baz.DoTheThing();
                }
              }
            }";
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(xmlB, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElementB = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");

            var scopeA = codeParser.ParseFileUnit(xmlElementA);
            var scopeB = codeParser.ParseFileUnit(xmlElementB);
            var globalScope = scopeA.Merge(scopeB);
            Assert.AreEqual(3, globalScope.ChildStatements.Count);

            var thingDef = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(md => md.Name == "DoTheThing");
            Assert.IsNotNull(thingDef);
            Assert.AreEqual("Baz", ((TypeDefinition)thingDef.ParentStatement).Name);

            var bDef = globalScope.GetDescendants<MethodDefinition>().FirstOrDefault(md => md.Name == "B");
            Assert.IsNotNull(bDef);
            Assert.AreEqual(1, bDef.ChildStatements.Count);
            var thingCall = bDef.ChildStatements[0].Content.GetDescendantsAndSelf<MethodCall>().FirstOrDefault();
            Assert.IsNotNull(thingCall);
            var data = thingCall.FindMatches().First();
            Assert.AreSame(thingDef, thingCall);
        }
示例#31
0
        public void TestUsingBlock_MultipleDecl() {
            // using(Foo a = new Foo(1), b = new Foo(2)) { ; }
            string xml = @"using(Foo a = new Foo(1), b = new Foo(2)) { ; }";
            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(xml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");

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

            var actual = globalScope.ChildStatements[0] as UsingBlockStatement;
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.ChildStatements.Count);
            Assert.IsNotNull(actual.Initializer);
            var decls = actual.Initializer.GetDescendantsAndSelf<VariableDeclaration>().ToList();
            Assert.AreEqual(2, decls.Count);
            Assert.AreEqual("a", decls[0].Name);
            Assert.AreEqual("Foo", decls[0].VariableType.Name);
            Assert.IsNotNull(decls[0].Initializer);
            Assert.AreEqual("b", decls[1].Name);
            Assert.AreEqual("Foo", decls[1].VariableType.Name);
            Assert.IsNotNull(decls[1].Initializer);
        }
示例#32
0
        public void TestCallingVariableDeclaredInParentClass() {
            //class A { void Foo() { } }
            string axml = @"class A { void Foo() { } }";

            //class B { protected A a; }
            string bxml = @"class B { protected A a; }";

            //class C : B { void Bar() { a.Foo(); } }
            string cxml = @"class C : B { void Bar() { a.Foo(); } }";

            LibSrcMLRunner runA = new LibSrcMLRunner();
            string srcMLA = runA.GenerateSrcMLFromString(axml, "A.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var aUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLA, "A.cs");
            LibSrcMLRunner runB = new LibSrcMLRunner();
            string srcMLB = runB.GenerateSrcMLFromString(bxml, "B.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var bUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLB, "B.cs");
            LibSrcMLRunner runC = new LibSrcMLRunner();
            string srcMLC = runC.GenerateSrcMLFromString(cxml, "C.cs", Language.CSharp, new Collection<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_POSITION }, false);
            var cUnit = fileSetup.GetFileUnitForXmlSnippet(srcMLC, "C.cs");

            var globalScope = codeParser.ParseFileUnit(aUnit);
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(bUnit));
            globalScope = globalScope.Merge(codeParser.ParseFileUnit(cUnit));

            var typeA = globalScope.GetNamedChildren<TypeDefinition>("A").FirstOrDefault();
            var typeB = globalScope.GetNamedChildren<TypeDefinition>("B").FirstOrDefault();
            var typeC = globalScope.GetNamedChildren<TypeDefinition>("C").FirstOrDefault();

            Assert.IsNotNull(typeA, "could not find class A");
            Assert.IsNotNull(typeB, "could not find class B");
            Assert.IsNotNull(typeC, "could not find class C");

            var aDotFoo = typeA.GetNamedChildren<MethodDefinition>("Foo").FirstOrDefault();
            Assert.IsNotNull(aDotFoo, "could not find method A.Foo()");

            var cDotBar = typeC.GetNamedChildren<MethodDefinition>("Bar").FirstOrDefault();
            Assert.IsNotNull(cDotBar, "could not find method C.Bar()");

            var callToFoo = cDotBar.FindExpressions<MethodCall>(true).FirstOrDefault();
            Assert.IsNotNull(callToFoo, "could not find any method calls in C.Bar()");
            Assert.AreEqual("Foo", callToFoo.Name);

            Assert.AreEqual(aDotFoo, callToFoo.FindMatches().FirstOrDefault());
        }