Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage to the lexer #55

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ tasks {
?: tasksUsingDownloadedJbr.add(this)
}

withType<Test> {
systemProperty("idea.test.execution.policy", "org.nixos.idea.NixTestExecutionPolicy")
systemProperty("plugin.testDataPath", rootProject.rootDir.resolve("src/test/testData").path)
}

task("metadata") {
outputs.upToDateWhen { false }
doLast {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/nixos/idea/NixTestExecutionPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.nixos.idea;

import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy;

/**
* Test execution policy referenced by the build setup build.gradle.kts.
* Tests like {@link org.nixos.idea.lang.LexerFilesTest} use it to locate the test data directory.
*/
@SuppressWarnings("unused")
public class NixTestExecutionPolicy extends IdeaTestExecutionPolicy {
@Override
protected String getName() {
return "Nix";
}

@Override
public String getHomePath() {
return System.getProperty("plugin.testDataPath");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I am wondering if this could also replace ParsingTest.getTestDataPath() and ParsingFailTest.getTestDataPath().

@Override
protected String getTestDataPath() {
return "src/test/testData";
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should work, I think. As far as I understand this is only working because it's using the current working dir (i.e. the root project directory) as default.

}
}
70 changes: 70 additions & 0 deletions src/test/java/org/nixos/idea/lang/LexerFilesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.nixos.idea.lang;

import com.intellij.lexer.Lexer;
import com.intellij.testFramework.LexerTestCase;
import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

/**
* Parameterized lexer test for all Nix files inside src/test/testData/ParsingTest/.
*/
@RunWith(Parameterized.class)
public class LexerFilesTest extends LexerTestCase {
private static final String DIRECTORY_NAME = "ParsingTest";

private final String filePath;

public LexerFilesTest(@NotNull String filePath) {
this.filePath = filePath;
}

/**
* Collects all the files to run this test with.
*
* @return Iterable of files src/test/testData/ParsingTest/*.nix.
*/
@Parameterized.Parameters(name = "{0}")
public static Iterable<String> filePaths() throws IOException {
var baseDir = Paths.get(IdeaTestExecutionPolicy.getHomePathWithPolicy()).resolve(DIRECTORY_NAME);
var fileStream = Files.find(baseDir,
Integer.MAX_VALUE,
(path1, attributes) -> attributes.isRegularFile() && path1.toString().endsWith(".nix"));

try (fileStream) {
return fileStream.map(path -> baseDir.relativize(path).toString()).collect(Collectors.toList());
}
}

@Test
public void check() {
doFileTest("nix");
}

@Override
protected Lexer createLexer() {
return new NixLexer();
}

@Override
protected String getDirPath() {
return DIRECTORY_NAME;
}

@Override
protected @NotNull String getTestName(boolean lowercaseFirstLetter) {
return filePath.replace(".nix", "");
}

@Override
protected @NotNull String getExpectedFileExtension() {
return ".lexer.txt";
}
}
6 changes: 6 additions & 0 deletions src/test/testData/ParsingTest/Assertion.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
⁠assert ('assert')
WHITE_SPACE (' ')
ID ('e1')
; (';')
WHITE_SPACE (' ')
ID ('e2')
7 changes: 7 additions & 0 deletions src/test/testData/ParsingTest/Boolean.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ ('[')
WHITE_SPACE ('\n')
ID ('true')
WHITE_SPACE ('\n')
ID ('false')
WHITE_SPACE ('\n')
] (']')
109 changes: 109 additions & 0 deletions src/test/testData/ParsingTest/Function.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
[ ('[')
WHITE_SPACE ('\n')
( ('(')
ID ('x')
: (':')
WHITE_SPACE (' ')
ID ('x')
) (')')
WHITE_SPACE ('\n')
( ('(')
ID ('x')
: (':')
WHITE_SPACE (' ')
ID ('y')
: (':')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
+ ('+')
WHITE_SPACE (' ')
ID ('y')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
, (',')
WHITE_SPACE (' ')
ID ('y')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
+ ('+')
WHITE_SPACE (' ')
ID ('y')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
, (',')
WHITE_SPACE (' ')
ID ('y')
, (',')
WHITE_SPACE (' ')
... ('...')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
+ ('+')
WHITE_SPACE (' ')
ID ('y')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
? ('?')
WHITE_SPACE (' ')
STRING_OPEN ('"')
STR ('default')
STRING_CLOSE ('"')
WHITE_SPACE (' ')
} ('}')
WHITE_SPACE (' ')
: (':')
WHITE_SPACE (' ')
ID ('x')
) (')')
WHITE_SPACE ('\n')
( ('(')
ID ('args')
@ ('@')
{ ('{')
WHITE_SPACE (' ')
... ('...')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('args')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
... ('...')
WHITE_SPACE (' ')
} ('}')
WHITE_SPACE (' ')
@ ('@')
WHITE_SPACE (' ')
ID ('args')
: (':')
WHITE_SPACE (' ')
ID ('args')
) (')')
WHITE_SPACE ('\n')
] (']')
6 changes: 6 additions & 0 deletions src/test/testData/ParsingTest/FunctionEmptySet.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ ('{')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('null')
46 changes: 46 additions & 0 deletions src/test/testData/ParsingTest/FunctionTrailingComma.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[ ('[')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
, (',')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('null')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
, (',')
WHITE_SPACE (' ')
ID ('y')
, (',')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('null')
) (')')
WHITE_SPACE ('\n')
( ('(')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
? ('?')
WHITE_SPACE (' ')
ID ('null')
, (',')
WHITE_SPACE (' ')
} ('}')
: (':')
WHITE_SPACE (' ')
ID ('null')
) (')')
WHITE_SPACE ('\n')
] (']')
9 changes: 9 additions & 0 deletions src/test/testData/ParsingTest/Identifier.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ ('[')
WHITE_SPACE ('\n')
ID ('_')
WHITE_SPACE ('\n')
ID ('A')
WHITE_SPACE ('\n')
ID ('a-0'')
WHITE_SPACE ('\n')
] (']')
11 changes: 11 additions & 0 deletions src/test/testData/ParsingTest/If.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
⁠if ('if')
WHITE_SPACE (' ')
ID ('e1')
WHITE_SPACE (' ')
⁠then ('then')
WHITE_SPACE (' ')
ID ('e2')
WHITE_SPACE (' ')
⁠else ('else')
WHITE_SPACE (' ')
ID ('e3')
19 changes: 19 additions & 0 deletions src/test/testData/ParsingTest/LegacyLet.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
⁠let ('let')
WHITE_SPACE (' ')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
= ('=')
WHITE_SPACE (' ')
INT ('1')
; (';')
WHITE_SPACE (' ')
ID ('body')
WHITE_SPACE (' ')
= ('=')
WHITE_SPACE (' ')
ID ('x')
; (';')
WHITE_SPACE (' ')
} ('}')
23 changes: 23 additions & 0 deletions src/test/testData/ParsingTest/LegacyLetInList.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[ ('[')
WHITE_SPACE (' ')
⁠let ('let')
WHITE_SPACE (' ')
{ ('{')
WHITE_SPACE (' ')
ID ('x')
WHITE_SPACE (' ')
= ('=')
WHITE_SPACE (' ')
INT ('1')
; (';')
WHITE_SPACE (' ')
ID ('body')
WHITE_SPACE (' ')
= ('=')
WHITE_SPACE (' ')
ID ('x')
; (';')
WHITE_SPACE (' ')
} ('}')
WHITE_SPACE (' ')
] (']')
3 changes: 3 additions & 0 deletions src/test/testData/ParsingTest/LegacyOrAsArgument.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID ('f')
WHITE_SPACE (' ')
⁠or ('or')
21 changes: 21 additions & 0 deletions src/test/testData/ParsingTest/LegacyOrAsAttribute.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
⁠let ('let')
WHITE_SPACE (' ')
⁠or ('or')
WHITE_SPACE (' ')
= ('=')
WHITE_SPACE (' ')
INT ('1')
; (';')
WHITE_SPACE (' ')
⁠in ('in')
WHITE_SPACE (' ')
{ ('{')
WHITE_SPACE (' ')
⁠inherit ('inherit')
WHITE_SPACE (' ')
⁠or ('or')
; (';')
WHITE_SPACE (' ')
} ('}')
. ('.')
⁠or ('or')
11 changes: 11 additions & 0 deletions src/test/testData/ParsingTest/LegacyOrInList.lexer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ ('[')
WHITE_SPACE (' ')
ID ('before')
WHITE_SPACE (' ')
ID ('f')
WHITE_SPACE (' ')
⁠or ('or')
WHITE_SPACE (' ')
ID ('after')
WHITE_SPACE (' ')
] (']')
Loading