forked from antlr/intellij-plugin-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/antlrGH-433
- Loading branch information
Showing
5 changed files
with
221 additions
and
11 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
98 changes: 98 additions & 0 deletions
98
src/test/java/org/antlr/intellij/plugin/editor/Issue540Test.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,98 @@ | ||
package org.antlr.intellij.plugin.editor; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.application.WriteAction; | ||
import com.intellij.openapi.editor.Document; | ||
import com.intellij.openapi.fileEditor.FileDocumentManager; | ||
import com.intellij.openapi.vfs.LocalFileSystem; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.testFramework.VfsTestUtil; | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase; | ||
import org.antlr.intellij.plugin.ANTLRv4PluginController; | ||
import org.antlr.intellij.plugin.TestUtils; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Test; | ||
import java.io.File; | ||
|
||
public class Issue540Test extends BasePlatformTestCase { | ||
|
||
private final File LEXER_FILE = new File(getTestDataPath()+"TestLexer.g4"); | ||
private final File PARSER_FILE = new File(getTestDataPath()+"TestParser.g4"); | ||
private final File TOKENS_FILE = new File(getTestDataGenPath()+"TestLexer.tokens"); | ||
|
||
@Test | ||
public void test_shouldOnlyCreateTokensWhenModified() { | ||
|
||
// Create files and controller | ||
VirtualFile lexerFile = createAndOpenFile(LEXER_FILE, "lexer grammar TestLexer;\nTOKEN1: 'TOKEN1';"); | ||
VirtualFile parserFile = createAndOpenFile(PARSER_FILE, "parser grammar TestParser;\noptions {tokenVocab=TestLexer;}\nstartRule: TOKEN1;"); | ||
ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(getProject()); | ||
|
||
// No tokens should be created yet | ||
assertFalse(TOKENS_FILE.exists()); | ||
|
||
// Add one token to file, switch to parser file and check that tokens were created | ||
switchToFile(lexerFile); | ||
addLineToCurrentFile("TOKEN2: 'TOKEN2';", lexerFile, controller); | ||
switchToFile(parserFile); | ||
assertTrue(TOKENS_FILE.exists()); | ||
long lastModified1 = TOKENS_FILE.lastModified(); | ||
|
||
// Switch to lexer file, add token, and check if tokens file was updated | ||
switchToFile(lexerFile); | ||
addLineToCurrentFile("TOKEN3: 'TOKEN3';", lexerFile, controller); | ||
switchToFile(parserFile); | ||
long lastModified2 = new File(TOKENS_FILE.getAbsolutePath()).lastModified(); | ||
assertTrue(lastModified2 > lastModified1); | ||
|
||
// Switch back and forth again, and make sure tokens are not recreated | ||
switchToFile(lexerFile); | ||
switchToFile(parserFile); | ||
assertEquals(lastModified2, TOKENS_FILE.lastModified()); | ||
|
||
} | ||
|
||
private VirtualFile createAndOpenFile(File file, String contents) { | ||
String absPath = file.getAbsolutePath(); | ||
VfsTestUtil.overwriteTestData(absPath, contents); | ||
VirtualFile virtualFile = WriteAction.computeAndWait(() -> | ||
LocalFileSystem.getInstance().refreshAndFindFileByPath(absPath)); | ||
myFixture.openFileInEditor(virtualFile); | ||
return virtualFile; | ||
} | ||
|
||
private String getTestDataGenPath() { | ||
return getTestDataPath() + "gen/"; | ||
} | ||
|
||
private void addLineToCurrentFile(String line, VirtualFile file, ANTLRv4PluginController controller) { | ||
ApplicationManager.getApplication().runWriteAction(() -> { | ||
Document doc = myFixture.getEditor().getDocument(); | ||
doc.setText(doc.getText() + "\n" + line); | ||
PsiDocumentManager.getInstance(getProject()).commitDocument(doc); | ||
FileDocumentManager.getInstance().saveDocument(doc); | ||
if (controller != null) controller.grammarFileSavedEvent(file); | ||
}); | ||
} | ||
|
||
private void switchToFile(VirtualFile vf) { | ||
myFixture.openFileInEditor(vf); | ||
} | ||
|
||
@Override | ||
protected String getTestDataPath() { | ||
return "src/test/resources/"; | ||
} | ||
|
||
@Override | ||
protected void tearDown() throws Exception { | ||
FileUtils.forceDeleteOnExit(LEXER_FILE); | ||
FileUtils.forceDeleteOnExit(PARSER_FILE); | ||
FileUtils.forceDeleteOnExit(new File(getTestDataGenPath())); | ||
TestUtils.tearDownIgnoringObjectNotDisposedException(() -> { | ||
super.tearDown(); | ||
}); | ||
} | ||
|
||
} |
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
50 changes: 50 additions & 0 deletions
50
src/test/java/org/antlr/intellij/plugin/editor/Issue569Test.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,50 @@ | ||
package org.antlr.intellij.plugin.editor; | ||
|
||
import com.intellij.openapi.command.WriteCommandAction; | ||
import com.intellij.openapi.editor.EditorFactory; | ||
import com.intellij.openapi.fileTypes.FileTypeManager; | ||
import com.intellij.openapi.fileTypes.FileTypes; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase; | ||
import org.antlr.intellij.plugin.ANTLRv4FileType; | ||
import org.antlr.intellij.plugin.ANTLRv4PluginController; | ||
import org.antlr.intellij.plugin.TestUtils; | ||
import org.junit.Test; | ||
|
||
public class Issue569Test extends BasePlatformTestCase { | ||
|
||
@Test | ||
public void test_shouldReassignExtensionType() { | ||
|
||
// Reassign extension temporarily | ||
WriteCommandAction.runWriteCommandAction(getProject(), () -> | ||
FileTypeManager.getInstance().associateExtension(FileTypes.PLAIN_TEXT, "g4")); | ||
|
||
// Test File | ||
VirtualFile file = myFixture.configureByFile("FooParser.g4").getVirtualFile(); | ||
assertEquals(file.getFileType(), FileTypes.PLAIN_TEXT); | ||
|
||
// Create controller and initialize it | ||
ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(getProject()); | ||
assertNotNull(controller); | ||
controller.initComponent(); | ||
|
||
// Check if file type is reassigned | ||
assertEquals(file.getFileType(), ANTLRv4FileType.INSTANCE); | ||
|
||
} | ||
|
||
@Override | ||
protected String getTestDataPath() { | ||
return "src/test/resources/references"; | ||
} | ||
|
||
@Override | ||
protected void tearDown() throws Exception { | ||
TestUtils.tearDownIgnoringObjectNotDisposedException(() -> { | ||
EditorFactory.getInstance().releaseEditor(myFixture.getEditor()); | ||
super.tearDown(); | ||
}); | ||
} | ||
|
||
} |