Test code folding

This commit is contained in:
Patrick Förster 2018-12-25 21:43:33 +01:00
parent b69dd2b71c
commit 4ddf87900d
3 changed files with 98 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package texlab
import org.eclipse.lsp4j.Position
import texlab.completion.CompletionRequest
import texlab.folding.FoldingRequest
import java.io.File
class WorkspaceBuilder {
@ -22,4 +23,9 @@ class WorkspaceBuilder {
val position = Position(line, character)
return CompletionRequest(uri, workspace.relatedDocuments(uri), position)
}
fun folding(name: String): FoldingRequest {
val uri = File(name).toURI()
return FoldingRequest(workspace.documents.first { it.uri == uri })
}
}

View file

@ -0,0 +1,37 @@
package texlab.folding
import org.eclipse.lsp4j.FoldingRange
import org.eclipse.lsp4j.FoldingRangeKind
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import texlab.WorkspaceBuilder
class LatexEnvironmentFoldingProviderTests {
@Test
fun `it should work with multiline environments`() {
val expected = arrayOf(FoldingRange().apply {
startLine = 0
startCharacter = 11
endLine = 1
endCharacter = 0
kind = FoldingRangeKind.Region
})
WorkspaceBuilder()
.document("foo.tex", "\\begin{foo}\n\\end{foo}")
.folding("foo.tex")
.let { LatexEnvironmentFoldingProvider.fold(it) }
.toTypedArray()
.also { assertArrayEquals(expected, it) }
}
@Test
fun `it should not process BibTeX documents`() {
WorkspaceBuilder()
.document("foo.bib", "")
.folding("foo.bib")
.let { LatexEnvironmentFoldingProvider.fold(it) }
.also { assertEquals(0, it.size) }
}
}

View file

@ -0,0 +1,55 @@
package texlab.folding
import org.eclipse.lsp4j.FoldingRange
import org.eclipse.lsp4j.FoldingRangeKind
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Test
import texlab.WorkspaceBuilder
class LatexSectionFoldingProviderTests {
@Test
fun `it should work with multiple levels of nesting`() {
val text = "\\section{Foo}\nfoo\n\\subsection{Bar}\nbar\n\\section{Baz}\nbaz\n\\section{Qux}"
val folding1 = FoldingRange().apply {
startLine = 0
startCharacter = 13
endLine = 3
endCharacter = 0
kind = FoldingRangeKind.Region
}
val folding2 = FoldingRange().apply {
startLine = 2
startCharacter = 16
endLine = 3
endCharacter = 0
kind = FoldingRangeKind.Region
}
val folding3 = FoldingRange().apply {
startLine = 4
startCharacter = 13
endLine = 5
endCharacter = 0
kind = FoldingRangeKind.Region
}
val expected = arrayOf(folding1, folding2, folding3)
WorkspaceBuilder()
.document("foo.tex", text)
.folding("foo.tex")
.let { LatexSectionFoldingProvider.fold(it) }
.sortedBy { it.startLine }
.toTypedArray()
.also { assertArrayEquals(expected, it) }
}
@Test
fun `it should not process BibTeX documents`() {
WorkspaceBuilder()
.document("foo.bib", "")
.folding("foo.bib")
.let { LatexSectionFoldingProvider.fold(it) }
.also { Assertions.assertEquals(0, it.size) }
}
}