package org.javacs; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import org.javacs.lsp.*; import org.junit.Test; public class CodeLensTest { private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer(); private List lenses(String file) { var uri = FindResource.uri(file); var params = new CodeLensParams(new TextDocumentIdentifier(uri)); var lenses = server.codeLens(params); var resolved = new ArrayList(); for (var lens : lenses) { if (lens.command == null) { lens = server.resolveCodeLens(lens); } resolved.add(lens); } return resolved; } private List commands(List lenses) { var commands = new ArrayList(); for (var lens : lenses) { commands.add(String.format("%s(%s)", lens.command.command, lens.command.arguments)); } return commands; } private List titles(List lenses) { var titles = new ArrayList(); for (var lens : lenses) { var line = lens.range.start.line + 1; var title = lens.command.title; titles.add(line + ":" + title); } return titles; } @Test public void testMethods() { var lenses = lenses("/org/javacs/example/HasTest.java"); assertThat(lenses, not(empty())); var commands = commands(lenses); assertThat(commands, hasItem(containsString("\"HasTest\",null"))); assertThat(commands, hasItem(containsString("\"HasTest\",\"testMethod\""))); assertThat(commands, hasItem(containsString("\"HasTest\",\"otherTestMethod\""))); } @Test public void constructorReferences() { var lenses = lenses("/org/javacs/example/ConstructorRefs.java"); assertThat(lenses, not(empty())); var titles = titles(lenses); assertThat(titles, hasItem("4:1 reference")); assertThat(titles, hasItem("6:1 reference")); } @Test public void enumConstants() { var lenses = lenses("/org/javacs/example/DontShowEnumConstRefs.java"); var titles = titles(lenses); assertThat(titles, not(hasItem("4:0 references"))); } @Test public void signatureMatches() { var file = "/org/javacs/example/ConstructorRefs.java"; var uri = FindResource.uri(file); var contents = FindResource.contents(file); var compile = server.compiler.compileFile(uri, contents); var signatureMatches = compile.signatureMatches(); var good = List.of(new Ptr("org.javacs.example/ConstructorRefs#(int)")); assertTrue(signatureMatches.test(good)); var bad = List.of(new Ptr("org.javacs.example/ConstructorRefs#(int,int)")); assertFalse(signatureMatches.test(bad)); } }