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

chore(java): write tests for most of the JavaIconProvider #146

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions .run/Test Plugin.run.xml
thelooter marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test Plugin" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="test" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<EXTENSION ID="com.intellij.execution.ExternalSystemRunConfigurationJavaExtension">
<extension name="coverage">
<pattern>
<option name="PATTERN" value="test.*" />
<option name="ENABLED" value="true" />
<option name="INCLUDE" value="false" />
</pattern>
</extension>
</EXTENSION>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class JavaIconProvider : IconProvider() {
* @param psiElement The [PsiClass] to get the visibility icon for.
* @return The visibility icon based on the class's modifier, or null if not applicable.
*/
private fun getVisibilityIcon(psiElement: PsiClass): Icon? =
internal fun getVisibilityIcon(psiElement: PsiClass): Icon? =
thelooter marked this conversation as resolved.
Show resolved Hide resolved
when {
psiElement.hasModifierProperty(PsiModifier.PUBLIC) -> AllIcons.Nodes.Public
psiElement.hasModifierProperty(PsiModifier.PRIVATE) -> AllIcons.Nodes.Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ package com.github.com.catppuccin.jetbrains_icons.providers

import com.github.catppuccin.jetbrains_icons.Icons
import com.github.catppuccin.jetbrains_icons.providers.JavaIconProvider
import com.github.catppuccin.jetbrains_icons.settings.PluginSettingsState
import com.intellij.icons.AllIcons
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase5
import com.intellij.testFramework.runInEdtAndGet
import com.intellij.ui.LayeredIcon
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

/**
* Test class for JavaIconProvider. This class tests various scenarios for icon provision for Java
* elements.
thelooter marked this conversation as resolved.
Show resolved Hide resolved
*/
@Tag("fast")
thelooter marked this conversation as resolved.
Show resolved Hide resolved
@Tag("javaIcons")
thelooter marked this conversation as resolved.
Show resolved Hide resolved
class JavaIconProviderTest : LightJavaCodeInsightFixtureTestCase5() {

override fun getTestDataPath(): String = "src/test/testData"

private val icons = Icons("mocha")
private val provider = JavaIconProvider()

@Test
@DisplayName("Test icon provision for a standard Java class")
thelooter marked this conversation as resolved.
Show resolved Hide resolved
fun getIcon_javaClass() {
val provider = JavaIconProvider()

fixture.addFileToProject(
"SomeClass.java",
"""
Expand All @@ -29,4 +43,301 @@ class JavaIconProviderTest : LightJavaCodeInsightFixtureTestCase5() {

assertEquals(icons.java_class, icon)
}

@Test
@DisplayName("Test icon provision for a Java interface")
thelooter marked this conversation as resolved.
Show resolved Hide resolved
fun getIcon_javaInterface() {
fixture.addFileToProject(
"SomeInterface.java",
"""
interface SomeInterface { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeInterface"), 1) }

assertEquals(icons.java_interface, icon)
}

@Test
@DisplayName("Test icon provision for a Java enum")
fun getIcon_javaEnum() {
fixture.addFileToProject(
"SomeEnum.java",
"""
enum SomeEnum { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeEnum"), 1) }

assertEquals(icons.java_enum, icon)
}

@Test
@DisplayName("Test icon provision for a Java annotation")
fun getIcon_javaAnnotation() {
fixture.addFileToProject(
"SomeAnnotation.java",
"""
@interface SomeAnnotation { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeAnnotation"), 1) }

assertEquals(icons.java_annotation, icon)
}

@Test
@DisplayName("Test icon provision for a Java record")
fun getIcon_javaRecord() {
fixture.addFileToProject(
"SomeRecord.java",
"""
record SomeRecord() { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeRecord"), 1) }

assertEquals(icons.java_record, icon)
}

@Test
@DisplayName("Test icon provision for a Java exception class")
fun getIcon_javaException() {
fixture.addFileToProject(
"SomeException.java",
"""
class SomeException extends Exception { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeException"), 1) }

assertEquals(icons.java_exception, icon)
}

@Test
@DisplayName("Test icon provision for a Java sealed class")
fun getIcon_javaSealedClass() {
fixture.addFileToProject(
"SomeSealedClass.java",
"""
sealed class SomeSealedClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeSealedClass"), 1) }

assertEquals(icons.java_class_sealed, icon)
}

@Test
@DisplayName("Test icon provision for a Java final class")
fun getIcon_javaFinalClass() {
fixture.addFileToProject(
"SomeFinalClass.java",
"""
final class SomeFinalClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeFinalClass"), 1) }

assertEquals(icons.java_class_final, icon)
}

@Test
@DisplayName("Test icon provision for a Java abstract class")
fun getIcon_javaAbstractClass() {
fixture.addFileToProject(
"SomeAbstractClass.java",
"""
abstract class SomeAbstractClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeAbstractClass"), 1) }

assertEquals(icons.java_class_abstract, icon)
}

@Test
@DisplayName("Test that a static class icon includes a static mark")
fun getIcon_staticClass_hasStaticMark() {
fixture.addFileToProject(
"StaticClass.java",
"""
public class OuterClass {
static class StaticClass { }
}
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("OuterClass.StaticClass"), 1) }

// The icon should be a LayeredIcon with 2 layers - base icon and static mark
assertTrue(icon is LayeredIcon)
assertEquals(2, (icon as LayeredIcon).allLayers.size)
assertEquals(AllIcons.Nodes.StaticMark, icon.allLayers[1])
}

@Test
@DisplayName("Test that a non-static class icon does not include a static mark")
fun getIcon_nonStaticClass_hasNoStaticMark() {
fixture.addFileToProject(
"RegularClass.java",
"""
public class RegularClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("RegularClass"), 1) }

// For non-static classes, the icon should not be layered
assertFalse(icon is LayeredIcon)
}

@Test
@DisplayName("Test that requesting an icon for a non-PsiClass element returns null")
fun getIcon_notPsiClass_returnsNull() {
fixture.addFileToProject(
"SomeClass.java",
"""
class SomeClass {
private int someField;
}
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeClass").fields[0], 1) }

assertNull(icon)
}

@Test
@DisplayName("Test that requesting an icon for a non-Java file returns null")
fun getIcon_nonJavaFile_returnsNull() {
fixture.addFileToProject(
"SomeClass.kt",
"""
class SomeClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeClass"), 1) }

assertNull(icon)
}

@Test
@DisplayName("Test that when Java support is disabled, a generic Java icon is returned")
fun getIcon_javaDisabled_returnsJavaIcon() {
fixture.addFileToProject(
"SomeClass.java",
"""
interface SomeClass { }
"""
.trimIndent(),
)

// Temporarily disable Java support
val originalSetting = PluginSettingsState.instance.javaSupport
PluginSettingsState.instance.javaSupport = false

val icon = runInEdtAndGet { provider.getIcon(fixture.findClass("SomeClass"), 1) }

assertEquals(icons.java, icon)

// Restore original setting
PluginSettingsState.instance.javaSupport = originalSetting
thelooter marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Test visibility icon for a public class")
fun getVisibilityIconForTesting_publicClass_returnsPublicIcon() {
thelooter marked this conversation as resolved.
Show resolved Hide resolved
fixture.addFileToProject(
"PublicClass.java",
"""
public class PublicClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet { provider.getVisibilityIcon(fixture.findClass("PublicClass")) }
thelooter marked this conversation as resolved.
Show resolved Hide resolved

assertEquals(AllIcons.Nodes.Public, icon)
}

@Test
@DisplayName("Test visibility icon for a private class")
fun getVisibilityIconForTesting_privateClass_returnsPrivateIcon() {
fixture.addFileToProject(
"OuterClass.java",
"""
public class OuterClass {
private class PrivateClass { }
}
"""
.trimIndent(),
)

val icon = runInEdtAndGet {
provider.getVisibilityIcon(fixture.findClass("OuterClass.PrivateClass"))
thelooter marked this conversation as resolved.
Show resolved Hide resolved
}

assertEquals(AllIcons.Nodes.Private, icon)
}

@Test
@DisplayName("Test visibility icon for a protected class")
fun getVisibilityIconForTesting_protectedClass_returnsProtectedIcon() {
fixture.addFileToProject(
"OuterClass.java",
"""
public class OuterClass {
protected class ProtectedClass { }
}
"""
.trimIndent(),
)

val icon = runInEdtAndGet {
provider.getVisibilityIcon(fixture.findClass("OuterClass.ProtectedClass"))
thelooter marked this conversation as resolved.
Show resolved Hide resolved
}

assertEquals(AllIcons.Nodes.Protected, icon)
}

@Test
@DisplayName("Test visibility icon for a package-private class")
fun getVisibilityIconForTesting_packagePrivateClass_returnsPackageLocalIcon() {
fixture.addFileToProject(
"PackagePrivateClass.java",
"""
class PackagePrivateClass { }
"""
.trimIndent(),
)

val icon = runInEdtAndGet {
provider.getVisibilityIcon(fixture.findClass("PackagePrivateClass"))
}

assertEquals(AllIcons.Nodes.PackageLocal, icon)
}
}