From 4ddf87900da09048aaa4e0d194610cd157648f89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Tue, 25 Dec 2018 21:43:33 +0100 Subject: [PATCH] Test code folding --- src/test/kotlin/texlab/WorkspaceBuilder.kt | 6 ++ .../LatexEnvironmentFoldingProviderTests.kt | 37 +++++++++++++ .../LatexSectionFoldingProviderTests.kt | 55 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/test/kotlin/texlab/folding/LatexEnvironmentFoldingProviderTests.kt create mode 100644 src/test/kotlin/texlab/folding/LatexSectionFoldingProviderTests.kt diff --git a/src/test/kotlin/texlab/WorkspaceBuilder.kt b/src/test/kotlin/texlab/WorkspaceBuilder.kt index ddba6489..9f1a223f 100644 --- a/src/test/kotlin/texlab/WorkspaceBuilder.kt +++ b/src/test/kotlin/texlab/WorkspaceBuilder.kt @@ -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 }) + } } diff --git a/src/test/kotlin/texlab/folding/LatexEnvironmentFoldingProviderTests.kt b/src/test/kotlin/texlab/folding/LatexEnvironmentFoldingProviderTests.kt new file mode 100644 index 00000000..db15d631 --- /dev/null +++ b/src/test/kotlin/texlab/folding/LatexEnvironmentFoldingProviderTests.kt @@ -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) } + } +} diff --git a/src/test/kotlin/texlab/folding/LatexSectionFoldingProviderTests.kt b/src/test/kotlin/texlab/folding/LatexSectionFoldingProviderTests.kt new file mode 100644 index 00000000..bd220c3d --- /dev/null +++ b/src/test/kotlin/texlab/folding/LatexSectionFoldingProviderTests.kt @@ -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) } + } +}