summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-05 13:16:45 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-05 13:16:45 -0800
commit7c7202e0ad442dddac1a56890b34c3eb2f045f28 (patch)
tree6f0d5f51aaa3d018eb35bdda641f3aa92dc6eb34 /src/test
parent08b36f1358642b370b4a24be73bab7c29bdac39d (diff)
downloadjava-language-server-7c7202e0ad442dddac1a56890b34c3eb2f045f28.zip
Detect unused variables
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/javacs/LanguageServerFixture.java8
-rw-r--r--src/test/java/org/javacs/WarningsTest.java36
-rw-r--r--src/test/test-project/workspace/src/org/javacs/warn/Unused.java7
3 files changed, 51 insertions, 0 deletions
diff --git a/src/test/java/org/javacs/LanguageServerFixture.java b/src/test/java/org/javacs/LanguageServerFixture.java
index f2da3b7..f9052a0 100644
--- a/src/test/java/org/javacs/LanguageServerFixture.java
+++ b/src/test/java/org/javacs/LanguageServerFixture.java
@@ -19,18 +19,26 @@ class LanguageServerFixture {
return getJavaLanguageServer(DEFAULT_WORKSPACE_ROOT, diagnostic -> LOG.info(diagnostic.message));
}
+ static JavaLanguageServer getJavaLanguageServer(Consumer<Diagnostic> onError) {
+ return getJavaLanguageServer(DEFAULT_WORKSPACE_ROOT, onError);
+ }
+
static JavaLanguageServer getJavaLanguageServer(Path workspaceRoot, Consumer<Diagnostic> onError) {
return getJavaLanguageServer(
workspaceRoot,
new LanguageClient() {
+ @Override
public void publishDiagnostics(PublishDiagnosticsParams params) {
params.diagnostics.forEach(onError);
}
+ @Override
public void showMessage(ShowMessageParams params) {}
+ @Override
public void registerCapability(String method, JsonElement options) {}
+ @Override
public void customNotification(String method, JsonElement params) {}
});
}
diff --git a/src/test/java/org/javacs/WarningsTest.java b/src/test/java/org/javacs/WarningsTest.java
new file mode 100644
index 0000000..692e985
--- /dev/null
+++ b/src/test/java/org/javacs/WarningsTest.java
@@ -0,0 +1,36 @@
+package org.javacs;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+import org.javacs.lsp.Diagnostic;
+import org.junit.Before;
+import org.junit.Test;
+
+public class WarningsTest {
+ protected static final Logger LOG = Logger.getLogger("main");
+
+ private static List<String> errors = new ArrayList<>();
+
+ protected static final JavaLanguageServer server =
+ LanguageServerFixture.getJavaLanguageServer(WarningsTest::onError);
+
+ private static void onError(Diagnostic error) {
+ var string = String.format("%s(%d)", error.code, error.range.start.line + 1);
+ errors.add(string);
+ }
+
+ @Before
+ public void setup() {
+ errors.clear();
+ }
+
+ @Test
+ public void unusedLocal() {
+ server.reportErrors(List.of(FindResource.uri("org/javacs/warn/Unused.java")));
+ assertThat(errors, hasItem("unused(5)"));
+ }
+}
diff --git a/src/test/test-project/workspace/src/org/javacs/warn/Unused.java b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
new file mode 100644
index 0000000..9b4f11c
--- /dev/null
+++ b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
@@ -0,0 +1,7 @@
+package org.javacs.warn;
+
+class Unused {
+ void test() {
+ int unusedLocal = 1;
+ }
+} \ No newline at end of file