generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Added unit tests for LSP-based code folding and the code block …
- Loading branch information
Showing
7 changed files
with
500 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ava/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptCodeBlockProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
|
||
package com.redhat.devtools.lsp4ij.features.foldingRange; | ||
|
||
import com.redhat.devtools.lsp4ij.fixtures.LSPCodeBlockProviderFixtureTestCase; | ||
|
||
/** | ||
* Selection range tests by emulating LSP 'textDocument/foldingRange' responses from the typescript-language-server. | ||
*/ | ||
public class TypeScriptCodeBlockProviderTest extends LSPCodeBlockProviderFixtureTestCase { | ||
|
||
public TypeScriptCodeBlockProviderTest() { | ||
super("*.ts"); | ||
} | ||
|
||
public void testCodeBlocks() { | ||
// language=json | ||
String mockFoldingRangesJson = """ | ||
[ | ||
{ | ||
"startLine": 0, | ||
"endLine": 3 | ||
}, | ||
{ | ||
"startLine": 1, | ||
"endLine": 2 | ||
} | ||
] | ||
"""; | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo { | ||
demo() {<start> | ||
<caret>console.log('demo'); | ||
<end>} | ||
} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo {<start> | ||
<caret>demo() { | ||
console.log('demo'); | ||
} | ||
<end>} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo {<caret><start> | ||
demo() { | ||
console.log('demo'); | ||
} | ||
<end>} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo <caret>{<start> | ||
demo() { | ||
console.log('demo'); | ||
} | ||
<end>} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo {<start> | ||
demo() { | ||
console.log('demo'); | ||
} | ||
<end><caret>} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class Demo {<start> | ||
demo() { | ||
console.log('demo'); | ||
} | ||
<end>}<caret> | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
|
||
assertCodeBlock( | ||
"demo.ts", | ||
""" | ||
export class <caret><start><end>Demo { | ||
demo() { | ||
console.log('demo'); | ||
} | ||
} | ||
""", | ||
mockFoldingRangesJson | ||
); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...est/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptFoldingRangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
|
||
package com.redhat.devtools.lsp4ij.features.foldingRange; | ||
|
||
import com.redhat.devtools.lsp4ij.fixtures.LSPFoldingRangeFixtureTestCase; | ||
|
||
/** | ||
* Selection range tests by emulating LSP 'textDocument/foldingRange' responses from the typescript-language-server. | ||
*/ | ||
public class TypeScriptFoldingRangeTest extends LSPFoldingRangeFixtureTestCase { | ||
|
||
public TypeScriptFoldingRangeTest() { | ||
super("*.ts"); | ||
} | ||
|
||
public void testFoldingRanges() { | ||
assertFoldingRanges( | ||
"demo.ts", | ||
""" | ||
export class Demo {<start1> | ||
demo() {<start2> | ||
console.log('demo'); | ||
<end2>} | ||
<end1>} | ||
""", | ||
// language=json | ||
""" | ||
[ | ||
{ | ||
"startLine": 0, | ||
"endLine": 3 | ||
}, | ||
{ | ||
"startLine": 1, | ||
"endLine": 2 | ||
} | ||
] | ||
""" | ||
); | ||
} | ||
|
||
public void testFoldingRanges_collapsedText() { | ||
assertFoldingRanges( | ||
"demo.ts", | ||
""" | ||
export class Demo {<start1> | ||
demo() {<start2> | ||
console.log('demo'); | ||
<end2>} | ||
<end1>} | ||
""", | ||
// language=json | ||
""" | ||
[ | ||
{ | ||
"startLine": 0, | ||
"endLine": 3, | ||
"collapsedText": "classBody" | ||
}, | ||
{ | ||
"startLine": 1, | ||
"endLine": 2, | ||
"collapsedText": "methodBody" | ||
} | ||
] | ||
""" | ||
); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.fixtures; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
import com.intellij.codeInsight.editorActions.CodeBlockUtil; | ||
import com.intellij.openapi.editor.CaretModel; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.Trinity; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.testFramework.EditorTestUtil; | ||
import com.redhat.devtools.lsp4ij.JSONUtils; | ||
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor; | ||
import com.redhat.devtools.lsp4ij.mock.MockLanguageServer; | ||
import org.eclipse.lsp4j.FoldingRange; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Base class test case to test the 'codeBlockProvider' based on LSP 'textDocument/codeBlock' feature. | ||
*/ | ||
public abstract class LSPCodeBlockProviderFixtureTestCase extends LSPCodeInsightFixtureTestCase { | ||
|
||
private static final String CARET_TOKEN = "<caret>"; | ||
private static final String START_TOKEN = "<start>"; | ||
private static final String END_TOKEN = "<end>"; | ||
|
||
protected LSPCodeBlockProviderFixtureTestCase(String... fileNamePatterns) { | ||
super(fileNamePatterns); | ||
} | ||
|
||
protected void assertCodeBlock(@NotNull String fileName, | ||
@NotNull String fileBody, | ||
@NotNull String mockFoldingRangesJson) { | ||
MockLanguageServer.INSTANCE.setTimeToProceedQueries(100); | ||
List<FoldingRange> mockFoldingRanges = JSONUtils.getLsp4jGson().fromJson(mockFoldingRangesJson, new TypeToken<List<FoldingRange>>() { | ||
}.getType()); | ||
MockLanguageServer.INSTANCE.setFoldingRanges(mockFoldingRanges); | ||
|
||
Project project = myFixture.getProject(); | ||
PsiFile file = myFixture.configureByText(fileName, stripTokens(fileBody)); | ||
Editor editor = myFixture.getEditor(); | ||
|
||
// Initialize the language server | ||
try { | ||
LanguageServiceAccessor.getInstance(project) | ||
.getLanguageServers(file.getVirtualFile(), null, null) | ||
.get(5000, TimeUnit.MILLISECONDS); | ||
} catch (Exception e) { | ||
fail(e.getMessage()); | ||
} | ||
|
||
EditorTestUtil.buildInitialFoldingsInBackground(editor); | ||
|
||
CaretModel caretModel = editor.getCaretModel(); | ||
|
||
// Derive the caret, start, and end offsets from tokens in the file body | ||
Trinity<Integer, Integer, Integer> offsets = getOffsets(fileBody); | ||
int caretOffset = offsets.getFirst(); | ||
int startOffset = offsets.getSecond(); | ||
int endOffset = offsets.getThird(); | ||
|
||
caretModel.moveToOffset(caretOffset); | ||
CodeBlockUtil.moveCaretToCodeBlockStart(project, editor, false); | ||
assertEquals(startOffset, caretModel.getOffset()); | ||
|
||
caretModel.moveToOffset(caretOffset); | ||
CodeBlockUtil.moveCaretToCodeBlockEnd(project, editor, false); | ||
assertEquals(endOffset, caretModel.getOffset()); | ||
} | ||
|
||
@NotNull | ||
private static String stripTokens(@NotNull String fileBody) { | ||
return fileBody | ||
.replace(CARET_TOKEN, "") | ||
.replace(START_TOKEN, "") | ||
.replace(END_TOKEN, ""); | ||
} | ||
|
||
@NotNull | ||
private static Trinity<@NotNull Integer, @NotNull Integer, @NotNull Integer> getOffsets(@NotNull String fileBody) { | ||
// Gather the raw token offsets | ||
int rawCaretOffset = fileBody.indexOf(CARET_TOKEN); | ||
assertFalse("No " + CARET_TOKEN + " found.", rawCaretOffset == -1); | ||
int rawStartOffset = fileBody.indexOf(START_TOKEN); | ||
assertFalse("No " + START_TOKEN + " found.", rawStartOffset == -1); | ||
int rawEndOffset = fileBody.indexOf(END_TOKEN); | ||
assertFalse("No " + END_TOKEN + " found.", rawEndOffset == -1); | ||
|
||
// Adjust final offsets as appropriate based on relative token positioning | ||
int caretOffset = rawCaretOffset; | ||
if (rawCaretOffset > rawStartOffset) caretOffset -= START_TOKEN.length(); | ||
if (rawCaretOffset > rawEndOffset) caretOffset -= END_TOKEN.length(); | ||
int startOffset = rawStartOffset; | ||
if (rawStartOffset > rawCaretOffset) startOffset -= CARET_TOKEN.length(); | ||
if (rawStartOffset > rawEndOffset) startOffset -= END_TOKEN.length(); | ||
int endOffset = rawEndOffset; | ||
if (rawEndOffset > rawCaretOffset) endOffset -= CARET_TOKEN.length(); | ||
if (rawEndOffset > rawStartOffset) endOffset -= START_TOKEN.length(); | ||
|
||
return Trinity.create(caretOffset, startOffset, endOffset); | ||
} | ||
} |
Oops, something went wrong.