PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/ABB.SrcML.Test/SrcMLGeneratorTests.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 252 lines | 169 code | 35 blank | 48 comment | 1 complexity | 7bf922f9a0bb8a2bef7220c795fe1b3d 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;
 20using ABB.SrcML.Utilities;
 21
 22namespace ABB.SrcML.Test {
 23    [TestFixture]
 24    [Category("Build")]
 25    public class SrcMLGeneratorTests {
 26        private SrcMLGenerator generator;
 27        
 28        [TestFixtureSetUp]
 29        public static void FixtureInitialize() {
 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 FixtureCleanup() {
 70            /*
 71            foreach (var file in Directory.GetFiles("srcmltest"))
 72            {
 73                File.Delete(file);
 74            }
 75            foreach (var file in Directory.GetFiles("srcml_xml"))
 76            {
 77                File.Delete(file);
 78            }
 79            Directory.Delete("srcmltest");
 80            Directory.Delete("srcml_xml");
 81            */
 82        }
 83
 84        [SetUp]
 85        public void TestSetup() {
 86            generator = new SrcMLGenerator(TestConstants.SrcmlPath);
 87        }
 88
 89        [Test]
 90        public void DifferentLanguageTest() {
 91            generator.GenerateSrcMLFromFile("srcmltest\\CSHARP.cs", "srcml_xml\\differentlanguage_java.xml", Language.Java);
 92            var doc = new SrcMLFile("srcml_xml\\differentlanguage_java.xml");
 93            Assert.IsNotNull(doc);
 94        }
 95
 96        [Test]
 97        public void SrcMLFromStringTest() {
 98            string sourceCode = @"int foo() {
 99printf(""hello world!"");
100}";
101            string xml = generator.GenerateSrcMLFromString(sourceCode, Language.C);
102
103            XElement element = XElement.Parse(xml);
104
105            Assert.IsNotNull(element);
106        }
107
108        [Test]
109        public void InvalidLanguageTest() {
110            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_java.xml", Language.Java);
111            var doc = new SrcMLFile("srcml_xml\\invalidlanguage_java.xml");
112            Assert.IsNotNull(doc);
113
114            doc = null;
115            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_cpp.xml", Language.CPlusPlus);
116            doc = new SrcMLFile("srcml_xml\\invalidlanguage_cpp.xml");
117            Assert.IsNotNull(doc);
118
119            doc = null;
120            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_c.xml", Language.C);
121            doc = new SrcMLFile("srcml_xml\\invalidlanguage_c.xml");
122
123            Assert.IsNotNull(doc);
124        }
125
126        [Test]
127        public void SingleFileTest() {
128            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\singlefile.xml");
129            var unit = SrcMLElement.Load("srcml_xml\\singlefile.xml");
130            Assert.IsNotNull(unit);
131            Assert.AreEqual("srcmltest\\foo.c", unit.Attribute("filename").Value);
132        }
133
134        [Test]
135        public void MultipleFilesTest() {
136            var doc = generator.GenerateSrcMLFileFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c" }, "srcml_xml\\multiplefile.xml");
137
138            Assert.IsNotNull(doc);
139            var files = doc.FileUnits.ToList();
140            Assert.AreEqual(2, files.Count());
141            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
142            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
143        }
144
145        [Test]
146        public void MultipleFilesTest_DifferentDirectories() {
147            var doc = generator.GenerateSrcMLFileFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c", "..\\..\\TestInputs\\baz.cpp" }, "srcml_xml\\multiplefile.xml");
148
149            Assert.IsNotNull(doc);
150            var files = doc.FileUnits.ToList();
151            Assert.AreEqual(3, files.Count());
152            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
153            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
154            Assert.AreEqual("TestInputs\\baz.cpp", files[2].Attribute("filename").Value);
155        }
156
157        [Test]
158        public void MultipleFilesTest_Language() {
159            generator.GenerateSrcMLFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c" }, "srcml_xml\\multiplefile.xml", Language.CPlusPlus);
160            var doc = new SrcMLFile("srcml_xml\\multiplefile.xml");
161
162            Assert.IsNotNull(doc);
163            var files = doc.FileUnits.ToList();
164            Assert.AreEqual(2, files.Count());
165            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
166            Assert.AreEqual("C++", files[0].Attribute("language").Value);
167            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
168            Assert.AreEqual("C++", files[1].Attribute("language").Value);
169        }
170
171        [Test]
172        public void ExclusionFilterTest() {
173            var exclusionList = new List<string>();
174            exclusionList.Add("srcmltest\\bar.c");
175
176            var doc = generator.GenerateSrcMLFileFromDirectory("srcmltest", "srcml_xml\\exclusionfilter.xml", exclusionList, Language.C);
177
178            var numFileUnits = doc.FileUnits.Count();
179            string firstSourceFile = null;
180            if(numFileUnits > 0) {
181                firstSourceFile = doc.FileUnits.First().Attribute("filename").Value;
182            }
183
184            Assert.AreEqual(1, numFileUnits, "test.xml should have only one file in it");
185            Assert.AreEqual(Path.GetFullPath("srcmltest\\foo.c"), firstSourceFile);
186        }
187
188        [Test]
189        public void EmptyOutputFileTest() {
190            File.WriteAllText("srcml_xml\\emptyFile.xml", "");
191            Assert.IsTrue(File.Exists("srcml_xml\\emptyFile.xml"));
192
193            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\emptyFile.xml");
194            var doc = new SrcMLFile("srcml_xml\\emptyFile.xml");
195            Assert.IsNotNull(doc);
196            Assert.AreEqual(1, doc.FileUnits.Count());
197        }
198
199        [Test]
200        public void InputWithSpacesTest() {
201            generator.GenerateSrcMLFromFile("srcmltest\\File with spaces.cpp", "srcml_xml\\input_with_spaces.xml");
202            var doc = new SrcMLFile("srcml_xml\\input_with_spaces.xml");
203            Assert.IsNotNull(doc);
204            Assert.AreEqual(1, doc.FileUnits.Count());
205        }
206
207        //[Test]
208        //[ExpectedException(typeof(SrcMLException))]
209        //public void TestGenerateSrcMLFromFile_UnRegisteredExtension() {
210        //    //The default src2srcml can't parse c#, so this should fail
211        //    generator.NonDefaultExecutables.Clear();
212        //    generator.GenerateSrcMLFromFile(@"srcmltest\CSHARP.csx", @"srcml_xml\CSHARP.xml");
213        //}
214
215        [Test]
216        public void TestGenerateSrcMLFromFile_NonDefaultExtension() {
217            generator.GenerateSrcMLFromFile(@"srcmltest\CSHARP.cs", @"srcml_xml\CSHARP.xml");
218            var doc = new SrcMLFile(@"srcml_xml\CSHARP.xml");
219            Assert.IsNotNull(doc);
220            Assert.AreEqual(1, doc.FileUnits.Count());
221        }
222
223        [Test]
224        public void TestGenerateSrcMLFromFiles_NonDefaultExtension() {
225            var doc = generator.GenerateSrcMLFileFromFiles(new[] {@"srcmltest\File with spaces.cpp", @"srcmltest\CSHARP.cs", @"srcmltest\foo.c"}, @"srcml_xml\multiple_files_csharp.xml");
226            Assert.IsNotNull(doc);
227            Assert.AreEqual(3, doc.FileUnits.Count());
228        }
229
230        //[Test]
231        //[ExpectedException(typeof(InvalidOperationException))]
232        //public void TestRegisterExecutable_Duplicate() {
233        //    generator.RegisterExecutable(Path.Combine(TestConstants.SrcmlPath, "csharp"), new[] {Language.CSharp});
234        //}
235
236        //[Test]
237        //public void TestSupportedLanguages() {
238        //    var langs = generator.SupportedLanguages.ToList();
239        //    Assert.AreEqual(5, langs.Count);
240        //    Assert.IsTrue(langs.Contains(Language.C));
241        //    Assert.IsTrue(langs.Contains(Language.CPlusPlus));
242        //    Assert.IsTrue(langs.Contains(Language.CSharp));
243        //    Assert.IsTrue(langs.Contains(Language.Java));
244        //    Assert.IsTrue(langs.Contains(Language.AspectJ));
245
246        //    generator.NonDefaultExecutables.Clear();
247        //    langs = generator.SupportedLanguages.ToList();
248        //    Assert.AreEqual(4, langs.Count);
249        //    Assert.IsFalse(langs.Contains(Language.CSharp));
250        //}
251    }
252}