aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/kotlin/model/SourceLinksTest.kt
blob: 8f7766f6978acbe30667f9abc8ffcb5831558591 (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
package org.jetbrains.dokka.tests.model

import org.jetbrains.dokka.NodeKind
import org.jetbrains.dokka.SourceLinkDefinitionImpl
import org.jetbrains.dokka.testApi.ModelConfig
import org.jetbrains.dokka.testApi.checkSourceExistsAndVerifyModel
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.File

@RunWith(Parameterized::class)
class SourceLinksTest(
    private val srcLink: String,
    private val url: String,
    private val lineSuffix: String?,
    private val expectedUrl: String
) {

    @Test
    fun test() {
        val link = if(srcLink.contains(sourceLinks)){
            srcLink.substringBeforeLast(sourceLinks) + sourceLinks
        } else {
            srcLink.substringBeforeLast(testdata) + testdata
        }
        val sourceLink = SourceLinkDefinitionImpl(link, url, lineSuffix)

        checkSourceExistsAndVerifyModel(filePath, ModelConfig(sourceLinks = listOf(sourceLink))) { model ->
            with(model.members.single().members.single()) {
                Assert.assertEquals("foo", name)
                Assert.assertEquals(NodeKind.Function, kind)
                Assert.assertEquals(expectedUrl, details(NodeKind.SourceUrl).single().name)
            }
        }
    }

    companion object {
        private const val testdata = "testdata"
        private const val sourceLinks = "sourceLinks"
        private const val dummy = "dummy.kt"
        private const val pathSuffix = "$sourceLinks/$dummy"
        private const val filePath = "$testdata/$pathSuffix"
        private const val url = "https://example.com"

        @Parameterized.Parameters(name = "{index}: {0}, {1}, {2} = {3}")
        @JvmStatic
        fun data(): Collection<Array<String?>> {
            val longestPath = File(testdata).absolutePath.removeSuffix("/") + "/../$testdata/"
            val maxLength = longestPath.length
            val list = listOf(
                arrayOf(File(testdata).absolutePath.removeSuffix("/"), "$url/$pathSuffix"),
                arrayOf(File("$testdata/$sourceLinks").absolutePath.removeSuffix("/") + "/", "$url/$dummy"),
                arrayOf(longestPath, "$url/$pathSuffix"),

                arrayOf(testdata, "$url/$pathSuffix"),
                arrayOf("./$testdata", "$url/$pathSuffix"),
                arrayOf("../core/$testdata", "$url/$pathSuffix"),
                arrayOf("$testdata/$sourceLinks", "$url/$dummy"),
                arrayOf("./$testdata/../$testdata/$sourceLinks", "$url/$dummy")
            )

            return list.map { arrayOf(it[0].padEnd(maxLength, '_'), url, null, it[1]) } +
                    listOf(
                        // check that it also works if url ends with /
                        arrayOf((File(testdata).absolutePath.removeSuffix("/") + "/").padEnd(maxLength, '_'), "$url/", null, "$url/$pathSuffix"),
                        // check if line suffix work
                        arrayOf<String?>("../core/../core/./$testdata/$sourceLinks/".padEnd(maxLength, '_'), "$url/", "#L", "$url/$dummy#L4")
                    )
        }
    }

}