-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't suppress obvious members in kotlin.Enum and kotlin.Any (#3349)
- Loading branch information
Showing
5 changed files
with
391 additions
and
14 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
.../analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/TagsAnnotations.kt
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,43 @@ | ||
/* | ||
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package org.jetbrains.dokka.analysis.test | ||
|
||
import org.junit.jupiter.api.Tag | ||
|
||
// COPY OF dokka-subprojects/plugin-base/src/test/kotlin/utils/TagsAnnotations.kt | ||
|
||
/** | ||
* Run a test only for descriptors, not symbols. | ||
* | ||
* In theory, these tests can be fixed | ||
*/ | ||
@Target( | ||
AnnotationTarget.CLASS, | ||
AnnotationTarget.FUNCTION, | ||
AnnotationTarget.PROPERTY_GETTER, | ||
AnnotationTarget.PROPERTY_SETTER | ||
) | ||
@Retention( | ||
AnnotationRetention.RUNTIME | ||
) | ||
@Tag("onlyDescriptors") | ||
annotation class OnlyDescriptors(val reason: String = "") | ||
|
||
/** | ||
* Run a test only for descriptors, not symbols. | ||
* | ||
* These tests cannot be fixed until Analysis API does not support MPP | ||
*/ | ||
@Target( | ||
AnnotationTarget.CLASS, | ||
AnnotationTarget.FUNCTION, | ||
AnnotationTarget.PROPERTY_GETTER, | ||
AnnotationTarget.PROPERTY_SETTER | ||
) | ||
@Retention( | ||
AnnotationRetention.RUNTIME | ||
) | ||
@Tag("onlyDescriptorsMPP") | ||
annotation class OnlyDescriptorsMPP(val reason: String = "") |
260 changes: 260 additions & 0 deletions
260
...pi/src/test/kotlin/org/jetbrains/dokka/analysis/test/documentable/ObviousFunctionsTest.kt
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,260 @@ | ||
/* | ||
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package org.jetbrains.dokka.analysis.test.documentable | ||
|
||
import org.jetbrains.dokka.analysis.test.OnlyDescriptors | ||
import org.jetbrains.dokka.analysis.test.api.kotlinJvmTestProject | ||
import org.jetbrains.dokka.analysis.test.api.parse | ||
import org.jetbrains.dokka.analysis.test.api.useServices | ||
import org.jetbrains.dokka.links.DRI | ||
import org.jetbrains.dokka.model.DFunction | ||
import org.jetbrains.dokka.model.ObviousMember | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
|
||
class ObviousFunctionsTest { | ||
|
||
@OnlyDescriptors("#3354") | ||
@Test | ||
fun `kotlin_Any should not have obvious members`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("kotlin/Any.kt") { | ||
+""" | ||
package kotlin | ||
public open class Any { | ||
public open fun equals(other: Any?): Boolean | ||
public open fun hashCode(): Int | ||
public open fun toString(): String | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val any = pkg.classlikes.single() | ||
|
||
assertEquals("Any", any.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = emptySet(), | ||
expectedNonObviousFunctions = setOf("equals", "hashCode", "toString"), | ||
actualFunctions = any.functions | ||
) | ||
} | ||
|
||
@Test | ||
fun `kotlin_Any should not have obvious members via external documentable provider`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+"class SomeClass" | ||
} | ||
} | ||
|
||
project.useServices { | ||
val any = externalDocumentableProvider.getClasslike( | ||
DRI("kotlin", "Any"), | ||
it.context.configuration.sourceSets.single() | ||
) | ||
assertNotNull(any) | ||
assertEquals("Any", any.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = emptySet(), | ||
expectedNonObviousFunctions = setOf("equals", "hashCode", "toString"), | ||
actualFunctions = any.functions | ||
) | ||
} | ||
} | ||
|
||
// when running with K2 - inherited from java enum functions available: "clone", "finalize", "getDeclaringClass" | ||
@OnlyDescriptors("#3196") | ||
@Test | ||
fun `kotlin_Enum should not have obvious members`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("kotlin/Any.kt") { | ||
+""" | ||
package kotlin | ||
public abstract class Enum<E : Enum<E>>(name: String, ordinal: Int): Comparable<E> { | ||
public override final fun compareTo(other: E): Int | ||
public override final fun equals(other: Any?): Boolean | ||
public override final fun hashCode(): Int | ||
public override fun toString(): String | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val any = pkg.classlikes.single() | ||
|
||
assertEquals("Enum", any.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = emptySet(), | ||
expectedNonObviousFunctions = setOf("compareTo", "equals", "hashCode", "toString"), | ||
actualFunctions = any.functions | ||
) | ||
} | ||
|
||
// when running with K2 there is no equals, hashCode, toString present | ||
@OnlyDescriptors("#3196") | ||
@Test | ||
fun `kotlin_Enum should not have obvious members via external documentable provider`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+"class SomeClass" | ||
} | ||
} | ||
|
||
project.useServices { | ||
val enum = externalDocumentableProvider.getClasslike( | ||
DRI("kotlin", "Enum"), | ||
it.context.configuration.sourceSets.single() | ||
) | ||
assertNotNull(enum) | ||
assertEquals("Enum", enum.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = emptySet(), | ||
expectedNonObviousFunctions = setOf( | ||
"compareTo", "equals", "hashCode", "toString", | ||
// inherited from java enum | ||
"clone", "finalize", "getDeclaringClass" | ||
), | ||
actualFunctions = enum.functions | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should mark only toString, equals and hashcode as obvious for class`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+""" | ||
class SomeClass { | ||
fun custom() {} | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val cls = pkg.classlikes.single() | ||
|
||
assertEquals("SomeClass", cls.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = setOf("equals", "hashCode", "toString"), | ||
expectedNonObviousFunctions = setOf("custom"), | ||
actualFunctions = cls.functions | ||
) | ||
} | ||
|
||
@Test | ||
fun `should mark only toString, equals and hashcode as obvious for interface`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+""" | ||
interface SomeClass { | ||
fun custom() | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val cls = pkg.classlikes.single() | ||
|
||
assertEquals("SomeClass", cls.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = setOf("equals", "hashCode", "toString"), | ||
expectedNonObviousFunctions = setOf("custom"), | ||
actualFunctions = cls.functions | ||
) | ||
} | ||
|
||
@Test | ||
fun `should mark data class generated functions as obvious`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+""" | ||
data class SomeClass(val x: String) { | ||
fun custom() {} | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val cls = pkg.classlikes.single() | ||
|
||
assertEquals("SomeClass", cls.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = setOf("equals", "hashCode", "toString", "component1", "copy"), | ||
expectedNonObviousFunctions = setOf("custom"), | ||
actualFunctions = cls.functions | ||
) | ||
} | ||
|
||
@Test | ||
fun `should not mark as obvious if override`() { | ||
val project = kotlinJvmTestProject { | ||
ktFile("SomeClass.kt") { | ||
+""" | ||
data class SomeClass(val x: String) { | ||
override fun toString(): String = x | ||
} | ||
""" | ||
} | ||
} | ||
|
||
val module = project.parse() | ||
|
||
val pkg = module.packages.single() | ||
val cls = pkg.classlikes.single() | ||
|
||
assertEquals("SomeClass", cls.name) | ||
|
||
assertObviousFunctions( | ||
expectedObviousFunctions = setOf("equals", "hashCode", "component1", "copy"), | ||
expectedNonObviousFunctions = setOf("toString"), | ||
actualFunctions = cls.functions | ||
) | ||
} | ||
|
||
private fun assertObviousFunctions( | ||
expectedObviousFunctions: Set<String>, | ||
expectedNonObviousFunctions: Set<String>, | ||
actualFunctions: List<DFunction> | ||
) { | ||
val (notObviousFunctions, obviousFunctions) = actualFunctions.partition { | ||
it.extra[ObviousMember] == null | ||
} | ||
|
||
assertEquals( | ||
expectedNonObviousFunctions, | ||
notObviousFunctions.map { it.name }.toSet() | ||
) | ||
|
||
assertEquals( | ||
expectedObviousFunctions, | ||
obviousFunctions.map { it.name }.toSet() | ||
) | ||
} | ||
|
||
} |
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
Oops, something went wrong.