|
|
@@ -1,25 +1,26 @@
|
|
|
package de.jplag.swift;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.IntStream;
|
|
|
-import java.util.stream.StreamSupport;
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
+import de.jplag.SharedTokenType;
|
|
|
import de.jplag.Token;
|
|
|
-import de.jplag.TokenConstants;
|
|
|
-import de.jplag.TokenList;
|
|
|
import de.jplag.TokenPrinter;
|
|
|
|
|
|
class SwiftFrontendTest {
|
|
|
@@ -48,8 +49,6 @@ class SwiftFrontendTest {
|
|
|
* Regular expression that describes lines containing the end of a multiline comment and no more code after that.
|
|
|
*/
|
|
|
private static final String DELIMITED_COMMENT_END = ".*\\*/\\s*$";
|
|
|
- private static final String NOT_SET_STRING = "";
|
|
|
- private static final int NOT_SET = -1;
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger("Swift frontend test");
|
|
|
private final String[] testFiles = new String[] {COMPLETE_TEST_FILE};
|
|
|
@@ -64,8 +63,8 @@ class SwiftFrontendTest {
|
|
|
@Test
|
|
|
void parseTestFiles() {
|
|
|
for (String fileName : testFiles) {
|
|
|
- TokenList tokens = language.parse(testFileLocation, new String[] {fileName});
|
|
|
- String output = TokenPrinter.printTokens(tokens, testFileLocation, List.of(fileName));
|
|
|
+ List<Token> tokens = language.parse(testFileLocation, new String[] {fileName});
|
|
|
+ String output = TokenPrinter.printTokens(tokens, testFileLocation, Optional.empty());
|
|
|
logger.info(output);
|
|
|
|
|
|
testSourceCoverage(fileName, tokens);
|
|
|
@@ -76,28 +75,12 @@ class SwiftFrontendTest {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Confirms that every type of SwiftToken has a Sting representation associated to it.
|
|
|
- */
|
|
|
- @Test
|
|
|
- void testTokenToString() {
|
|
|
- var missingTokens = IntStream.range(0, SwiftTokenConstants.NUMBER_DIFF_TOKENS)
|
|
|
- .mapToObj(type -> new SwiftToken(type, NOT_SET_STRING, NOT_SET, NOT_SET, NOT_SET))
|
|
|
- .filter(token -> token.type2string().contains("UNKNOWN")).toList();
|
|
|
-
|
|
|
- if (!missingTokens.isEmpty()) {
|
|
|
- var typeList = missingTokens.stream().map(Token::getType).map(Object::toString).collect(Collectors.joining(", "));
|
|
|
- fail("Found token types with no string representation: %s".formatted(typeList));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Confirms that the code is covered to a basic extent, i.e. each line of code contains at least one token.
|
|
|
* @param fileName a code sample file name
|
|
|
* @param tokens the list of Tokens generated from the sample
|
|
|
*/
|
|
|
- private void testSourceCoverage(String fileName, TokenList tokens) {
|
|
|
+ private void testSourceCoverage(String fileName, List<Token> tokens) {
|
|
|
File testFile = new File(testFileLocation, fileName);
|
|
|
|
|
|
try {
|
|
|
@@ -106,13 +89,13 @@ class SwiftFrontendTest {
|
|
|
// All lines that contain code
|
|
|
var codeLines = getCodeLines(lines);
|
|
|
// All lines that contain token
|
|
|
- var tokenLines = IntStream.range(0, tokens.size()).mapToObj(tokens::getToken).mapToInt(Token::getLine).distinct().toArray();
|
|
|
+ var tokenLines = tokens.stream().mapToInt(Token::getLine).filter(line -> line != Token.NO_VALUE).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)));
|
|
|
+ assertTrue(diffLine.isEmpty(),
|
|
|
+ "Line %d of file '%s' is not represented in the token list.".formatted(codeLines[diffLine.getAsInt()], fileName));
|
|
|
}
|
|
|
assertArrayEquals(codeLines, tokenLines);
|
|
|
} catch (IOException exception) {
|
|
|
@@ -158,18 +141,14 @@ class SwiftFrontendTest {
|
|
|
* @param tokens TokenList which is supposed to contain all types of tokens
|
|
|
* @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();
|
|
|
- // Exclude SEPARATOR_TOKEN, as it does not occur
|
|
|
- var allTokens = IntStream.range(0, SwiftTokenConstants.NUMBER_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 SwiftToken(allTokens[lineIdx], fileName, -1, -1, -1).type2string(), fileName)));
|
|
|
- }
|
|
|
- assertArrayEquals(allTokens, foundTokens);
|
|
|
+ private void testTokenCoverage(List<Token> tokens, String fileName) {
|
|
|
+ var annotatedTokens = tokens.stream().map(Token::getType).collect(Collectors.toSet());
|
|
|
+ assertTrue(annotatedTokens.contains(SharedTokenType.FILE_END));
|
|
|
+ var annotatedSwiftTokens = annotatedTokens.stream().filter(SwiftTokenType.class::isInstance).collect(Collectors.toSet());
|
|
|
+ var allSwiftTokens = SwiftTokenType.values();
|
|
|
+ var missingSwiftTokens = Arrays.stream(allSwiftTokens).filter(token -> !annotatedSwiftTokens.contains(token)).toList();
|
|
|
+ assertTrue(missingSwiftTokens.isEmpty(), "The following Swift tokens are missing in the code example '%s':\n".formatted(fileName)
|
|
|
+ + String.join("\n", missingSwiftTokens.stream().map(SwiftTokenType::getDescription).toList()));
|
|
|
}
|
|
|
|
|
|
}
|