CgCppBasicTest.xtend 7.2 KB

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