Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenporras committed Dec 13, 2022
1 parent 60ef8b8 commit 9c80db6
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import org.eclipse.lsp4e.test.references.FindReferencesTest;
import org.eclipse.lsp4e.test.rename.LSPTextChangeTest;
import org.eclipse.lsp4e.test.rename.RenameTest;
import org.eclipse.lsp4e.test.semanticTokens.SemanticTokensDataStreamProcessorTest;
import org.eclipse.lsp4e.test.semanticTokens.SemanticTokensLegendProviderTest;
import org.eclipse.lsp4e.test.semanticTokens.StyleRangeHolderTest;
import org.eclipse.lsp4e.test.symbols.SymbolsModelTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand Down Expand Up @@ -88,7 +91,10 @@
LSPCodeMiningTest.class,
ShowMessageTest.class,
WorkspaceFoldersTest.class,
DebugTest.class
DebugTest.class,
SemanticTokensLegendProviderTest.class,
SemanticTokensDataStreamProcessorTest.class,
StyleRangeHolderTest.class
})
public class AllTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*******************************************************************************
* Copyright (c) 2022 Avaloq Group AG.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.lsp4e.test.semanticTokens;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor;
import org.eclipse.lsp4e.test.AllCleanRule;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.SemanticTokensLegend;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.junit.Rule;
import org.junit.Test;

