| 1 | package com.github.continuedev.continueintellijextension |
| 2 | |
| 3 | import com.github.continuedev.continueintellijextension.editor.VerticalDiffBlock |
| 4 | import com.intellij.openapi.command.WriteCommandAction |
| 5 | import com.intellij.openapi.editor.Editor |
| 6 | import com.intellij.openapi.editor.EditorFactory |
| 7 | import com.intellij.openapi.editor.colors.EditorColorsManager |
| 8 | import com.intellij.openapi.editor.colors.TextAttributesKey |
| 9 | import com.intellij.testFramework.LightVirtualFile |
| 10 | import com.intellij.openapi.fileTypes.PlainTextFileType |
| 11 | import com.intellij.testFramework.fixtures.BasePlatformTestCase |
| 12 | |
| 13 | class VerticalDiffBlockTest : BasePlatformTestCase() { |
| 14 | |
| 15 | private lateinit var editor: Editor |
| 16 | |
| 17 | override fun setUp() { |
| 18 | super.setUp() |
| 19 | // Create a document and an editor with a virtual file |
| 20 | val document = EditorFactory.getInstance().createDocument("") |
| 21 | val virtualFile = LightVirtualFile("TestFile.kt", PlainTextFileType.INSTANCE, "") |
| 22 | editor = EditorFactory.getInstance().createEditor(document, project, virtualFile, false) |
| 23 | } |
| 24 | |
| 25 | override fun tearDown() { |
| 26 | try { |
| 27 | // Release the editor to avoid memory leaks |
| 28 | EditorFactory.getInstance().releaseEditor(editor) |
| 29 | } finally { |
| 30 | super.tearDown() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fun testAddNewLine() { |
| 35 | // Arrange |
| 36 | val textToAdd = "This is a new line" |
| 37 | val startLine = 0 |
| 38 | |
| 39 | // Create an instance of VerticalDiffBlock |
| 40 | val verticalDiffBlock = VerticalDiffBlock( |
| 41 | editor = editor, |
| 42 | project = project, |
| 43 | startLine = startLine, |
| 44 | onAcceptReject = { _, _ -> } |
| 45 | ) |
| 46 | |
| 47 | // Act |
| 48 | WriteCommandAction.runWriteCommandAction(project) { |
| 49 | verticalDiffBlock.addNewLine(textToAdd, startLine) |
| 50 | } |
| 51 | |
| 52 | // Assert |
| 53 | val documentText = editor.document.text |
| 54 | |
| 55 | // We expect a newline to be inserted in addition to our line |
| 56 | val expectedText = "$textToAdd\n\n" |
| 57 | assertEquals(expectedText, documentText) |
| 58 | |
| 59 | // Check if the new line is highlighted |
| 60 | val highlighters = editor.markupModel.allHighlighters.filter { highlighter -> |
| 61 | val line = editor.document.getLineNumber(highlighter.startOffset) |
| 62 | line == startLine |
| 63 | } |
| 64 | |
| 65 | assertTrue("Expected at least one highlighter on the new line", highlighters.isNotEmpty()) |
| 66 | |
| 67 | // Check the attributes of the highlighter |
| 68 | val expectedTextAttributesKey = TextAttributesKey.find("CONTINUE_DIFF_NEW_LINE") |
| 69 | val expectedAttributes = EditorColorsManager.getInstance().globalScheme.getAttributes(expectedTextAttributesKey) |
| 70 | val highlighterAttributes = highlighters.first().textAttributes |
| 71 | |
| 72 | assertEquals(expectedAttributes.foregroundColor, highlighterAttributes?.foregroundColor) |
| 73 | } |
| 74 | } |