Skip to content

Commit

Permalink
Improve/refactor (action) tests
Browse files Browse the repository at this point in the history
  • Loading branch information
opwvhk committed Nov 14, 2024
1 parent 6142394 commit 791e6b2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ dependencies {
intellijIdeaCommunity("2023.2.6")
//intellijIdeaCommunity("2023.3.6")
//intellijIdeaCommunity("2024.1.6")
//intellijIdeaCommunity("2024.2.1")
//intellijIdeaCommunity("2024.2.4")
//intellijIdeaCommunity("2024.3")
// EAP
//create("IC","243.12818.47")
bundledPlugin("com.intellij.java")
Expand All @@ -74,7 +75,7 @@ dependencies {
//bundledPlugin("com.intellij.modules.json") // TODO: Uncomment when it exists (check at least when requiring 2024.3+)...
bundledPlugin("org.intellij.intelliLang")

// Extra plugin(s); not needed, but maybe useful during development:
// Extra plugin(s); not needed for the plugin, but maybe useful during development:
bundledPlugin("org.intellij.plugins.markdown")
/* Other (bundled) plugins: */
// Define these variables to prevent spell checking errors in the comment below
Expand Down Expand Up @@ -123,6 +124,7 @@ intellijPlatform {
changeNotes.set("""
<p>Version 232.0.1:</p><ul>
<li>Improve threading for previewing quick fixes</li>
<li>Improve/refactor action tests</li>
</ul>
<p>Version 232.0.0:</p><ul>
<li>Upgraded minimum supported JetBrains version to 2023.2</li>
Expand Down
27 changes: 4 additions & 23 deletions src/test/java/opwvhk/intellij/avro_idl/AvroIdlCodeInsightTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.lang.LanguageDocumentation;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.lang.documentation.DocumentationProvider;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.spellchecker.SpellCheckerSeveritiesProvider;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import opwvhk.intellij.avro_idl.inspections.*;
Expand All @@ -16,10 +12,6 @@

@SuppressWarnings("unchecked")
public class AvroIdlCodeInsightTest extends BasePlatformTestCase {

private static final DocumentationProvider DOCUMENTATION_PROVIDER = LanguageDocumentation.INSTANCE.forLanguage(
AvroIdlLanguage.INSTANCE);

@Override
protected String getTestDataPath() {
return "src/test/testData/codeInsight";
Expand All @@ -43,6 +35,7 @@ public void testAnnotator() {
Highlight.error("my-data", "Not a valid identifier: my-data"),
Highlight.error("duplicate", "Field \"duplicate\" is already defined"),
Highlight.error("duplicate", "Field \"duplicate\" is already defined"),
// 11
Highlight.error("@namespace(\"unused\")",
"Type references must not be annotated: Avro < 1.11.1 changes the referenced type, Avro >= 1.11.1 fails to compile."),
Highlight.error("one-letter", "Not a valid identifier: one-letter"),
Expand All @@ -59,6 +52,7 @@ public void testAnnotator() {
Highlight.error("@logicalType(\"timestamp-millis\")",
"The logical type \"timestamp-millis\" requires the underlying type \"long\""),
Highlight.error("56", "@aliases elements must be strings"),
// 21
Highlight.error("\"invites-failure\"", "Not a valid identifier: invites-failure"),
Highlight.error("@logicalType(\"local-timestamp-millis\")",
"The logical type \"local-timestamp-millis\" requires the underlying type \"long\""),
Expand All @@ -73,8 +67,10 @@ public void testAnnotator() {
Highlight.error("@logicalType(\"decimal\")",
"The logical type \"decimal\" requires the underlying type \"bytes\" or \"fixed\""),
Highlight.error("8", "@scale must contain a non-negative number of at most the value of @precision"),
// 31
Highlight.error("@logicalType(\"decimal\")",
"The logical type \"decimal\" requires the underlying type \"bytes\" or \"fixed\""),
// Swap these two when using 2024.3
Highlight.error("@logicalType(\"decimal\")",
"The logical type \"decimal\" requires the underlying type \"bytes\" or \"fixed\""),
Highlight.error("@logicalType(\"decimal\")",
Expand Down Expand Up @@ -481,21 +477,6 @@ public void testMissingErrorQuickFix() {
myFixture.checkResultByFile("MissingErrorFixed.avdl");
}

@SuppressWarnings("unused")
public void _testDocumentation() {
myFixture.configureByFiles("DocumentationTestData.java", "DocumentationTestData.simple");
final PsiElement originalElement = myFixture.getElementAtCaret();
Editor editor = myFixture.getEditor();
PsiElement element = DOCUMENTATION_PROVIDER.getCustomDocumentationElement(editor, originalElement.getContainingFile(), originalElement, editor.getCaretModel().getOffset());
if (element == null) {
element = originalElement;
}

final String generateDoc = DOCUMENTATION_PROVIDER.generateDoc(element, originalElement);
assertNotNull(generateDoc);
assertSameLinesWithFile(getTestDataPath() + "/" + "DocumentationTest.html.expected", generateDoc);
}

@SuppressWarnings("SameParameterValue")
private record Highlight(HighlightSeverity severity, String text, String description) {
static Highlight error(String text, String description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package opwvhk.intellij.avro_idl.actions;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileWrapper;
import com.intellij.testFramework.HeavyPlatformTestCase;
import com.intellij.testFramework.MapDataContext;
import com.intellij.testFramework.TestActionEvent;

import java.io.IOException;
Expand Down Expand Up @@ -61,6 +62,7 @@ public void testNoFiles() throws IOException {

public void testNoSuitableFiles() throws IOException {
AnAction action = ActionManager.getInstance().getAction("AvroIdl.IdlToProtocol");

executeTest(action, false);
}

Expand All @@ -85,19 +87,20 @@ public void testSchemaToIdl() throws IOException {
}

private void executeTest(AnAction action, boolean shouldExecute) throws IOException {
MapDataContext dataContext = new MapDataContext();
LocalFileSystem vfs = LocalFileSystem.getInstance();
dataContext.put(LangDataKeys.VIRTUAL_FILE_ARRAY,
list(inputDirectory, s -> s.map(vfs::refreshAndFindFileByNioFile).toArray(VirtualFile[]::new)));
dataContext.put(CommonDataKeys.PROJECT, getProject());
DataContext dataContext = SimpleDataContext.builder()
.add(LangDataKeys.VIRTUAL_FILE_ARRAY,
list(inputDirectory, s -> s.map(vfs::refreshAndFindFileByNioFile).toArray(VirtualFile[]::new)))
.add(CommonDataKeys.PROJECT, getProject())
.build();

AnActionEvent event = TestActionEvent.createTestEvent(action, dataContext);
action.update(event);
ActionUtil.performDumbAwareUpdate(action, event, false);
Presentation p = event.getPresentation();
assertThat(p.isEnabled()).as("event %s", p.isEnabled() ? "enabled" : "disabled").isEqualTo(shouldExecute);
assertThat(p.isVisible()).as("event %s", p.isVisible() ? "visible" : "hidden").isEqualTo(shouldExecute);

action.actionPerformed(event); // Outside if statement to verify it correctly handles null invocations
ActionUtil.performActionDumbAwareWithCallbacks(action, event);
if (shouldExecute) {
assertSameTextContentRecursive(resultDirectory, outputDirectory);
} else {
Expand Down

0 comments on commit 791e6b2

Please sign in to comment.