TestLogger.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package me.hammerle.snuviscript.test;
  2. import java.io.File;
  3. import java.nio.file.Files;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import me.hammerle.snuviscript.code.ISnuviLogger;
  8. import me.hammerle.snuviscript.code.Script;
  9. import me.hammerle.snuviscript.exceptions.StackTrace;
  10. public class TestLogger implements ISnuviLogger {
  11. private final ArrayList<String> list = new ArrayList<>();
  12. @Override
  13. public void print(String message, Exception ex, String function, String scriptname, Script sc, StackTrace lines) {
  14. if(ex == null) {
  15. list.addAll(Arrays.asList(message.split("\n")));
  16. } else {
  17. System.out.println(ex);
  18. System.out.println(ex.getMessage());
  19. System.out.println("error line " + lines);
  20. }
  21. }
  22. public void reset() {
  23. list.clear();
  24. }
  25. public void printAll() {
  26. list.stream().forEach(s -> System.out.println(s));
  27. list.clear();
  28. }
  29. public boolean check(File f) {
  30. if(!f.exists()) {
  31. System.out.println(String.format("\"%s\" does not exist", f.getPath()));
  32. return false;
  33. }
  34. try {
  35. List<String> file = Files.readAllLines(f.toPath());
  36. if(file.size() != list.size()) {
  37. printNoMatch(f, file);
  38. return false;
  39. }
  40. for(int i = 0; i < file.size(); i++) {
  41. if(!file.get(i).equals(list.get(i))) {
  42. printNoMatch(f, file);
  43. return false;
  44. }
  45. }
  46. } catch(Exception ex) {
  47. ex.printStackTrace();
  48. return false;
  49. }
  50. return true;
  51. }
  52. private void printNoMatch(File f, List<String> file) {
  53. System.out.println(String.format("error checking %s ", f.getPath()));
  54. System.out.println("Expected ----------------------------------------");
  55. file.forEach(s -> System.out.println(s));
  56. System.out.println("Actual ------------------------------------------");
  57. list.forEach(s -> System.out.println(s));
  58. System.out.println("-------------------------------------------------");
  59. }
  60. }