public void TestGetXElement() { var archive = new SrcMLArchive("SrcMLLocationTest", false, new SrcMLGenerator("SrcML")); var sourcePath = Path.GetFullPath(@"..\..\TestInputs\class_test.h"); archive.AddOrUpdateFile(sourcePath); var unit = archive.GetXElementForSourceFile(sourcePath); Assert.IsNotNull(unit); var classElement = unit.Descendants(SRC.Class).FirstOrDefault(); Assert.IsNotNull(classElement); var parser = new CPlusPlusCodeParser(); var globalScope = parser.ParseFileUnit(unit); var typeDefinition = globalScope.ChildStatements.OfType <TypeDefinition>().FirstOrDefault(); Assert.IsNotNull(typeDefinition); var element = typeDefinition.PrimaryLocation.GetXElement(archive); Assert.IsNotNull(element); Assert.AreEqual(classElement.GetSrcLineNumber(), element.GetSrcLineNumber()); Assert.AreEqual(classElement.GetSrcLinePosition(), element.GetSrcLinePosition()); Assert.AreEqual(classElement.GetXPath(), element.GetXPath()); }
public void PrepareMethodDefinition() { // SrcML objects which help generate the SrcML objects we need to test var fileSetup = new SrcMLFileUnitSetup(Language.CPlusPlus); var parser = new CPlusPlusCodeParser(); var fileUnit = fileSetup.GetFileUnitForXmlSnippet(srcmlOutput, "sampletestmethods.cpp"); var scope = parser.ParseFileUnit(fileUnit); // Create the method srcml object in which to search for s-units. methodDef = scope.GetDescendants <MethodDefinition>().First(); }
ExtractAllMethodsFromFile(string filePath) { // open the file and convert to srcml //var srcml = new SrcMLFile( filePath ); //var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //var srcmlPath = Path.Combine(baseDir,"..", "..","..","..","External"); var srcmlPath = @"C:\Users\kh\Documents\GitHubVisualStudio\Swummary\External"; Console.WriteLine(srcmlPath); var srcml = new SrcML(srcmlPath); //var srcml = new SrcMLGenerator(); srcml.GenerateSrcMLFromFile(filePath, Path.Combine(filePath, ".xml")); //var srcmlFile = var srcmlFile = srcml.GenerateSrcMLFromFile(filePath, Path.Combine(filePath, ".xml")); //var ns = new NamespaceDefinition(); // get a grip on the parts we care about var fileUnits = srcmlFile.FileUnits; var parser = new CPlusPlusCodeParser(); //TODO: allow language to be chosen or detected // iterate through the mess to build a list of what we want var methodList = new List <Tuple <string, string, MethodDefinition> >(); foreach (var file in fileUnits) { var fileScope = parser.ParseFileUnit(file); var methods = fileScope.GetDescendants <MethodDefinition>(); string filename = file.Name.ToString(); foreach (var method in methods) { string methodName = method.Name.ToString(); var definition = method.GetDescendants <MethodDefinition>().Single(); var element = new Tuple <string, string, MethodDefinition>(filename, methodName, definition); methodList.Add(element); } } // done! return(methodList.AsEnumerable()); }
public void TestGetXElement() { var archive = new SrcMLArchive("SrcMLLocationTest", false, new SrcMLGenerator("SrcML")); var sourcePath = Path.GetFullPath(@"..\..\TestInputs\class_test.h"); archive.AddOrUpdateFile(sourcePath); var unit = archive.GetXElementForSourceFile(sourcePath); Assert.IsNotNull(unit); var classElement = unit.Descendants(SRC.Class).FirstOrDefault(); Assert.IsNotNull(classElement); var parser = new CPlusPlusCodeParser(); var globalScope = parser.ParseFileUnit(unit); var typeDefinition = globalScope.ChildStatements.OfType<TypeDefinition>().FirstOrDefault(); Assert.IsNotNull(typeDefinition); var element = typeDefinition.PrimaryLocation.GetXElement(archive); Assert.IsNotNull(element); Assert.AreEqual(classElement.GetSrcLineNumber(), element.GetSrcLineNumber()); Assert.AreEqual(classElement.GetSrcLinePosition(), element.GetSrcLinePosition()); Assert.AreEqual(classElement.GetXPath(), element.GetXPath()); }
public void TestPipelineXMLSnippet() { // SrcML sample method string srcmlOutput = @"<function><type><name> bool </name></type> <name> findInFiles </name><parameter_list> () </parameter_list> <block>{ <decl_stmt><decl><type><specifier>const</specifier> <name>TCHAR</name> <modifier>*</modifier></type><name>dir2Search</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator>.</operator><name>getDir2Search</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <expr_stmt><expr><call><name>findFilesInOut</name><argument_list>()</argument_list></call></expr>;</expr_stmt> <if>if <condition>(<expr><operator>!</operator><name><name>dir2Search</name><index>[<expr><literal type=""number"">0</literal></expr>]</index></name> <operator>||</operator> <operator>!</operator><call><name><operator>::</operator><name>PathFileExists</name></name><argument_list>(<argument><expr><name>dir2Search</name></expr></argument>)</argument_list></call></expr>)</condition><then> <block>{ <return>return <expr><literal type = ""boolean"" > false </literal></expr>;</return> }</block></then></if> <decl_stmt><decl><type><name>string</name></type> <name>findString</name> <init>= <expr><literal type = ""string"" > """" </literal ></expr ></init ></decl >;</decl_stmt> <expr_stmt><expr><call><name>gethurry</name><argument_list>()</argument_list></call></expr>;</expr_stmt> <macro><name>findInOne</name><argument_list>(<argument>int a</argument>, <argument>findString</argument>)</argument_list></macro><empty_stmt>;</empty_stmt> <decl_stmt><decl><type><name>bool</name></type> <name>isRecursive</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator >.</operator><name>isRecursive</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <decl_stmt><decl><type><name>bool</name></type> <name>isInHiddenDir</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator >.</operator><name>isInHiddenDir</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <if>if <condition>(<expr><call><name><name>a</name><operator >.</operator><name>size</name></name><argument_list>()</argument_list></call> <operator >==</operator> <literal type = ""number"" > 0 </literal></expr>)</condition><then> <block>{ <expr_stmt><expr><call><name><name>a</name><operator >.</operator><name>setFindInFilesDirFilter</name></name><argument_list>(<argument><expr><literal type = ""string""> ""dddd"" </literal ></expr ></argument>, <argument><expr><call><name>TEXT</name><argument_list>(<argument><expr><literal type = ""string"" > ""*.*"" </literal ></expr></argument>)</argument_list></call></expr></argument>)</argument_list></call></expr>;</expr_stmt> <expr_stmt><expr><call><name><name>a</name><operator >.</operator><name>getPatterns</name></name><argument_list>(<argument><expr><name>findString</name></expr></argument>)</argument_list></call></expr>;</expr_stmt> }</block></then></if> <return>return <expr><literal type = ""boolean"" > true </literal ></expr>;</return> }</block></function>"; // Convert raw string to MethodDefinition var fileSetup = new SrcMLFileUnitSetup(Language.CPlusPlus); var parser = new CPlusPlusCodeParser(); var fileUnit = fileSetup.GetFileUnitForXmlSnippet(srcmlOutput, "sampletestmethods.cpp"); var scope = parser.ParseFileUnit(fileUnit); var srcmlMethod = scope.GetDescendants <MethodDefinition>().First(); // Verify the method definition Assert.IsInstanceOf <MethodDefinition>(srcmlMethod, "MethodDefinition found."); Console.WriteLine(srcmlMethod.ToString()); // Extract SUnit Statements from MethodDefinition var statements = new List <Statement>(); statements.AddRange(SUnitExtractor.ExtractEnding(srcmlMethod)); statements.AddRange(SUnitExtractor.ExtractSameAction(srcmlMethod)); statements.AddRange(SUnitExtractor.ExtractVoidReturn(srcmlMethod)); // verify the statements selected Assert.IsNotEmpty(statements, "statements selected from method definition"); Console.WriteLine(statements.ToString()); // Translate Statements into SUnits List <SUnit> sunits = statements.ConvertAll( new Converter <Statement, SUnit> (SUnitTranslator.Translate)); // verify sunits have been translated Assert.That(sunits.TrueForAll(s => s.action != null), "All SUnits initialized."); Console.WriteLine(sunits.ToString()); // Generate text from SUnits List <string> sentences = sunits.ConvertAll( new Converter <SUnit, string> (TextGenerator.GenerateText)); // verify string generated Assert.That(sentences.TrueForAll(s => s.Length > 0)); Console.WriteLine(sentences); // Collect text and summarize var methodDocument = String.Join <string>(" ", sentences); var summary = Summarizer.Summarize(methodDocument); // verify summary Assert.That(!summary.Equals("")); Console.WriteLine(summary); }
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 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>-></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); }