aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/com/facebook/ktfmt/kdoc/UtilitiesTest.kt
blob: e5ad1a7aabf840f8ae635bf052f1fa760cdd1b75 (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
/*
 * Copyright (c) Tor Norbye.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.ktfmt.kdoc

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class UtilitiesTest {
  @Test
  fun testFindSamePosition() {
    fun check(newWithCaret: String, oldWithCaret: String) {
      val oldCaretIndex = oldWithCaret.indexOf('|')
      val newCaretIndex = newWithCaret.indexOf('|')
      assertThat(oldCaretIndex != -1).isTrue()
      assertThat(newCaretIndex != -1).isTrue()
      val old = oldWithCaret.substring(0, oldCaretIndex) + oldWithCaret.substring(oldCaretIndex + 1)
      val new = newWithCaret.substring(0, newCaretIndex) + newWithCaret.substring(newCaretIndex + 1)
      val newPos = findSamePosition(old, oldCaretIndex, new)

      val actual = new.substring(0, newPos) + "|" + new.substring(newPos)
      assertThat(actual).isEqualTo(newWithCaret)
    }

    // Prefix match
    check("|/** Test\n Different Middle End */", "|/** Test2 End */")
    check("/|** Test\n Different Middle End */", "/|** Test2 End */")
    check("/*|* Test\n Different Middle End */", "/*|* Test2 End */")
    check("/**| Test\n Different Middle End */", "/**| Test2 End */")
    check("/** |Test\n Different Middle End */", "/** |Test2 End */")
    check("/** T|est\n Different Middle End */", "/** T|est2 End */")
    check("/** Te|st\n Different Middle End */", "/** Te|st2 End */")
    check("/** Tes|t\n Different Middle End */", "/** Tes|t2 End */")
    check("/** Test|\n Different Middle End */", "/** Test|2 End */")
    // End match
    check("/** Test\n Different Middle| End */", "/** Test2| End */")
    check("/** Test\n Different Middle E|nd */", "/** Test2 E|nd */")
    check("/** Test\n Different Middle En|d */", "/** Test2 En|d */")
    check("/** Test\n Different Middle End| */", "/** Test2 End| */")
    check("/** Test\n Different Middle End |*/", "/** Test2 End |*/")
    check("/** Test\n Different Middle End *|/", "/** Test2 End *|/")
    check("/** Test\n Different Middle End */|", "/** Test2 End */|")

    check("|/**\nTest End\n*/", "|/** Test End */")
    check("/|**\nTest End\n*/", "/|** Test End */")
    check("/*|*\nTest End\n*/", "/*|* Test End */")
    check("/**|\nTest End\n*/", "/**| Test End */")
    check("/**\n|Test End\n*/", "/** |Test End */")
    check("/**\nT|est End\n*/", "/** T|est End */")
    check("/**\nTe|st End\n*/", "/** Te|st End */")
    check("/**\nTes|t End\n*/", "/** Tes|t End */")
    check("/**\nTest| End\n*/", "/** Test| End */")
    check("/**\nTest |End\n*/", "/** Test |End */")
    check("/**\nTest E|nd\n*/", "/** Test E|nd */")
    check("/**\nTest En|d\n*/", "/** Test En|d */")
    check("/**\nTest End|\n*/", "/** Test End| */")
    check("/**\nTest End\n|*/", "/** Test End |*/")
    check("/**\nTest End\n*|/", "/** Test End *|/")
    check("/**\nTest End\n*/|", "/** Test End */|")

    check("|/** Test End */", "|/** Test2 End */")
    check("/|** Test End */", "/|** Test2 End */")
    check("/*|* Test End */", "/*|* Test2 End */")
    check("/**| Test End */", "/**| Test2 End */")
    check("/** |Test End */", "/** |Test2 End */")
    check("/** T|est End */", "/** T|est2 End */")
    check("/** Te|st End */", "/** Te|st2 End */")
    check("/** Tes|t End */", "/** Tes|t2 End */")
    check("/** Test| End */", "/** Test|2 End */")
    check("/** Test |End */", "/** Test2 |End */")
    check("/** Test E|nd */", "/** Test2 E|nd */")
    check("/** Test En|d */", "/** Test2 En|d */")
    check("/** Test End| */", "/** Test2 End| */")
    check("/** Test End |*/", "/** Test2 End |*/")
    check("/** Test End *|/", "/** Test2 End *|/")
    check("/** Test End */|", "/** Test2 End */|")
  }

  @Test
  fun testGetParamName() {
    assertThat("@param foo".getParamName()).isEqualTo("foo")
    assertThat("@param foo bar".getParamName()).isEqualTo("foo")
    assertThat("@param foo;".getParamName()).isEqualTo("foo")
    assertThat("  \t@param\t   foo  bar.".getParamName()).isEqualTo("foo")
    assertThat("@param[foo]".getParamName()).isEqualTo("foo")
    assertThat("@param  [foo]".getParamName()).isEqualTo("foo")
    assertThat("@param ".getParamName()).isEqualTo(null)
  }
}