summaryrefslogtreecommitdiff
path: root/plugins/github/test/org/jetbrains/plugins/github/pullrequest/comment/GHSuggestionHtmlSyntaxHighlighterTest.kt
blob: 84453779783fd2086d393dfd964a7087f54adba0 (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
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.pullrequest.comment

import org.junit.Assert.assertArrayEquals
import org.junit.Test

class GHSuggestionHtmlSyntaxHighlighterTest {

  @Test
  fun `cut multiple lines from diff hunk`() {
    val afterStartLine = 0
    val afterEndLine = 14

    val diffHunk = """
      @@ -0,0 +$afterStartLine,$afterEndLine @@
      +class MultipleDiffHunk {                   // line number: 0
      +                                           // line number: 1
      +                                           // line number: 2
      +                                           // line number: 3
      +                                           // line number: 4
      +                                           // line number: 5
      +                                           // line number: 6
      +                                           // line number: 7
      +    companion object {                     // line number: 8
      +        @JvmStatic
      +        fun main(args: Array<String>) {
      +            println("MultipleDiffHunk")
      +        }
      +    }                                      // line number: 13
    """.trimIndent()

    val expectedChangedContent = listOf(
      "class MultipleDiffHunk {                   // line number: 0",
      "                                           // line number: 1",
      "                                           // line number: 2",
      "                                           // line number: 3",
      "                                           // line number: 4",
      "                                           // line number: 5",
      "                                           // line number: 6",
      "                                           // line number: 7",
      "    companion object {                     // line number: 8",
      "        @JvmStatic",
      "        fun main(args: Array<String>) {",
      "            println(\"MultipleDiffHunk\")",
      "        }",
      "    }                                      // line number: 13"
    )

    checkSuggestedChange(expectedChangedContent, diffHunk, afterStartLine, afterEndLine - 1)
  }

  @Test
  fun `cut single line from diff hunk`() {
    val afterStartLine = 0
    val afterEndLine = 5

    val diffHunk = """
      @@ -0,0 +$afterStartLine,$afterEndLine @@
      +class SingleDiffHunk {                     // line number: 0
      +    companion object {                     // line number: 1
      +        @JvmStatic                         // line number: 2
      +        fun main(args: Array<String>) {    // line number: 3
      +            println("SingleDiffHunk")
    """.trimIndent()

    val expectedChangedContent = listOf(
      "            println(\"SingleDiffHunk\")"
    )

    checkSuggestedChange(expectedChangedContent, diffHunk, afterEndLine - 1, afterEndLine - 1)
  }

  @Test
  fun `cut single line from edited diff hunk`() {
    val afterStartLine = 0
    val afterEndLine = 8

    val diffHunk = """
      @@ -$afterStartLine,$afterEndLine +$afterStartLine,$afterEndLine @@
       class Apple {
      -    private var counter = 0
      +    private var counter = 42
       
           companion object {
               @JvmStatic
               fun main(args: Array<String>) {
      -            println(Companion::class.java.simpleName)
      +            println(Companion::class.java.name)
    """.trimIndent()

    val expectedChangedContent = listOf(
      "    companion object {",
      "        @JvmStatic",
      "        fun main(args: Array<String>) {",
      "            println(Companion::class.java.name)"
    )

    checkSuggestedChange(expectedChangedContent, diffHunk, 4, afterEndLine - 1)
  }

  @Test
  fun `cut multiple lines from edited diff hunk`() {
    val afterStartLine = 0
    val afterEndLine = 4

    val diffHunk = """
      @@ -0,3 +$afterStartLine,$afterEndLine @@
      -class A {
      -    private var counter = 0
      +class B {
      +    private var counter = 1
      +    private val name = "diffHunk"
    """.trimIndent()

    val expectedChangedContent = listOf(
      "class B {",
      "    private var counter = 1",
      "    private val name = \"diffHunk\""
    )

    checkSuggestedChange(expectedChangedContent, diffHunk, afterStartLine, afterEndLine - 1)
  }

  private fun checkSuggestedChange(expected: List<String>, diffHunk: String, afterStartLine: Int, afterEndLine: Int) {
    val suggestedChangeInfo = GHSuggestedChangeInfo.create(diffHunk, "", afterStartLine, afterEndLine)
    val changedContent = suggestedChangeInfo.cutChangedContent()

    assertArrayEquals(expected.toTypedArray(), changedContent.toTypedArray())
  }
}