RFrontendTest.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package de.jplag.rlang;
  2. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  3. import static org.junit.jupiter.api.Assertions.fail;
  4. import java.io.*;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.util.List;
  8. import java.util.stream.IntStream;
  9. import java.util.stream.StreamSupport;
  10. import org.junit.jupiter.api.BeforeEach;
  11. import org.junit.jupiter.api.Test;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import de.jplag.Token;
  15. import de.jplag.TokenConstants;
  16. import de.jplag.TokenList;
  17. import de.jplag.TokenPrinter;
  18. class RFrontendTest {
  19. /**
  20. * Regular expression for lines that contain no code.
  21. */
  22. private static final String R_NO_CODE_LINE = "\\s*(?:#.*)?";
  23. /**
  24. * Test source file that is supposed to produce a complete set of tokens, i.e. all types of tokens.
  25. */
  26. private static final String COMPLETE_TEST_FILE = "Complete.R";
  27. public static final int NOT_SET = -1;
  28. private final Logger logger = LoggerFactory.getLogger("R frontend test");
  29. private final String[] testFiles = new String[] {"Game.R", COMPLETE_TEST_FILE};
  30. private final File testFileLocation = Path.of("src", "test", "resources", "de", "jplag", "rlang").toFile();
  31. private Language language;
  32. @BeforeEach
  33. void setup() {
  34. language = new Language();
  35. }
  36. @Test
  37. void parseTestFiles() {
  38. for (String fileName : testFiles) {
  39. TokenList tokens = language.parse(testFileLocation, new String[] {fileName});
  40. String output = TokenPrinter.printTokens(tokens, testFileLocation, List.of(fileName));
  41. logger.info(output);
  42. testSourceCoverage(fileName, tokens);
  43. if (fileName.equals(COMPLETE_TEST_FILE))
  44. testTokenCoverage(tokens, fileName);
  45. }
  46. }
  47. /**
  48. * Confirms that the code is covered to a basic extent, i.e. each line of code contains at least one token.
  49. * @param fileName a code sample file name
  50. * @param tokens the TokenList generated from the sample
  51. */
  52. private void testSourceCoverage(String fileName, TokenList tokens) {
  53. File testFile = new File(testFileLocation, fileName);
  54. try {
  55. List<String> lines = Files.readAllLines(testFile.toPath());
  56. String emptyLineExpression = getNoCodeLineExpression();
  57. // All lines that contain code
  58. var codeLines = IntStream.range(1, lines.size() + 1).filter(idx -> !lines.get(idx - 1).matches(emptyLineExpression)).toArray();
  59. // All lines that contain token
  60. var tokenLines = IntStream.range(0, tokens.size()).mapToObj(tokens::getToken).mapToInt(Token::getLine).distinct().toArray();
  61. if (codeLines.length > tokenLines.length) {
  62. var diffLine = IntStream.range(0, codeLines.length)
  63. .dropWhile(lineIndex -> lineIndex < tokenLines.length && codeLines[lineIndex] == tokenLines[lineIndex]).findFirst();
  64. diffLine.ifPresent(
  65. lineIdx -> fail("Line %d of file '%s' is not represented in the token list.".formatted(codeLines[lineIdx], fileName)));
  66. }
  67. assertArrayEquals(codeLines, tokenLines);
  68. } catch (IOException exception) {
  69. logger.info("Error while reading test file %s".formatted(fileName), exception);
  70. fail();
  71. }
  72. }
  73. /**
  74. * Confirms that all Token types are 'reachable' with a complete code example.
  75. * @param tokens TokenList which is supposed to contain all types of tokens
  76. * @param fileName The file name of the complete code example
  77. */
  78. private void testTokenCoverage(TokenList tokens, String fileName) {
  79. var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).sorted().distinct().toArray();
  80. // Exclude SEPARATOR_TOKEN, as it does not occur
  81. var allTokens = IntStream.range(0, RTokenConstants.NUM_DIFF_TOKENS).filter(i -> i != TokenConstants.SEPARATOR_TOKEN).toArray();
  82. if (allTokens.length > foundTokens.length) {
  83. var diffLine = IntStream.range(0, allTokens.length)
  84. .dropWhile(lineIndex -> lineIndex < foundTokens.length && allTokens[lineIndex] == foundTokens[lineIndex]).findFirst();
  85. diffLine.ifPresent(lineIdx -> fail("Token type %s was not found in the complete code example '%s'."
  86. .formatted(new RToken(allTokens[lineIdx], fileName, NOT_SET, NOT_SET, NOT_SET).type2string(), fileName)));
  87. }
  88. assertArrayEquals(allTokens, foundTokens);
  89. }
  90. private static String getNoCodeLineExpression() {
  91. return R_NO_CODE_LINE;
  92. }
  93. }