TokenPrinterTest.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package de.jplag;
  2. import static de.jplag.TokenConstants.FILE_END;
  3. import static de.jplag.simple.TestTokenConstants.STRING;
  4. import static org.junit.jupiter.api.Assertions.assertEquals;
  5. import static org.junit.jupiter.api.Assertions.assertTrue;
  6. import java.nio.file.Path;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import org.junit.jupiter.api.Test;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. class TokenPrinterTest {
  13. private static final Path TEST_FILE_LOCATION = Path.of("src", "test", "resources", "de", "jplag", "samples");
  14. private static final String TEST_FILE_NAME = "TokenPrinterTest.txt";
  15. private static final String TOKEN_SEPARATOR = "(\\|)?\s*\\|";
  16. private static final Logger logger = LoggerFactory.getLogger(TokenPrinterTest.class);
  17. /**
  18. * Tests the function of the TokenPrinter
  19. */
  20. @Test
  21. void printMockDirectoriesAsSubmissions() {
  22. // See TokenPrinterTest.txt for the intended behaviour
  23. List<Token> tokens = new ArrayList<>();
  24. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 1, 1, "STRING".length()));
  25. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 2, 1, "STRING".length() + 1));
  26. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 3, 1, "STRING".length() + 2));
  27. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 4, 1, "STRING".length() + 10));
  28. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 6, 3, 1));
  29. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 7, 9, 1));
  30. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 9, 1, 1));
  31. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 9, 10, 1));
  32. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 10, 1, 1));
  33. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 10, 5, 1));
  34. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 12, 1, 1));
  35. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 12, 5, 1));
  36. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 12, 10, 1));
  37. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 14, 10, 1));
  38. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 14, 5, 1));
  39. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 14, 1, 1));
  40. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 16, -5, 1));
  41. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 19, 100, 1));
  42. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 22, 1, 100));
  43. tokens.add(new TestToken(FILE_END, TEST_FILE_NAME, Token.NO_VALUE, Token.NO_VALUE, Token.NO_VALUE));
  44. tokens.add(new TestToken(STRING, TEST_FILE_NAME, 100, 1, 1));
  45. String output = TokenPrinter.printTokens(tokens, TEST_FILE_LOCATION.toFile());
  46. logger.info(output); // no additional newline required
  47. testOutputCorrectness(TEST_FILE_NAME, tokens, output);
  48. }
  49. private static void testOutputCorrectness(String fileName, List<Token> tokens, String output) {
  50. int lineIndex = -1;
  51. int tokenIndex = 0;
  52. for (String line : output.lines().toList()) {
  53. if (line.isEmpty()) {
  54. continue;
  55. } else if (lineIndex == -1) {
  56. assertEquals(fileName, line);
  57. lineIndex = 0;
  58. } else if (line.startsWith(String.valueOf(lineIndex + 1))) {
  59. lineIndex++;
  60. } else {
  61. line = line.trim();
  62. String[] lineTokens = line.split(TOKEN_SEPARATOR);
  63. for (String lineToken : lineTokens) {
  64. if (lineToken.isEmpty()) {
  65. continue;
  66. }
  67. Token currentToken = tokens.get(tokenIndex);
  68. assertTrue(lineToken.equalsIgnoreCase(currentToken.toString()), "expected: %s, actual: %s".formatted(lineToken, currentToken));
  69. if (currentToken.getLine() != Token.NO_VALUE) {
  70. assertEquals(lineIndex, currentToken.getLine(), "invalid line for token " + currentToken);
  71. }
  72. tokenIndex++;
  73. }
  74. }
  75. }
  76. assertEquals(tokens.size() - 1, tokenIndex, "incorrect number of tokens printed");
  77. }
  78. }