Test.java 4.0 KB

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