Test.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package me.hammerle.snuviscript.test;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.function.BiConsumer;
  7. import me.hammerle.snuviscript.code.Script;
  8. import me.hammerle.snuviscript.code.ScriptManager;
  9. import me.hammerle.snuviscript.tokenizer.Tokenizer;
  10. import me.hammerle.snuviscript.tokenizer.Token;
  11. import me.hammerle.snuviscript.code.Compiler;
  12. import me.hammerle.snuviscript.instructions.Instruction;
  13. public class Test
  14. {
  15. private static final TestScheduler SCHEDULER = new TestScheduler();
  16. private static final TestLogger LOGGER = new TestLogger();
  17. private static final ScriptManager PARSER = new ScriptManager(LOGGER, SCHEDULER);
  18. private static int done = 0;
  19. private static int tests = 0;
  20. public static void test()
  21. {
  22. testTokenizer();
  23. testCompiler();
  24. testOutput();
  25. LOGGER.reset();
  26. PARSER.startScript(true, "test", "./test/test.test");
  27. PARSER.callEvent("testevent", null, null);
  28. LOGGER.printAll();
  29. }
  30. private static void testOutput()
  31. {
  32. done = 0;
  33. tests = 0;
  34. forEachFile(new File("./test"), ".out", (inFile, checkFile) ->
  35. {
  36. tests++;
  37. LOGGER.reset();
  38. Script sc = new Script(PARSER, null, null, inFile.getName(), inFile.getPath());
  39. sc.run();
  40. if(LOGGER.check(checkFile))
  41. {
  42. done++;
  43. }
  44. });
  45. System.out.println(String.format("%d / %d output tests succeeded", done, tests));
  46. }
  47. private static void testTokenizer()
  48. {
  49. done = 0;
  50. tests = 0;
  51. forEachFile(new File("./test"), ".tout", (inFile, checkFile) ->
  52. {
  53. try
  54. {
  55. try(FileInputStream in = new FileInputStream(inFile))
  56. {
  57. tests++;
  58. Tokenizer tokenizer = new Tokenizer();
  59. LOGGER.reset();
  60. for(Token t : tokenizer.tokenize(in))
  61. {
  62. LOGGER.print(t.toString(), null, null, null, null, -1);
  63. }
  64. if(LOGGER.check(checkFile))
  65. {
  66. done++;
  67. }
  68. }
  69. }
  70. catch(Exception ex)
  71. {
  72. ex.printStackTrace();
  73. }
  74. });
  75. System.out.println(String.format("%d / %d tokenizer tests succeeded", done, tests));
  76. }
  77. private static void testCompiler()
  78. {
  79. done = 0;
  80. tests = 0;
  81. final Compiler c = new Compiler();
  82. forEachFile(new File("./test"), ".cout", (inFile, checkFile) ->
  83. {
  84. tests++;
  85. try
  86. {
  87. try(FileInputStream in = new FileInputStream(inFile))
  88. {
  89. Tokenizer tokenizer = new Tokenizer();
  90. LOGGER.reset();
  91. Instruction[] instr = c.compile(tokenizer.tokenize(in),
  92. new HashMap<>(), new HashMap<>(), new HashMap<>(),
  93. new HashMap<>());
  94. for(Instruction i : instr)
  95. {
  96. LOGGER.print(i.toString(), null, null, null, null, -1);
  97. }
  98. if(LOGGER.check(checkFile))
  99. {
  100. done++;
  101. }
  102. }
  103. }
  104. catch(Exception ex)
  105. {
  106. System.out.println("_________________________________________");
  107. System.out.println(inFile + " failed:");
  108. System.out.println(ex.getMessage());
  109. ex.printStackTrace();
  110. }
  111. });
  112. System.out.println(String.format("%d / %d compiler tests succeeded", done, tests));
  113. }
  114. private static void forEachFile(File f, String ending, BiConsumer<File, File> bc)
  115. {
  116. if(f.isFile())
  117. {
  118. if(!f.getName().contains("."))
  119. {
  120. File checkFile = new File(f.getPath() + ending);
  121. if(!checkFile.exists())
  122. {
  123. try
  124. {
  125. checkFile.createNewFile();
  126. }
  127. catch(IOException ex)
  128. {
  129. }
  130. }
  131. bc.accept(f, checkFile);
  132. }
  133. }
  134. else if(f.isDirectory())
  135. {
  136. for(File fi : f.listFiles())
  137. {
  138. forEachFile(fi, ending, bc);
  139. }
  140. }
  141. }
  142. }