|
4 | 4 | package software.aws.toolkits.jetbrains.utils |
5 | 5 |
|
6 | 6 | import com.intellij.json.JsonLanguage |
| 7 | +import com.intellij.openapi.editor.Document |
| 8 | +import com.intellij.openapi.project.Project |
| 9 | +import com.intellij.psi.PsiDocumentManager |
7 | 10 | import com.intellij.testFramework.ProjectRule |
8 | 11 | import com.intellij.testFramework.runInEdtAndWait |
9 | 12 | import org.assertj.core.api.Assertions.assertThat |
10 | 13 | import org.intellij.lang.annotations.Language |
11 | 14 | import org.junit.Rule |
12 | 15 | import org.junit.Test |
| 16 | +import org.mockito.Mockito |
| 17 | +import org.mockito.Mockito.mock |
| 18 | +import org.mockito.Mockito.times |
| 19 | +import org.mockito.Mockito.`when` |
| 20 | +import org.mockito.kotlin.doNothing |
| 21 | +import org.mockito.kotlin.verify |
13 | 22 | import software.aws.toolkits.core.utils.convertMarkdownToHTML |
| 23 | +import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.CodeWhispererCodeScanIssue |
| 24 | +import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.SuggestedFix |
14 | 25 |
|
15 | 26 | class TextUtilsTest { |
16 | 27 | @Rule |
@@ -94,4 +105,84 @@ class TextUtilsTest { |
94 | 105 | val actual = convertMarkdownToHTML(input) |
95 | 106 | assertThat(actual).isEqualTo(expected) |
96 | 107 | } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun extractChangesWithSingleLineDeletion() { |
| 111 | + val suggestedFixes = listOf(SuggestedFix(description = "MockDescription", code = "-oldLine\n+newLine")) |
| 112 | + val issue = mock(CodeWhispererCodeScanIssue::class.java) |
| 113 | + `when`(issue.suggestedFixes).thenReturn(suggestedFixes) |
| 114 | + |
| 115 | + val (linesToDelete, linesToInsert) = extractChanges(issue) |
| 116 | + |
| 117 | + assertThat(linesToDelete).isEqualTo(1) |
| 118 | + assertThat(linesToInsert).isEqualTo(listOf("newLine")) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun extractChangesWithMultipleLineDeletionAndInsertion() { |
| 123 | + val suggestedFixes = listOf(SuggestedFix(description = "MockDescription", code = "-oldLine1\n-oldLine2\n+newLine1\n+newLine2")) |
| 124 | + val issue = mock(CodeWhispererCodeScanIssue::class.java) |
| 125 | + `when`(issue.suggestedFixes).thenReturn(suggestedFixes) |
| 126 | + |
| 127 | + val (linesToDelete, linesToInsert) = extractChanges(issue) |
| 128 | + |
| 129 | + assertThat(linesToDelete).isEqualTo(2) |
| 130 | + assertThat(linesToInsert).isEqualTo(listOf("newLine1", "newLine2")) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun extractChangesWithEmptySuggestedFixes() { |
| 135 | + val emptyStringList: List<String> = emptyList() |
| 136 | + val issue = mock(CodeWhispererCodeScanIssue::class.java) |
| 137 | + `when`(issue.suggestedFixes).thenReturn(emptyList()) |
| 138 | + |
| 139 | + val (linesToDelete, linesToInsert) = extractChanges(issue) |
| 140 | + |
| 141 | + assertThat(linesToDelete).isEqualTo(0) |
| 142 | + assertThat(linesToInsert).isEqualTo(emptyStringList) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun updateEditorDocumentWithSingleLineDeletionAndInsertion() { |
| 147 | + val document = mock(Document::class.java) |
| 148 | + val project = mock(Project::class.java) |
| 149 | + val issue = mock(CodeWhispererCodeScanIssue::class.java) |
| 150 | + val suggestedFixes = listOf(SuggestedFix(description = "MockDescription", code = "-oldLine\n+newLine")) |
| 151 | + `when`(issue.startLine).thenReturn(1) |
| 152 | + `when`(issue.suggestedFixes).thenReturn(suggestedFixes) |
| 153 | + `when`(document.getLineStartOffset(0)).thenReturn(0) |
| 154 | + `when`(document.getLineEndOffset(0)).thenReturn(10) |
| 155 | + |
| 156 | + val psiDocumentManager = mock(PsiDocumentManager::class.java) |
| 157 | + doNothing().`when`(psiDocumentManager).commitDocument(document) |
| 158 | + Mockito.`when`(PsiDocumentManager.getInstance(project)).thenReturn(psiDocumentManager) |
| 159 | + |
| 160 | + runInEdtAndWait { |
| 161 | + updateEditorDocument(document, issue, project) |
| 162 | + } |
| 163 | + |
| 164 | + verify(document).replaceString(0, 10, "newLine") |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + fun updateEditorDocumentWithMultipleLineDeletionAndInsertion() { |
| 169 | + val document = mock(Document::class.java) |
| 170 | + val project = mock(Project::class.java) |
| 171 | + val issue = mock(CodeWhispererCodeScanIssue::class.java) |
| 172 | + val suggestedFixes = listOf(SuggestedFix(description = "MockDescription", code = "-oldLine1\n-oldLine2\n+newLine1\n+newLine2")) |
| 173 | + `when`(issue.startLine).thenReturn(1) |
| 174 | + `when`(issue.suggestedFixes).thenReturn(suggestedFixes) |
| 175 | + `when`(document.getLineStartOffset(0)).thenReturn(20) |
| 176 | + `when`(document.getLineEndOffset(1)).thenReturn(40) |
| 177 | + |
| 178 | + val psiDocumentManager = mock(PsiDocumentManager::class.java) |
| 179 | + doNothing().`when`(psiDocumentManager).commitDocument(document) |
| 180 | + Mockito.`when`(PsiDocumentManager.getInstance(project)).thenReturn(psiDocumentManager) |
| 181 | + |
| 182 | + runInEdtAndWait { |
| 183 | + updateEditorDocument(document, issue, project) |
| 184 | + } |
| 185 | + |
| 186 | + verify(document).replaceString(20, 40, "newLine1\nnewLine2") |
| 187 | + } |
97 | 188 | } |
0 commit comments