aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/translators/AccessorMethodNamingTest.kt
blob: dee4bf7aaa8a3c35cc1dcac79ec690b0a142f054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package translators

import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.model.DProperty
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

/**
 * https://kotlinlang.org/docs/java-to-kotlin-interop.html#properties
 * https://kotlinlang.org/docs/java-interop.html#getters-and-setters
 */
class AccessorMethodNamingTest : BaseAbstractTest() {

    @Test
    fun `standard property`() {
        testAccessors("data class TestCase(var standardString: String, var standardBoolean: Boolean)") {
            doTest("standardString", "getStandardString", "setStandardString")
            doTest("standardBoolean", "getStandardBoolean", "setStandardBoolean")
        }
    }

    @Test
    fun `properties that start with the word 'is' use the special is rules`() {
        testAccessors("data class TestCase(var isFoo: String, var isBar: Boolean)") {
            doTest("isFoo", "isFoo", "setFoo")
            doTest("isBar", "isBar", "setBar")
        }
    }

    @Test
    fun `properties that start with a word that starts with 'is' use get and set`() {
        testAccessors("data class TestCase(var issuesFetched: Int, var issuesWereDisplayed: Boolean)") {
            doTest("issuesFetched", "getIssuesFetched", "setIssuesFetched")
            doTest("issuesWereDisplayed", "getIssuesWereDisplayed", "setIssuesWereDisplayed")
        }
    }

    @Test
    fun `properties that start with the word 'is' followed by underscore use the special is rules`() {
        testAccessors("data class TestCase(var is_foo: String, var is_bar: Boolean)") {
            doTest("is_foo", "is_foo", "set_foo")
            doTest("is_bar", "is_bar", "set_bar")
        }
    }

    @Test
    fun `properties that start with the word 'is' followed by a number use the special is rules`() {
        testAccessors("data class TestCase(var is1of: String, var is2of: Boolean)") {
            doTest("is1of", "is1of", "set1of")
            doTest("is2of", "is2of", "set2of")
        }
    }

    @Test
    fun `sanity check short names`() {
        testAccessors(
            """
            data class TestCase(
               var i: Boolean,
               var `is`: Boolean,
               var isz: Boolean,
               var isA: Int,
               var isB: Boolean,
            )
            """.trimIndent()
        ) {
            doTest("i", "getI", "setI")
            doTest("is", "getIs", "setIs")
            doTest("isz", "getIsz", "setIsz")
            doTest("isA", "isA", "setA")
            doTest("isB", "isB", "setB")
        }
    }

    private fun testAccessors(code: String, block: PropertyTestCase.() -> Unit) {
        val configuration = dokkaConfiguration {
            suppressObviousFunctions = false
            sourceSets {
                sourceSet {
                    sourceRoots = listOf("src/main/kotlin")
                }
            }
        }

        testInline("""
            /src/main/kotlin/sample/TestCase.kt
            package sample
            
            $code
            """.trimIndent(),
            configuration) {
            documentablesMergingStage = { module ->
                val properties = module.packages.single().classlikes.first().properties
                PropertyTestCase(properties).apply {
                    block()
                    finish()
                }
            }
        }
    }

    private class PropertyTestCase(private val properties: List<DProperty>) {
        private var testsDone: Int = 0

        fun doTest(kotlinName: String, getter: String? = null, setter: String? = null) {
            properties.first { it.name == kotlinName }.let {
                assertEquals(getter, it.getter?.name)
                assertEquals(setter, it.setter?.name)
            }
            testsDone += 1
        }

        fun finish() {
            assertTrue(testsDone > 0, "No tests in TestCase")
            assertEquals(testsDone, properties.size)
        }
    }
}