public class SemanticTokensDataStreamProcessorTest {
@Rule
public AllCleanRule clear = new AllCleanRule();

private static final Color RED = new Color(255, 0, 0);

private @NonNull Function<Position, Integer> offsetMapper(IDocument document) {
return (p) -> {
try {
return LSPEclipseUtils.toOffset(p, document);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
};
}

private static final IToken RED_TOKEN = new IToken() {

@Override
public boolean isWhitespace() {
return false;
}

@Override
public boolean isUndefined() {
return false;
}

@Override
public boolean isOther() {
return false;
}

@Override
public boolean isEOF() {
return false;
}

@Override
public Object getData() {
return new TextAttribute(RED);
}
};

private static @NonNull Function<String, IToken> KEYWORD_TOKEN_TYPE_MAPPER = t -> {
if ("keyword".equals(t)) {
return RED_TOKEN;
} else {
return null;
}
};

@Test
public void testKeyword() throws InterruptedException, ExecutionException {
String text =
"type foo {\n" +
" \n" +
"}\n" +
"type bar extends foo {\n" +
" \n" +
"}\n";
Document document = new Document(text);

List<List<Integer>> expectedTokens = new ArrayList<>();
expectedTokens.add(Arrays.asList(0,0,4,1,0));
expectedTokens.add(Arrays.asList(3,0,4,1,0));
expectedTokens.add(Arrays.asList(0,9,7,1,0));

List<Integer> expectedStream =expectedTokens.stream().flatMap(List::stream).toList();

SemanticTokensDataStreamProcessor processor = new SemanticTokensDataStreamProcessor(KEYWORD_TOKEN_TYPE_MAPPER, offsetMapper(document));

List<StyleRange> styleRanges = processor.getStyleRanges(expectedStream, getSemanticTokensLegend());

List<StyleRange> expectedStyleRanges = Arrays.asList(new StyleRange(0, 4, RED, null), new StyleRange(15, 4, RED, null), new StyleRange(24, 7, RED, null));
assertEquals(expectedStyleRanges, styleRanges);
}

private SemanticTokensLegend getSemanticTokensLegend() {
SemanticTokensLegend semanticTokensLegend = new SemanticTokensLegend();
semanticTokensLegend.setTokenTypes(Arrays.asList("keyword","other"));
semanticTokensLegend.setTokenModifiers(Arrays.asList("obsolete"));
return semanticTokensLegend;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2022 Avaloq Group AG.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.lsp4e.test.semanticTokens;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensLegendProvider;
import org.eclipse.lsp4e.test.AllCleanRule;
import org.eclipse.lsp4e.test.TestUtils;
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
import org.eclipse.lsp4j.SemanticTokensLegend;
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class SemanticTokensLegendProviderTest {

@Rule
public AllCleanRule clear = new AllCleanRule();

private IProject project;

@Before
public void setUp() throws CoreException {
project = TestUtils.createProject(getClass().getName() + System.currentTimeMillis());
}

@Test
public void testSemanticTokensLegendProvider() throws BadLocationException, CoreException {
// Setup Server Capabilities
List<String> tokenTypes = Arrays.asList("keyword","other");
List<String> tokenModifiers = Arrays.asList("obsolete");
SemanticTokensLegend legend = new SemanticTokensLegend(tokenTypes, tokenModifiers);
SemanticTokensWithRegistrationOptions semanticTokensWithRegistrationOptions = new SemanticTokensWithRegistrationOptions(legend);
semanticTokensWithRegistrationOptions.setFull(true);
semanticTokensWithRegistrationOptions.setRange(false);

MockLanguageServer.INSTANCE.getInitializeResult().getCapabilities().setSemanticTokensProvider(semanticTokensWithRegistrationOptions);

// Setup test data
IFile file = TestUtils.createUniqueTestFile(project, "lspt", "test content");
IDocument document = TestUtils.openTextViewer(file).getDocument();
// start the LS
LanguageServiceAccessor.getLanguageServers(document, c -> true);

SemanticTokensLegendProvider semanticTokensLegendProvider = new SemanticTokensLegendProvider();
SemanticTokensLegend semanticTokensLegend = semanticTokensLegendProvider.getSemanticTokensLegend(MockLanguageServer.INSTANCE);
assertNotNull(semanticTokensLegend);
assertEquals(tokenTypes, semanticTokensLegend.getTokenTypes());
assertEquals(tokenModifiers, semanticTokensLegend.getTokenModifiers());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*******************************************************************************
* Copyright (c) 2022 Avaloq Group AG.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.lsp4e.test.semanticTokens;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.lsp4e.operations.semanticTokens.StyleRangeHolder;
import org.eclipse.lsp4e.test.AllCleanRule;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.junit.Rule;
import org.junit.Test;

public class StyleRangeHolderTest {
@Rule
public AllCleanRule clear = new AllCleanRule();

private static final Color RED = new Color(255, 0, 0);
private List<StyleRange> originalStyleRanges = Arrays.asList(new StyleRange(0, 4, RED, null), new StyleRange(15, 4, RED, null), new StyleRange(24, 7, RED, null));

@Test
public void testAllDocumentRanges() {
StyleRangeHolder holder = new StyleRangeHolder();
holder.saveStyles(originalStyleRanges);

StyleRange[] allDocumentRanges = holder.overlappingRanges(new Region(0, 50));

assertNotEquals(originalStyleRanges, allDocumentRanges); // styles must be copied
assertEquals(originalStyleRanges.size(), allDocumentRanges.length);
}

@Test
public void testPartialDocumentRanges() {
StyleRangeHolder holder = new StyleRangeHolder();
holder.saveStyles(originalStyleRanges);

StyleRange[] allDocumentRanges = holder.overlappingRanges(new Region(0, 20)); // only two ranges overlap this region

assertEquals(2, allDocumentRanges.length);
}

@Test
public void testDocumentChange() {
StyleRangeHolder holder = new StyleRangeHolder();
holder.saveStyles(originalStyleRanges);

TextEvent textEvent = new TextEvent(0, 1, " ", null, new DocumentEvent(), false) {};

// this will remove the first style and shift the last two
holder.textChanged(textEvent);

StyleRange[] noOverlappingRanges = holder.overlappingRanges(new Region(0, 10)); // only one range overlap this region

assertEquals(0, noOverlappingRanges.length);

StyleRange[] twoShiftedOverlappingRanges = holder.overlappingRanges(new Region(10, 50)); // only one range overlap this region

assertEquals(2, twoShiftedOverlappingRanges.length);
assertEquals(16, twoShiftedOverlappingRanges[0].start);
assertEquals(4, twoShiftedOverlappingRanges[0].length);
assertEquals(25, twoShiftedOverlappingRanges[1].start);
assertEquals(7, twoShiftedOverlappingRanges[1].length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
import org.eclipse.lsp4e.LanguageServiceAccessor.LSPDocumentInfo;
import org.eclipse.lsp4e.server.StreamConnectionProvider;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentIdentifier;
Expand Down Expand Up @@ -397,7 +398,7 @@ private static Collection<LanguageServerWrapper> getLSWrappers(@NonNull final ID
}

if (contentType.getBaseType() != null) {
contentTypesToProcess.add(contentType.getBaseType());
contentTypesToProcess.add(contentType.getBaseType());
}
processedContentTypes.add(contentType);
}
Expand Down Expand Up @@ -648,9 +649,12 @@ public static boolean checkCapability(LanguageServer languageServer, Predicate<S
.anyMatch(wrapper -> condition.test(wrapper.getServerCapabilities()));
}

public static Optional<LanguageServerDefinition> resolveServerDefinition(LanguageServer languageServer) {
public static Optional<LanguageServerWrapper> resolveLanguageServerWrapper(LanguageServer languageServer) {
return startedServers.stream() //
.filter(wrapper -> languageServer.equals(wrapper.getServer())).findFirst()
.map(wrapper -> wrapper.serverDefinition);
.filter(wrapper -> languageServer.equals(wrapper.getServer())).findFirst();
}

public static Optional<LanguageServerDefinition> resolveServerDefinition(LanguageServer languageServer) {
return resolveLanguageServerWrapper(languageServer).map(w -> w.serverDefinition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public void setProgressMonitor(final IProgressMonitor monitor) {
@Override
public void setDocument(final IDocument document) {
this.document = document;
semanticTokensLegendProvider.setDocument(document);
}

private boolean hasSemanticTokensFull(final ServerCapabilities serverCapabilities) {
Expand Down
Loading

0 comments on commit 9c80db6

Please sign in to comment.