From 9b2c31db6a55de9aff5d160e6d2e07bd215d3376 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 7 Aug 2024 14:06:05 +0200 Subject: [PATCH 1/5] Added structure for token position tests. --- .../jplag/testutils/LanguageModuleTest.java | 34 +++++++++ .../de/jplag/testutils/TmpFileHolder.java | 14 ++++ .../datacollector/InlineTestData.java | 3 +- .../datacollector/TestDataCollector.java | 31 ++++++++ .../datacollector/TokenPositionTestData.java | 74 +++++++++++++++++++ .../java/de/jplag/java/JavaLanguageTest.java | 2 + 6 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java create mode 100644 language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java diff --git a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java index 36d997f7be..dd0b3a8a64 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java +++ b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java @@ -14,6 +14,7 @@ import java.util.Collection; import java.util.List; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -32,6 +33,7 @@ import de.jplag.testutils.datacollector.TestData; import de.jplag.testutils.datacollector.TestDataCollector; import de.jplag.testutils.datacollector.TestSourceIgnoredLinesCollector; +import de.jplag.testutils.datacollector.TokenPositionTestData; /** * Base class for language module tests. Automatically adds all common tests types for jplag languages. @@ -196,6 +198,33 @@ final List testTokenSequenceData() { return ignoreEmptyTestType(this.collector.getTokenSequenceTest()); } + @ParameterizedTest + @MethodSource("getTokenPositionTestData") + @DisplayName("Tests if the extracted tokens contain the tokens specified in the test files.") + final void testTokenPositions(TokenPositionTestData testData) throws ParsingException, IOException { + List extractedTokens = parseTokens(testData); + List failedTokens = new ArrayList<>(); + + for (TokenPositionTestData.TokenData expectedToken : testData.getExpectedTokens()) { + TokenType expectedType = this.languageTokens.stream().filter(type -> type.toString().equals(expectedToken.typeName())).findFirst().get(); + + if (extractedTokens.stream().noneMatch(token -> token.getType() == expectedType && token.getLine() == expectedToken.line() + && token.getColumn() == expectedToken.col() && token.getLength() == expectedToken.length())) { + failedTokens.add(expectedToken); + } + } + + if (!failedTokens.isEmpty()) { + String failDescriptors = String.join(System.lineSeparator(), failedTokens.stream() + .map(token -> token.typeName() + " at (" + token.line() + ":" + token.col() + ") with length " + token.length()).toList()); + fail("Some tokens weren't extracted with the correct properties:" + System.lineSeparator() + failDescriptors); + } + } + + final List getTokenPositionTestData() { + return this.collector.getTokenPositionTestData(); + } + /** * Tests all configured test sources for a monotone order of tokens * @param data The test source @@ -251,6 +280,11 @@ final void collectTestData() { collectTestData(this.collector); } + @AfterAll + final void releaseTmpFiles() { + TmpFileHolder.deleteTmpFiles(); + } + private List parseTokens(TestData source) throws ParsingException, IOException { List tokens = source.parseTokens(this.language); logger.info(TokenPrinter.printTokens(tokens)); diff --git a/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java b/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java new file mode 100644 index 0000000000..153c9aec36 --- /dev/null +++ b/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java @@ -0,0 +1,14 @@ +package de.jplag.testutils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class TmpFileHolder { + public static List tmpFiles = new ArrayList<>(); + + public static void deleteTmpFiles() { + tmpFiles.forEach(File::delete); + tmpFiles.clear(); + } +} diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java index 8d93f7e156..dc59f7de8b 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java @@ -8,6 +8,7 @@ import de.jplag.Language; import de.jplag.ParsingException; import de.jplag.Token; +import de.jplag.testutils.TmpFileHolder; import de.jplag.util.FileUtils; /** @@ -25,7 +26,7 @@ public List parseTokens(Language language) throws ParsingException, IOExc File file = File.createTempFile("testSource", language.suffixes()[0]); FileUtils.write(file, this.testData); List tokens = language.parse(Collections.singleton(file)); - file.delete(); + TmpFileHolder.tmpFiles.add(file); return tokens; } diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java index d5a929d06f..0291b4d608 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java @@ -1,10 +1,13 @@ package de.jplag.testutils.datacollector; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -18,6 +21,7 @@ public class TestDataCollector { private final List tokenCoverageData; private final List containedTokenData; private final List tokenSequenceTest; + private final List tokenPositionTestData; private final List allTestData; @@ -34,6 +38,7 @@ public TestDataCollector(File testFileLocation) { this.tokenCoverageData = new ArrayList<>(); this.containedTokenData = new ArrayList<>(); this.tokenSequenceTest = new ArrayList<>(); + this.tokenPositionTestData = new ArrayList<>(); this.allTestData = new ArrayList<>(); } @@ -73,6 +78,28 @@ public TestDataContext inlineSource(String... sources) { return new TestDataContext(data); } + /** + * Adds all files from the given directory for token position tests. The sources can still be used for other tests, + * using the returned {@link TestDataContext} + * @param directoryName The name of the directory containing the token position tests. + * @return The context containing the added sources + * @throws IOException If the files cannot be read + */ + public TestDataContext addTokenPositionTests(String directoryName) { + File dir = new File(this.testFileLocation, directoryName); + Set allData = new HashSet<>(); + for (File file : Objects.requireNonNull(dir.listFiles())) { + try { + TokenPositionTestData data = new TokenPositionTestData(file); + allData.add(data); + this.tokenPositionTestData.add(data); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return new TestDataContext(allData); + } + /** * @return The test data that should be checked for source coverage */ @@ -101,6 +128,10 @@ public List getTokenSequenceTest() { return Collections.unmodifiableList(tokenSequenceTest); } + public List getTokenPositionTestData() { + return Collections.unmodifiableList(this.tokenPositionTestData); + } + /** * @return The list of all test data */ diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java new file mode 100644 index 0000000000..e31429f7d8 --- /dev/null +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java @@ -0,0 +1,74 @@ +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.TmpFileHolder; +import de.jplag.util.FileUtils; + +public class TokenPositionTestData implements TestData { + private final List sourceLines; + private final List expectedTokens; + + private final String descriptor; + + 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 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 col = sourceLine.indexOf('|'); + String[] parts = sourceLine.split(" ", 0); + + String typeName = parts[parts.length - 2]; + int length = Integer.parseInt(parts[parts.length - 1]); + this.expectedTokens.add(new TokenData(typeName, currentLine, col, length)); + } + } + } + + @Override + public List parseTokens(Language language) throws ParsingException, IOException { + File file = File.createTempFile("testSource", language.suffixes()[0]); + FileUtils.write(file, String.join(System.lineSeparator(), sourceLines)); + List tokens = language.parse(Collections.singleton(file)); + TmpFileHolder.tmpFiles.add(file); + return tokens; + } + + @Override + public String[] getSourceLines() { + return this.sourceLines.toArray(new String[0]); + } + + @Override + public String describeTestSource() { + return this.descriptor; + } + + public List getExpectedTokens() { + return expectedTokens; + } + + public record TokenData(String typeName, int line, int col, int length) { + } +} diff --git a/languages/java/src/test/java/de/jplag/java/JavaLanguageTest.java b/languages/java/src/test/java/de/jplag/java/JavaLanguageTest.java index e54955781a..a4f89d9bb0 100644 --- a/languages/java/src/test/java/de/jplag/java/JavaLanguageTest.java +++ b/languages/java/src/test/java/de/jplag/java/JavaLanguageTest.java @@ -73,6 +73,8 @@ protected void collectTestData(TestDataCollector collector) { collector.testFile("AnonymousVariables.java").testTokenSequence(J_CLASS_BEGIN, J_METHOD_BEGIN, J_VARDEF, J_IF_BEGIN, J_IF_END, J_METHOD_END, J_CLASS_END); + + collector.addTokenPositionTests("tokenPositions"); } @Override From 465316ae6d96af426e601ebb1de2a6e9f02b7f16 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 7 Aug 2024 14:12:22 +0200 Subject: [PATCH 2/5] Added missing test source. --- .../test/resources/de/jplag/java/tokenPositions/VarDef_1.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/VarDef_1.java diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/VarDef_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/VarDef_1.java new file mode 100644 index 0000000000..276f17351b --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/VarDef_1.java @@ -0,0 +1,4 @@ +>class Test { +> int test; +$ | J_VARDEF 8 +>} From 458e8de8d09150489297e2199e82e986eeb8d7e9 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 7 Aug 2024 14:26:06 +0200 Subject: [PATCH 3/5] Ignoring empty tests for token positions. --- .../src/test/java/de/jplag/testutils/LanguageModuleTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java index dd0b3a8a64..229bf690dc 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java +++ b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java @@ -222,7 +222,7 @@ final void testTokenPositions(TokenPositionTestData testData) throws ParsingExce } final List getTokenPositionTestData() { - return this.collector.getTokenPositionTestData(); + return ignoreEmptyTestType(this.collector.getTokenPositionTestData()); } /** From 4f512d5c71c60de57f4469cec58164f3f96d48f2 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 13 Aug 2024 22:42:06 +0200 Subject: [PATCH 4/5] Added documentation and better error messages. --- docs/4.-Adding-New-Languages.md | 25 +++++++++++++++++++ .../jplag/testutils/LanguageModuleTest.java | 17 ++++++++++--- .../de/jplag/testutils/TmpFileHolder.java | 6 +++++ .../datacollector/TokenPositionTestData.java | 18 +++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/docs/4.-Adding-New-Languages.md b/docs/4.-Adding-New-Languages.md index a4dbf71f00..a596e3e3cd 100644 --- a/docs/4.-Adding-New-Languages.md +++ b/docs/4.-Adding-New-Languages.md @@ -510,6 +510,31 @@ protected File getTestFileLocation() { } ``` +## Testing token positions + +The precise position of a token can be relevant for the visualization in the report viewer. To make sure the token positions are extracted correctly language modules should include some test for that. + +Writing such tests can be done using a specific syntax in the test sources directly. +Such a file can look like this: +```java +>class Test { +> int test; +$ | J_VARDEF 8 +>} +``` + +Every line that is prefixed with '>' will be interpreted as a line of test source code. + +Every line starting with '$' contains information about one expected token. The token is expected in the first source line above this one. +The '|' marks the column the token should be in. It is followed by one space, then the name of the token (The name of the enum constant). +Finally separated with another space is the length of the token. +A single file may contain any number of tokens. +The test will verify that at least one token with those exact properties exist. + +These test files have to be added in the TestDataCollector. Put all test files in a single directory and specify it though collector.addTokenPositionTests(""). +If the directory is in the default location for test files, a relative path is enough, otherwise a full path has to be specified. + + # Adding code highlighting to the report-viewer To ensure your language gets properly registered and its code is correctly highlighted in the report-viewer: 1) Add your language to the `ParserLanguage` enum in 'src/model/Language.ts'. As the value for the entry use its frontend name. diff --git a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java index 229bf690dc..1ec50f7d15 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java +++ b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java @@ -176,7 +176,7 @@ final List testTokensContainedData() { final void testTokenSequence(TestDataCollector.TokenListTest test) throws ParsingException, IOException { List actual = extractTokenTypes(test.data()); List expected = new ArrayList<>(test.tokens()); - if (expected.get(expected.size() - 1) != SharedTokenType.FILE_END) { + if (expected.getLast() != SharedTokenType.FILE_END) { expected.add(SharedTokenType.FILE_END); } assertTokensMatch(expected, actual, "Extracted token from " + test.data().describeTestSource() + " does not match expected sequence."); @@ -198,6 +198,12 @@ final List testTokenSequenceData() { return ignoreEmptyTestType(this.collector.getTokenSequenceTest()); } + /** + * Tests if the tokens specified for the token position tests are present in the sources + * @param testData The specifications of the expected tokens and the test source + * @throws ParsingException If the parsing fails + * @throws IOException If IO operations fail. If this happens, that should be unrelated to the test itself. + */ @ParameterizedTest @MethodSource("getTokenPositionTestData") @DisplayName("Tests if the extracted tokens contain the tokens specified in the test files.") @@ -206,7 +212,8 @@ final void testTokenPositions(TokenPositionTestData testData) throws ParsingExce List failedTokens = new ArrayList<>(); for (TokenPositionTestData.TokenData expectedToken : testData.getExpectedTokens()) { - TokenType expectedType = this.languageTokens.stream().filter(type -> type.toString().equals(expectedToken.typeName())).findFirst().get(); + TokenType expectedType = this.languageTokens.stream().filter(type -> type.toString().equals(expectedToken.typeName())).findFirst() + .orElseThrow(() -> new IOException(String.format("The token type %s does not exist.", expectedToken.typeName()))); if (extractedTokens.stream().noneMatch(token -> token.getType() == expectedType && token.getLine() == expectedToken.line() && token.getColumn() == expectedToken.col() && token.getLength() == expectedToken.length())) { @@ -221,6 +228,9 @@ final void testTokenPositions(TokenPositionTestData testData) throws ParsingExce } } + /** + * @return All token positions tests that are configured + */ final List getTokenPositionTestData() { return ignoreEmptyTestType(this.collector.getTokenPositionTestData()); } @@ -260,8 +270,7 @@ final void testMonotoneTokenOrder(TestData data) throws ParsingException, IOExce final void testTokenSequencesEndsWithFileEnd(TestData data) throws ParsingException, IOException { List tokens = parseTokens(data); - assertEquals(SharedTokenType.FILE_END, tokens.get(tokens.size() - 1).getType(), - "Last token in " + data.describeTestSource() + " is not file end."); + assertEquals(SharedTokenType.FILE_END, tokens.getLast().getType(), "Last token in " + data.describeTestSource() + " is not file end."); } /** diff --git a/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java b/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java index 153c9aec36..da1a7feafa 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java +++ b/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java @@ -4,9 +4,15 @@ 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 TmpFileHolder { public static List tmpFiles = new ArrayList<>(); + /** + * Deletes all temporary files that have been created up to this point + */ public static void deleteTmpFiles() { tmpFiles.forEach(File::delete); tmpFiles.clear(); diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java index e31429f7d8..f6ef70d75c 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java @@ -12,12 +12,20 @@ import de.jplag.testutils.TmpFileHolder; 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 cna be used as regular test sources. + */ public class TokenPositionTestData implements TestData { private final List sourceLines; private final List 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<>(); @@ -65,10 +73,20 @@ public String describeTestSource() { return this.descriptor; } + /** + * @return A list of the expected tokens for this test source + */ public List getExpectedTokens() { return expectedTokens; } + /** + * Information about a single token + * @param typeName The name of the token type + * @param line The line the token is in + * @param col The column the token is in + * @param length The length of the token + */ public record TokenData(String typeName, int line, int col, int length) { } } From 0923193e9dd799ca950ca732de0eda471dd2a36a Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Mon, 26 Aug 2024 17:31:06 +0200 Subject: [PATCH 5/5] Improved code quality and fixed typos. --- docs/4.-Adding-New-Languages.md | 8 +++---- .../jplag/testutils/LanguageModuleTest.java | 16 ++++++++------ ...leHolder.java => TemporaryFileHolder.java} | 10 ++++----- .../datacollector/InlineTestData.java | 4 ++-- .../datacollector/TestDataCollector.java | 10 ++++----- .../datacollector/TokenPositionTestData.java | 22 +++++++++---------- .../java/de/jplag/rlang/RParserAdapter.java | 4 ++-- 7 files changed, 38 insertions(+), 36 deletions(-) rename language-testutils/src/test/java/de/jplag/testutils/{TmpFileHolder.java => TemporaryFileHolder.java} (59%) diff --git a/docs/4.-Adding-New-Languages.md b/docs/4.-Adding-New-Languages.md index a596e3e3cd..00cc28eaff 100644 --- a/docs/4.-Adding-New-Languages.md +++ b/docs/4.-Adding-New-Languages.md @@ -512,7 +512,7 @@ protected File getTestFileLocation() { ## Testing token positions -The precise position of a token can be relevant for the visualization in the report viewer. To make sure the token positions are extracted correctly language modules should include some test for that. +The precise position of a token can be relevant for the visualization in the report viewer. To make sure the token positions are extracted correctly language modules should include some tests for that. Writing such tests can be done using a specific syntax in the test sources directly. Such a file can look like this: @@ -527,11 +527,11 @@ Every line that is prefixed with '>' will be interpreted as a line of test sourc Every line starting with '$' contains information about one expected token. The token is expected in the first source line above this one. The '|' marks the column the token should be in. It is followed by one space, then the name of the token (The name of the enum constant). -Finally separated with another space is the length of the token. +Finally, separated by another space is the length of the token. A single file may contain any number of tokens. -The test will verify that at least one token with those exact properties exist. +The test will verify that at least one token with those exact properties exists. -These test files have to be added in the TestDataCollector. Put all test files in a single directory and specify it though collector.addTokenPositionTests(""). +These test files have to be added in the TestDataCollector. Put all test files in a single directory and specify it through collector.addTokenPositionTests(""). If the directory is in the default location for test files, a relative path is enough, otherwise a full path has to be specified. diff --git a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java index 1ec50f7d15..f659ba2b22 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java +++ b/language-testutils/src/test/java/de/jplag/testutils/LanguageModuleTest.java @@ -215,16 +215,18 @@ final void testTokenPositions(TokenPositionTestData testData) throws ParsingExce TokenType expectedType = this.languageTokens.stream().filter(type -> type.toString().equals(expectedToken.typeName())).findFirst() .orElseThrow(() -> new IOException(String.format("The token type %s does not exist.", expectedToken.typeName()))); - if (extractedTokens.stream().noneMatch(token -> token.getType() == expectedType && token.getLine() == expectedToken.line() - && token.getColumn() == expectedToken.col() && token.getLength() == expectedToken.length())) { + if (extractedTokens.stream().noneMatch(token -> token.getType() == expectedType && token.getLine() == expectedToken.lineNumber() + && token.getColumn() == expectedToken.columnNumber() && token.getLength() == expectedToken.length())) { failedTokens.add(expectedToken); } } if (!failedTokens.isEmpty()) { - String failDescriptors = String.join(System.lineSeparator(), failedTokens.stream() - .map(token -> token.typeName() + " at (" + token.line() + ":" + token.col() + ") with length " + token.length()).toList()); - fail("Some tokens weren't extracted with the correct properties:" + System.lineSeparator() + failDescriptors); + String failureDescriptors = String.join(System.lineSeparator(), + failedTokens.stream().map( + token -> token.typeName() + " at (" + token.lineNumber() + ":" + token.columnNumber() + ") with length " + token.length()) + .toList()); + fail("Some tokens weren't extracted with the correct properties:" + System.lineSeparator() + failureDescriptors); } } @@ -290,8 +292,8 @@ final void collectTestData() { } @AfterAll - final void releaseTmpFiles() { - TmpFileHolder.deleteTmpFiles(); + final void deleteTemporaryFiles() { + TemporaryFileHolder.deleteTemporaryFiles(); } private List parseTokens(TestData source) throws ParsingException, IOException { diff --git a/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java b/language-testutils/src/test/java/de/jplag/testutils/TemporaryFileHolder.java similarity index 59% rename from language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java rename to language-testutils/src/test/java/de/jplag/testutils/TemporaryFileHolder.java index da1a7feafa..98a95fb7fb 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/TmpFileHolder.java +++ b/language-testutils/src/test/java/de/jplag/testutils/TemporaryFileHolder.java @@ -7,14 +7,14 @@ /** * Stores all temporary files that are created for a {@link LanguageModuleTest} and provides the option to delete them */ -public class TmpFileHolder { - public static List tmpFiles = new ArrayList<>(); +public class TemporaryFileHolder { + public static List temporaryFiles = new ArrayList<>(); /** * Deletes all temporary files that have been created up to this point */ - public static void deleteTmpFiles() { - tmpFiles.forEach(File::delete); - tmpFiles.clear(); + public static void deleteTemporaryFiles() { + temporaryFiles.forEach(File::delete); + temporaryFiles.clear(); } } diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java index dc59f7de8b..6a87110234 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/InlineTestData.java @@ -8,7 +8,7 @@ import de.jplag.Language; import de.jplag.ParsingException; import de.jplag.Token; -import de.jplag.testutils.TmpFileHolder; +import de.jplag.testutils.TemporaryFileHolder; import de.jplag.util.FileUtils; /** @@ -26,7 +26,7 @@ public List parseTokens(Language language) throws ParsingException, IOExc File file = File.createTempFile("testSource", language.suffixes()[0]); FileUtils.write(file, this.testData); List tokens = language.parse(Collections.singleton(file)); - TmpFileHolder.tmpFiles.add(file); + TemporaryFileHolder.temporaryFiles.add(file); return tokens; } diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java index 0291b4d608..9b45e8d8b6 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TestDataCollector.java @@ -86,18 +86,18 @@ public TestDataContext inlineSource(String... sources) { * @throws IOException If the files cannot be read */ public TestDataContext addTokenPositionTests(String directoryName) { - File dir = new File(this.testFileLocation, directoryName); - Set allData = new HashSet<>(); - for (File file : Objects.requireNonNull(dir.listFiles())) { + File directory = new File(this.testFileLocation, directoryName); + Set allTestsInDirectory = new HashSet<>(); + for (File file : Objects.requireNonNull(directory.listFiles())) { try { TokenPositionTestData data = new TokenPositionTestData(file); - allData.add(data); + allTestsInDirectory.add(data); this.tokenPositionTestData.add(data); } catch (IOException e) { throw new RuntimeException(e); } } - return new TestDataContext(allData); + return new TestDataContext(allTestsInDirectory); } /** diff --git a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java index f6ef70d75c..d46288041a 100644 --- a/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java +++ b/language-testutils/src/test/java/de/jplag/testutils/datacollector/TokenPositionTestData.java @@ -9,12 +9,12 @@ import de.jplag.Language; import de.jplag.ParsingException; import de.jplag.Token; -import de.jplag.testutils.TmpFileHolder; +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 cna be used as regular test sources. + * information for tests. The sources can be used as regular test sources. */ public class TokenPositionTestData implements TestData { private final List sourceLines; @@ -44,12 +44,12 @@ private void readFile(File testFile) throws IOException { } if (sourceLine.charAt(0) == '$') { - int col = sourceLine.indexOf('|'); - String[] parts = sourceLine.split(" ", 0); + int column = sourceLine.indexOf('|'); + String[] tokenDescriptionParts = sourceLine.split(" ", 0); - String typeName = parts[parts.length - 2]; - int length = Integer.parseInt(parts[parts.length - 1]); - this.expectedTokens.add(new TokenData(typeName, currentLine, col, length)); + String typeName = tokenDescriptionParts[tokenDescriptionParts.length - 2]; + int length = Integer.parseInt(tokenDescriptionParts[tokenDescriptionParts.length - 1]); + this.expectedTokens.add(new TokenData(typeName, currentLine, column, length)); } } } @@ -59,7 +59,7 @@ public List parseTokens(Language language) throws ParsingException, IOExc File file = File.createTempFile("testSource", language.suffixes()[0]); FileUtils.write(file, String.join(System.lineSeparator(), sourceLines)); List tokens = language.parse(Collections.singleton(file)); - TmpFileHolder.tmpFiles.add(file); + TemporaryFileHolder.temporaryFiles.add(file); return tokens; } @@ -83,10 +83,10 @@ public List getExpectedTokens() { /** * Information about a single token * @param typeName The name of the token type - * @param line The line the token is in - * @param col The column the token is in + * @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 line, int col, int length) { + public record TokenData(String typeName, int lineNumber, int columnNumber, int length) { } } diff --git a/languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java b/languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java index e3c3aa6bce..e0ee215f75 100644 --- a/languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java +++ b/languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java @@ -83,8 +83,8 @@ private void parseFile(File file) throws ParsingException { /** * Adds a new {@link Token} to the current token list. * @param type the type of the new {@link Token} - * @param line the line of the Token in the current file - * @param start the start column of the Token in the line + * @param line the lineNumber of the Token in the current file + * @param start the start column of the Token in the lineNumber * @param length the length of the Token */ /* package-private */ void addToken(TokenType type, int line, int start, int length) {