CgCppBasicTest.xtend 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * generated by Xtext 2.10.0
  3. */
  4. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
  5. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation.BuildUtilities
  6. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation.CppGenerator
  7. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
  8. import be.uantwerpen.ansymo.semanticadaptation.testframework.StaticGenerators
  9. import be.uantwerpen.ansymo.semanticadaptation.tests.AbstractSemanticAdaptationTest
  10. import be.uantwerpen.ansymo.semanticadaptation.tests.SemanticAdaptationInjectorProvider
  11. import com.google.inject.Inject
  12. import java.io.File
  13. import java.io.FileWriter
  14. import java.nio.file.Files
  15. import java.util.regex.Pattern
  16. import org.eclipse.emf.common.CommonPlugin
  17. import org.eclipse.emf.common.util.URI
  18. import org.eclipse.emf.ecore.EObject
  19. import org.eclipse.emf.ecore.resource.ResourceSet
  20. import org.eclipse.xtext.generator.InMemoryFileSystemAccess
  21. import org.eclipse.xtext.testing.InjectWith
  22. import org.eclipse.xtext.testing.XtextRunner
  23. import org.eclipse.xtext.testing.util.ParseHelper
  24. import org.eclipse.xtext.testing.validation.ValidationTestHelper
  25. import org.junit.Assert
  26. import org.junit.Ignore
  27. import org.junit.Test
  28. import org.junit.runner.RunWith
  29. @RunWith(XtextRunner)
  30. @InjectWith(SemanticAdaptationInjectorProvider)
  31. class CgCppBasicTest extends AbstractSemanticAdaptationTest {
  32. val testOutputFolder = "c-gen-tests"
  33. def getOutputDirectory(String directoryName) {
  34. return new File(("target/" + testOutputFolder + "/" + directoryName).replace("/", File.separatorChar));
  35. }
  36. // @Inject CppGenerator underTest
  37. @Inject extension ParseHelper<SemanticAdaptation>
  38. @Inject extension ValidationTestHelper
  39. @Ignore
  40. @Test def window_sa_canonical() {
  41. __parseNoErrors('test_input/single_folder_spec/window/window_sa_canonical.BASE.sa', 'generated', "powerwindow");
  42. // __parseNoErrorsWithValidation('test_input/single_folder_spec/window',
  43. // 'test_input/single_folder_spec/window/window_sa_canonical.BASE.sa');
  44. }
  45. @Test def window_sa_canonical_new() {
  46. __parseNoErrors('test_input/single_folder_spec/window/window_sa_canonical_new.BASE.sa', 'generated',
  47. "powerwindow");
  48. }
  49. @Test def window_sa_canonical_generated() {
  50. __parseNoErrors('test_input/single_folder_spec/window/window_sa.BASE_canonical.sa', 'generated',
  51. "powerwindow_generated");
  52. }
  53. @Ignore
  54. @Test def lazy_sa_canonical() {
  55. __parseNoErrors('test_input/single_folder_spec/lazy/lazy_canonical.sa', 'generated', "lazy");
  56. }
  57. def __parseNoErrorsWithValidation(String directory, String filename) {
  58. val model = __parse(filename);
  59. __assertNoParseErrors(model, filename);
  60. val correctFileDirectory = new File(directory + File.separator + "correct");
  61. val fsa = new InMemoryFileSystemAccess()
  62. val cppGen = new CppGenerator();
  63. cppGen.doGenerate(model.eResource, fsa);
  64. for (files : fsa.allFiles.entrySet) {
  65. val filename2 = files.key.substring(14);
  66. val file = new File(correctFileDirectory, filename2);
  67. val correctFileContent = Files.readAllLines(file.toPath);
  68. var path = getOutputDirectory(directory + filename);
  69. if (path.exists)
  70. path.delete
  71. else
  72. path.mkdir;
  73. path = new File(path, files.key.substring(14))
  74. val FileWriter writer = new FileWriter(path);
  75. writer.write(files.value.toString);
  76. writer.close;
  77. val testFileContent = Files.readAllLines(path.toPath);
  78. if (correctFileContent.size != testFileContent.size) {
  79. System.out.println("Error: Lines are of different length in file: " + filename2);
  80. } else {
  81. for (var i = 0; i < testFileContent.size; i++) {
  82. val testLine = testFileContent.get(i);
  83. val correctLine = correctFileContent.get(i);
  84. if (testLine.compareTo(correctLine) != 0) {
  85. if (!testLine.contains("guid")) {
  86. System.out.println("ERROR: The following lines are not equal: \n" + testLine + "\n" +
  87. correctLine);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. def __parseNoErrors(String filename, String directory, String projectName) {
  95. val saRootDir = getOutputDirectory(directory + File.separatorChar + projectName);
  96. val srcGenPath = new File(saRootDir, "sources")
  97. val resourcesPath = new File(saRootDir, "resources");
  98. val saFrameworkPath = new File(saRootDir, "framework")
  99. System.out.println("Rootdir: " + saRootDir.absolutePath)
  100. val model = __parse(filename)
  101. __assertNoParseErrors(model, filename)
  102. val fsa = new InMemoryFileSystemAccess();
  103. val cppGen = new CppGenerator();
  104. cppGen.doGenerate(model.eResource, fsa, CommonPlugin.resolve(URI.createFileURI(".")));
  105. if (saRootDir.exists) {
  106. BuildUtilities.deleteFolder(srcGenPath);
  107. }
  108. saRootDir.mkdirs();
  109. srcGenPath.mkdirs();
  110. resourcesPath.mkdirs();
  111. saFrameworkPath.mkdirs();
  112. for (files : fsa.allFiles.entrySet) {
  113. val fName = files.key.substring(14);
  114. var File fp;
  115. if (fName.equals("modelDescription.xml")) {
  116. fp = new File(saRootDir, fName);
  117. } else {
  118. fp = new File(srcGenPath, fName);
  119. }
  120. BuildUtilities.writeToFile(fp, files.value.toString);
  121. }
  122. val mainCpp = StaticGenerators.generateMainCppFile(resourcesPath.absolutePath.replace("\\", "\\\\"));
  123. BuildUtilities.writeToFile(new File(srcGenPath, "main.cpp"), mainCpp);
  124. for (rf : cppGen.resourcePaths) {
  125. val sinkFile = new File(resourcesPath, rf.name);
  126. System.out.println("Copied file to: " + sinkFile);
  127. BuildUtilities.copyFile(rf, sinkFile);
  128. }
  129. BuildUtilities.writeToFile(new File(saRootDir, "CMakeLists.txt"),
  130. StaticGenerators.generateCMakeLists(projectName, "framework"));
  131. val cMakeToolChain = StaticGenerators.generateToolChainCmake();
  132. BuildUtilities.writeToFile(new File(saRootDir, "msys-toolchain.cmake"), cMakeToolChain);
  133. if (TestCompileUtils.mac || TestCompileUtils.unix) {
  134. Assert.assertTrue("Could not compile FMU.", new TestCompileUtils().checkCompile(saRootDir));
  135. }
  136. }
  137. def __parseNoErrorsPrint(String filename) {
  138. val root = __parse(filename)
  139. print_ast(root)
  140. __assertNoParseErrors(root, filename)
  141. }
  142. def __parse(String filename) {
  143. return readFile(filename).parse
  144. }
  145. def __parse(
  146. String filename,
  147. ResourceSet resourceSetToUse
  148. ) {
  149. return readFile(filename).parse(resourceSetToUse)
  150. }
  151. def __assertNoParseErrors(EObject root, String filename) {
  152. try {
  153. root.assertNoErrors
  154. } catch (AssertionError e) {
  155. val p = Pattern.compile(".*, offset (?<offset>[0-9]+), length (?<length>[0-9]+)")
  156. val code = readFile(filename)
  157. for (String line : e.message.split("\n")) {
  158. val m = p.matcher(line)
  159. m.matches()
  160. val count = __occurrencesInString(code.subSequence(0, Integer.valueOf(m.group("offset"))).toString(),
  161. "\n")
  162. print(filename + " at line " + (count + 1) + ": ")
  163. println(line)
  164. }
  165. throw e
  166. }
  167. }
  168. def __occurrencesInString(String str, String findstr) {
  169. var lastIndex = 0
  170. var count = 0
  171. while (lastIndex != -1) {
  172. lastIndex = str.indexOf(findstr, lastIndex)
  173. if (lastIndex != -1) {
  174. count++
  175. lastIndex += findstr.length()
  176. }
  177. }
  178. return count
  179. }
  180. }