-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1914 from jplag/feature/columnIndexTests
Added structure for token position tests.
- Loading branch information
Showing
9 changed files
with
226 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
language-testutils/src/test/java/de/jplag/testutils/TemporaryFileHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.jplag.testutils; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Stores all temporary files that are created for a {@link LanguageModuleTest} and provides the option to delete them | ||
*/ | ||
public class TemporaryFileHolder { | ||
public static List<File> temporaryFiles = new ArrayList<>(); | ||
|
||
/** | ||
* Deletes all temporary files that have been created up to this point | ||
*/ | ||
public static void deleteTemporaryFiles() { | ||
temporaryFiles.forEach(File::delete); | ||
temporaryFiles.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package de.jplag.testutils.datacollector; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import de.jplag.Language; | ||
import de.jplag.ParsingException; | ||
import de.jplag.Token; | ||
import de.jplag.testutils.TemporaryFileHolder; | ||
import de.jplag.util.FileUtils; | ||
|
||
/** | ||
* Test sources with token information Reads token position test specifications form a file and provides the token | ||
* information for tests. The sources can be used as regular test sources. | ||
*/ | ||
public class TokenPositionTestData implements TestData { | ||
private final List<String> sourceLines; | ||
private final List<TokenData> expectedTokens; | ||
|
||
private final String descriptor; | ||
|
||
/** | ||
* @param testFile The file containing the test specifications | ||
* @throws IOException If the file cannot be read | ||
*/ | ||
public TokenPositionTestData(File testFile) throws IOException { | ||
this.sourceLines = new ArrayList<>(); | ||
this.expectedTokens = new ArrayList<>(); | ||
this.descriptor = "(Token position file: " + testFile.getName() + ")"; | ||
this.readFile(testFile); | ||
} | ||
|
||
private void readFile(File testFile) throws IOException { | ||
List<String> testFileLines = FileUtils.readFileContent(testFile).lines().toList(); | ||
int currentLine = 0; | ||
|
||
for (String sourceLine : testFileLines) { | ||
if (sourceLine.charAt(0) == '>') { | ||
this.sourceLines.add(sourceLine.substring(1)); | ||
currentLine++; | ||
} | ||
|
||
if (sourceLine.charAt(0) == '$') { | ||
int column = sourceLine.indexOf('|'); | ||
String[] tokenDescriptionParts = sourceLine.split(" ", 0); | ||
|
||
String typeName = tokenDescriptionParts[tokenDescriptionParts.length - 2]; | ||
int length = Integer.parseInt(tokenDescriptionParts[tokenDescriptionParts.length - 1]); | ||
this.expectedTokens.add(new TokenData(typeName, currentLine, column, length)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<Token> parseTokens(Language language) throws ParsingException, IOException { | ||
File file = File.createTempFile("testSource", language.suffixes()[0]); | ||
FileUtils.write(file, String.join(System.lineSeparator(), sourceLines)); | ||
List<Token> tokens = language.parse(Collections.singleton(file)); | ||
TemporaryFileHolder.temporaryFiles.add(file); | ||
return tokens; | ||
} | ||
|
||
@Override | ||
public String[] getSourceLines() { | ||
return this.sourceLines.toArray(new String[0]); | ||
} | ||
|
||
@Override | ||
public String describeTestSource() { | ||
return this.descriptor; | ||
} | ||
|
||
/** | ||
* @return A list of the expected tokens for this test source | ||
*/ | ||
public List<TokenData> getExpectedTokens() { | ||
return expectedTokens; | ||
} | ||
|
||
/** | ||
* Information about a single token | ||
* @param typeName The name of the token type | ||
* @param lineNumber The line the token is in (1 based) | ||
* @param columnNumber The column the token is in (1 based) | ||
* @param length The length of the token | ||
*/ | ||
public record TokenData(String typeName, int lineNumber, int columnNumber, int length) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
languages/java/src/test/resources/de/jplag/java/tokenPositions/VarDef_1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
>class Test { | ||
> int test; | ||
$ | J_VARDEF 8 | ||
>} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters