|
|
@@ -1,14 +1,7 @@
|
|
|
package de.jplag.R;
|
|
|
|
|
|
-import de.jplag.Token;
|
|
|
-import de.jplag.TokenConstants;
|
|
|
-import de.jplag.TokenList;
|
|
|
-import de.jplag.TokenPrinter;
|
|
|
-import de.jplag.testutils.TestErrorConsumer;
|
|
|
-import org.junit.jupiter.api.BeforeEach;
|
|
|
-import org.junit.jupiter.api.Test;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
@@ -17,22 +10,30 @@ import java.util.List;
|
|
|
import java.util.stream.IntStream;
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
|
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
-import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import de.jplag.Token;
|
|
|
+import de.jplag.TokenConstants;
|
|
|
+import de.jplag.TokenList;
|
|
|
+import de.jplag.TokenPrinter;
|
|
|
+import de.jplag.testutils.TestErrorConsumer;
|
|
|
|
|
|
public class RFrontendTest {
|
|
|
|
|
|
/**
|
|
|
- * Regular expression for comments.
|
|
|
+ * Regular expression for comments.
|
|
|
*/
|
|
|
private static final String R_COMMENT = "#.*";
|
|
|
|
|
|
/**
|
|
|
- * Test source file that is supposed to produce a complete set of tokens, i.e. all types of tokens.
|
|
|
+ * Test source file that is supposed to produce a complete set of tokens, i.e. all types of tokens.
|
|
|
*/
|
|
|
private static final String COMPLETE_TEST_FILE = "Complete.R";
|
|
|
private final Logger logger = LoggerFactory.getLogger("R frontend test");
|
|
|
- private final String[] testFiles = new String[]{"Game.r", COMPLETE_TEST_FILE};
|
|
|
+ private final String[] testFiles = new String[] {"Game.r", COMPLETE_TEST_FILE};
|
|
|
private final File testFileLocation = Path.of("src", "test", "resources", "de", "jplag", "R").toFile();
|
|
|
private Language language;
|
|
|
|
|
|
@@ -45,12 +46,13 @@ public class RFrontendTest {
|
|
|
@Test
|
|
|
void parseTestFiles() {
|
|
|
for (String fileName : testFiles) {
|
|
|
- TokenList tokens = language.parse(testFileLocation, new String[]{fileName});
|
|
|
+ TokenList tokens = language.parse(testFileLocation, new String[] {fileName});
|
|
|
String output = TokenPrinter.printTokens(tokens, testFileLocation, List.of(fileName));
|
|
|
logger.info(output);
|
|
|
|
|
|
testSourceCoverage(fileName, tokens);
|
|
|
- if (fileName.equals(COMPLETE_TEST_FILE)) testTokenCoverage(tokens, fileName);
|
|
|
+ if (fileName.equals(COMPLETE_TEST_FILE))
|
|
|
+ testTokenCoverage(tokens, fileName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -67,13 +69,16 @@ public class RFrontendTest {
|
|
|
String commentExpression = getCommentExpression();
|
|
|
|
|
|
// All lines that contain code
|
|
|
- var codeLines = IntStream.range(1, lines.size() + 1).filter(idx -> !lines.get(idx - 1).matches("\\s*(%s)?".formatted(commentExpression))).toArray();
|
|
|
+ var codeLines = IntStream.range(1, lines.size() + 1).filter(idx -> !lines.get(idx - 1).matches("\\s*(%s)?".formatted(commentExpression)))
|
|
|
+ .toArray();
|
|
|
// All lines that contain token
|
|
|
var tokenLines = IntStream.range(0, tokens.size()).mapToObj(tokens::getToken).mapToInt(Token::getLine).distinct().toArray();
|
|
|
|
|
|
if (codeLines.length > tokenLines.length) {
|
|
|
- var diffLine = IntStream.range(0, codeLines.length).dropWhile(lineIdx -> lineIdx < tokenLines.length && codeLines[lineIdx] == tokenLines[lineIdx]).findFirst();
|
|
|
- diffLine.ifPresent(lineIdx -> fail("Line %d of file '%s' is not represented in the token list.".formatted(codeLines[lineIdx], fileName)));
|
|
|
+ var diffLine = IntStream.range(0, codeLines.length)
|
|
|
+ .dropWhile(lineIdx -> lineIdx < tokenLines.length && codeLines[lineIdx] == tokenLines[lineIdx]).findFirst();
|
|
|
+ diffLine.ifPresent(
|
|
|
+ lineIdx -> fail("Line %d of file '%s' is not represented in the token list.".formatted(codeLines[lineIdx], fileName)));
|
|
|
}
|
|
|
assertArrayEquals(codeLines, tokenLines);
|
|
|
} catch (IOException exception) {
|
|
|
@@ -88,15 +93,15 @@ public class RFrontendTest {
|
|
|
* @param fileName The file name of the complete code example
|
|
|
*/
|
|
|
private void testTokenCoverage(TokenList tokens, String fileName) {
|
|
|
- var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType)
|
|
|
- .sorted().distinct().toArray();
|
|
|
+ var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).sorted().distinct().toArray();
|
|
|
// Exclude SEPARATOR_TOKEN, as it does not occur
|
|
|
- var allTokens = IntStream.range(0, RTokenConstants.NUM_DIFF_TOKENS)
|
|
|
- .filter(i -> i != TokenConstants.SEPARATOR_TOKEN).toArray();
|
|
|
+ var allTokens = IntStream.range(0, RTokenConstants.NUM_DIFF_TOKENS).filter(i -> i != TokenConstants.SEPARATOR_TOKEN).toArray();
|
|
|
|
|
|
if (allTokens.length > foundTokens.length) {
|
|
|
- var diffLine = IntStream.range(0, allTokens.length).dropWhile(lineIdx -> lineIdx < foundTokens.length && allTokens[lineIdx] == foundTokens[lineIdx]).findFirst();
|
|
|
- diffLine.ifPresent(lineIdx -> fail("Token type %s was not found in the complete code example '%s'.".formatted(new RToken(allTokens[lineIdx], fileName, -1, -1, -1).type2string(), fileName)));
|
|
|
+ var diffLine = IntStream.range(0, allTokens.length)
|
|
|
+ .dropWhile(lineIdx -> lineIdx < foundTokens.length && allTokens[lineIdx] == foundTokens[lineIdx]).findFirst();
|
|
|
+ diffLine.ifPresent(lineIdx -> fail("Token type %s was not found in the complete code example '%s'."
|
|
|
+ .formatted(new RToken(allTokens[lineIdx], fileName, -1, -1, -1).type2string(), fileName)));
|
|
|
}
|
|
|
assertArrayEquals(allTokens, foundTokens);
|
|
|
}
|