PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 1ms

/ABB.SrcML.Test/Src2SrcMLRunnerTests.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 292 lines | 191 code | 45 blank | 56 comment | 1 complexity | 58b24046e5a84b936efd7a79196336c3 MD5 | raw file
  1/******************************************************************************
  2 * Copyright (c) 2010 ABB Group
  3 * All rights reserved. This program and the accompanying materials
  4 * are made available under the terms of the Eclipse Public License v1.0
  5 * which accompanies this distribution, and is available at
  6 * http://www.eclipse.org/legal/epl-v10.html
  7 *
  8 * Contributors:
  9 *    Vinay Augustine (ABB Group) - initial API, implementation, & documentation
 10 *****************************************************************************/
 11
 12using System;
 13using System.IO;
 14using System.Text;
 15using System.Collections.Generic;
 16using System.Linq;
 17using NUnit.Framework;
 18using ABB.SrcML;
 19using System.Xml.Linq;
 20
 21namespace ABB.SrcML.Test
 22{
 23    [TestFixture]
 24    [Category("Build")]
 25    public class Src2SrcMLRunnerTests
 26    {
 27        [TestFixtureSetUp]
 28        public static void SrcMLTestInitialize()
 29        {
 30            Directory.CreateDirectory("srcmltest");
 31            Directory.CreateDirectory("srcml_xml");
 32            File.WriteAllText("srcmltest\\foo.c", String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine));
 33
 34            File.WriteAllText("srcmltest\\bar.c", String.Format(@"int bar() {{{0}    printf(""goodbye, world!"");{0}}}", Environment.NewLine));
 35
 36            File.WriteAllText("srcmltest\\CSHARP.cs", @"using System.Collections.Generic;
 37using System.Linq;
 38using System.Xml;
 39using System.Xml.Linq;
 40using System.Xml.XPath;
 41using ABB.SrcML;
 42
 43namespace LoggingTransformation
 44{
 45    public class FunctionEntryLogTransform : ITransform
 46    {
 47        public IEnumerable<XElement> Query(XElement element)
 48        {
 49            var functions = from method in element.Descendants(SRC.method)
 50                                select method;
 51            return functions;
 52        }
 53
 54        public XElement Transform(XElement element)
 55        {
 56            var first = element.Element(SRC.Block).Elements().First();
 57            var functionName = element.Element(SRC.Name).Value;
 58            first.AddBeforeSelf(new XElement(SRC.ExpressionStatement, string.Format(""LOG_FUNCTION_ENTRY(\""{0}\"");\n\t"", functionName)));
 59
 60            return element;
 61        }
 62    }
 63}
 64");
 65            File.WriteAllText("srcmltest\\File with spaces.cpp", String.Format(@"int foo() {{{0}    printf(""hello world!"");{0}}}", Environment.NewLine));
 66        }
 67
 68        [TestFixtureTearDown]
 69        public static void SrcMLTestCleanup()
 70        {
 71            /*
 72            foreach (var file in Directory.GetFiles("srcmltest"))
 73            {
 74                File.Delete(file);
 75            }
 76            foreach (var file in Directory.GetFiles("srcml_xml"))
 77            {
 78                File.Delete(file);
 79            }
 80            Directory.Delete("srcmltest");
 81            Directory.Delete("srcml_xml");
 82            */
 83        }
 84
 85        [Test]
 86        public void DifferentLanguageTest()
 87        {
 88            var srcmlObject = new Src2SrcMLRunner();
 89            
 90            var doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\CSHARP.cs", "srcml_xml\\differentlanguage_java.xml", Language.Java);
 91
 92            Assert.IsNotNull(doc);
 93        }
 94
 95        [Test]
 96        public void SrcMLFromStringTest()
 97        {
 98            string sourceCode = @"int foo() {
 99printf(""hello world!"");
100}";
101            var srcmlObject = new Src2SrcMLRunner();
102            string xml = srcmlObject.GenerateSrcMLFromString(sourceCode, Language.C);
103
104            XElement element = XElement.Parse(xml);
105
106            Assert.IsNotNull(element);
107        }
108
109        [Test]
110        public void InvalidLanguageTest()
111        {
112            var srcmlObject = new Src2SrcMLRunner();
113
114            var doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_java.xml", Language.Java);
115            Assert.IsNotNull(doc);
116            
117            doc = null;
118            doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_cpp.xml", Language.CPlusPlus);
119            Assert.IsNotNull(doc);
120
121            doc = null;
122            doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_c.xml", Language.C);
123
124            Assert.IsNotNull(doc);
125        }
126
127        [Test]
128        public void SingleFileTest()
129        {
130            var srcmlObject = new Src2SrcMLRunner();
131
132            var doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\singlefile.xml");
133            
134            Assert.IsNotNull(doc);
135            Assert.AreEqual(1, doc.FileUnits.Count());
136            Assert.AreEqual("srcmltest\\foo.c", doc.FileUnits.First().Attribute("filename").Value);
137        }
138
139        /// <summary>
140        /// Added by JZ on 12/3/2012.
141        /// Unit test for Src2SrcMLRunner.GenerateSrcMLAndStringFromFile()
142        /// </summary>
143        [Test]
144        public void SingleFileToFileAndStringTest()
145        {
146            var srcmlObject = new Src2SrcMLRunner(TestConstants.SrcmlPath);
147
148            string xml = srcmlObject.GenerateSrcMLAndStringFromFile("srcmltest\\foo.c", "srcml_xml\\singlefile.xml");
149            Console.WriteLine("xml = " + xml);
150
151            Assert.IsNotNull(xml);
152            //Assert.AreEqual(1, doc.FileUnits.Count());
153            //Assert.AreEqual("srcmltest\\foo.c", doc.FileUnits.First().Attribute("filename").Value);
154            Assert.That(File.Exists("srcml_xml\\singlefile.xml"));
155        }
156
157        /// <summary>
158        /// Added by JZ on 12/4/2012.
159        /// Unit test for Src2SrcMLRunner.GenerateSrcMLAndXElementFromFile()
160        /// </summary>
161        [Test]
162        public void SingleFileToFileAndXElementTest()
163        {
164            var srcmlObject = new Src2SrcMLRunner(TestConstants.SrcmlPath);
165
166            XElement xElement = srcmlObject.GenerateSrcMLAndXElementFromFile("srcmltest\\foo.c", "srcml_xml\\singlefile.xml");
167
168            Assert.IsNotNull(xElement);
169            //Assert.AreEqual(1, doc.FileUnits.Count());
170            //Assert.AreEqual("srcmltest\\foo.c", doc.FileUnits.First().Attribute("filename").Value);
171            Assert.That(File.Exists("srcml_xml\\singlefile.xml"));
172        }
173
174        [Test]
175        public void MultipleFilesTest()
176        {
177            var srcmlObject = new Src2SrcMLRunner();
178            var doc = srcmlObject.GenerateSrcMLFromFiles(new string[] {"srcmltest\\foo.c", "srcmltest\\bar.c"}, "srcml_xml\\multiplefile.xml");
179
180            Assert.IsNotNull(doc);
181            var files = doc.FileUnits.ToList();
182            Assert.AreEqual(2, files.Count());
183            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
184            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
185        }
186
187        [Test]
188        public void MultipleFilesTest_DifferentDirectories()
189        {
190            var srcmlObject = new Src2SrcMLRunner();
191            var doc = srcmlObject.GenerateSrcMLFromFiles(new string[] {"srcmltest\\foo.c", "srcmltest\\bar.c", "..\\..\\TestInputs\\baz.cpp"}, "srcml_xml\\multiplefile.xml");
192
193            Assert.IsNotNull(doc);
194            var files = doc.FileUnits.ToList();
195            Assert.AreEqual(3, files.Count());
196            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
197            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
198            Assert.AreEqual("TestInputs\\baz.cpp", files[2].Attribute("filename").Value);
199        }
200
201        [Test]
202        public void MultipleFilesTest_Language() {
203            var srcmlObject = new Src2SrcMLRunner();
204            var doc = srcmlObject.GenerateSrcMLFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c" }, "srcml_xml\\multiplefile.xml", Language.CPlusPlus);
205
206            Assert.IsNotNull(doc);
207            var files = doc.FileUnits.ToList();
208            Assert.AreEqual(2, files.Count());
209            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
210            Assert.AreEqual("C++", files[0].Attribute("language").Value);
211            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
212            Assert.AreEqual("C++", files[1].Attribute("language").Value);
213        }
214
215        //[Test]
216        // TODO this test depends on my computer. Fix it.
217        //public void TestDirectoryParsing()
218        //{
219        //    var srcmlObject = new Src2SrcMLRunner(TestConstants.SrcmlPath);
220        //    var xmlFileName = Path.GetTempFileName();
221        //    var document = srcmlObject.GenerateSrcMLFromDirectory(@"C:\Users\USVIAUG\Documents\Source Code\Notepad++", xmlFileName);
222        //    Assert.AreEqual(283, document.FileUnits.Count());
223        //}
224        [Test]
225        public void ExclusionFilterTest()
226        {
227            var exclusionList = new List<string>();
228            exclusionList.Add("srcmltest\\bar.c");
229
230            var srcmlObject = new Src2SrcMLRunner();
231
232            var doc = srcmlObject.GenerateSrcMLFromDirectory("srcmltest", "srcml_xml\\exclusionfilter.xml", exclusionList, Language.C);
233
234            var numFileUnits = doc.FileUnits.Count();
235            string firstSourceFile = null;
236            if(numFileUnits > 0)
237            {
238                firstSourceFile = doc.FileUnits.First().Attribute("filename").Value;
239            }
240
241            Assert.AreEqual(1, numFileUnits, "test.xml should have only one file in it");
242            Assert.AreEqual(Path.GetFullPath("srcmltest\\foo.c"), firstSourceFile);
243        }
244
245        [Test]
246        public void EmptyOutputFileTest()
247        {
248            var srcmlObject = new Src2SrcMLRunner();
249            File.WriteAllText("srcml_xml\\emptyFile.xml", "");
250            Assert.IsTrue(File.Exists("srcml_xml\\emptyFile.xml"));
251
252            var doc = srcmlObject.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\emptyFile.xml");
253
254            Assert.IsNotNull(doc);
255            Assert.AreEqual(1, doc.FileUnits.Count());
256        }
257
258        [Test]
259        public void InputWithSpacesTest()
260        {
261            var runner = new Src2SrcMLRunner();
262            var doc = runner.GenerateSrcMLFromFile("srcmltest\\File with spaces.cpp", "srcml_xml\\input_with_spaces.xml");
263
264            Assert.IsNotNull(doc);
265            Assert.AreEqual(1, doc.FileUnits.Count());
266        }
267
268        [Test]
269        public void MyTestMethod()
270        {
271            var runner = new Src2SrcMLRunner();
272            runner.GenerateSrcMLFromDirectory("srcmltest", "srcmltest1.xml");
273            runner.GenerateSrcMLFromFile("srcmltest\\File with spaces.cpp", "testfile.xml");
274        }
275        //[Test]
276        //public void TestRegularExpression()
277        //{
278        //    string output;
279        //    output = SrcML.GetLogFromOutput(@"    - ..\..\TEST\dbglib.py      Skipped: Unregistered extension.");
280        //    Assert.AreEqual(@"-: ..\..\TEST\dbglib.py: Skipped: Unregistered extension.", output);
281
282        //    output = SrcML.GetLogFromOutput(@"Path: ..\..\TEST\dbglib.c       Error: Unable to open file.");
283        //    Assert.AreEqual(@"Path:: ..\..\TEST\dbglib.c: Error: Unable to open file.", output);
284
285        //    output = SrcML.GetLogFromOutput(@"    1 Z:\Source\Robotics\rel5_11.0160.release\sys\modulelib\dbglib\dbglib.c");
286        //    Assert.AreEqual(@"1: Z:\Source\Robotics\rel5_11.0160.release\sys\modulelib\dbglib\dbglib.c: : ", output);
287
288        //    output = SrcML.GetLogFromOutput(@"Path: Z:\Source\Robotics\rel5_11.0160.release\sys\modulelib\dbglib\dbglib.c");
289        //    Assert.AreEqual(@"Path:: Z:\Source\Robotics\rel5_11.0160.release\sys\modulelib\dbglib\dbglib.c: : ", output);
290        //}
291    }
292}