Skip to content

Commit 145c9ec

Browse files
committed
Add unit test for the sample workspace
1 parent a0aaf10 commit 145c9ec

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

adapter/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
// The Java Debug Interface classes (com.sun.jdi.*)
3030
implementation files("${System.properties['java.home']}/../lib/tools.jar")
3131
testImplementation 'junit:junit:4.12'
32+
testImplementation 'org.hamcrest:hamcrest-all:1.3'
3233
}
3334

3435
task fixFilePermissions(type: Exec) {

adapter/src/test/kotlin/org/javacs/ktda/DebugAdapterTestFixture.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.javacs.ktda.jdi.launch.JDILauncher
88
import org.junit.AfterClass
99

1010
abstract class DebugAdapterTestFixture(relativeWorkspaceRoot: String) {
11-
private val absoluteWorkspaceRoot: Path = Paths.get(DebugAdapterTestFixture::class.java.getResource("/$relativeWorkspaceRoot").toURI())
11+
val absoluteWorkspaceRoot: Path = Paths.get(DebugAdapterTestFixture::class.java.getResource("/").toURI()).resolve(relativeWorkspaceRoot)
1212
val debugAdapter: KotlinDebugAdapter = JDILauncher()
1313
.let(::KotlinDebugAdapter)
1414
.also { it.launch(mapOf(
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javacs.ktda
2+
3+
import org.eclipse.lsp4j.debug.ScopesArguments
4+
import org.eclipse.lsp4j.debug.SetBreakpointsArguments
5+
import org.eclipse.lsp4j.debug.Source
6+
import org.eclipse.lsp4j.debug.SourceBreakpoint
7+
import org.eclipse.lsp4j.debug.StackFrame
8+
import org.eclipse.lsp4j.debug.StackTraceArguments
9+
import org.eclipse.lsp4j.debug.StoppedEventArguments
10+
import org.eclipse.lsp4j.debug.VariablesArguments
11+
import org.eclipse.lsp4j.debug.services.IDebugProtocolClient
12+
import org.junit.Assert.assertThat
13+
import org.junit.Test
14+
import org.hamcrest.Matchers.contains
15+
import org.hamcrest.Matchers.equalTo
16+
17+
/**
18+
* Tests a very basic debugging scenario
19+
* using a sample application.
20+
*/
21+
class SampleWorkspaceTest : DebugAdapterTestFixture("sample-workspace") {
22+
@Test private fun testBreakpointsAndVariables() {
23+
debugAdapter.connect(object : IDebugProtocolClient {
24+
override fun stopped(args: StoppedEventArguments) {
25+
assertThat(args.reason, equalTo("breakpoint"))
26+
27+
// Query information about the debuggee's current state
28+
val stackTrace = debugAdapter.stackTrace(StackTraceArguments().apply {
29+
threadId = args.threadId
30+
}).join()
31+
val topFrame = stackTrace.stackFrames.first()
32+
val scopes = debugAdapter.scopes(ScopesArguments().apply {
33+
frameId = topFrame.id
34+
}).join()
35+
val scope = scopes.scopes.first()
36+
val variables = debugAdapter.variables(VariablesArguments().apply {
37+
variablesReference = scope.variablesReference
38+
}).join()
39+
40+
assertThat(variables.variables.map { Pair(it.name, it.value) }, contains(
41+
Pair("member", "\"test\""),
42+
Pair("local", "123")
43+
))
44+
}
45+
})
46+
debugAdapter.setBreakpoints(SetBreakpointsArguments().apply {
47+
source = Source().apply {
48+
path = absoluteWorkspaceRoot
49+
.resolve("src")
50+
.resolve("main")
51+
.resolve("kotlin")
52+
.resolve("sample")
53+
.resolve("workspace")
54+
.resolve("App.kt")
55+
.toString()
56+
}
57+
breakpoints = arrayOf(SourceBreakpoint().apply {
58+
line = 8
59+
})
60+
})
61+
}
62+
}

adapter/src/test/resources/sample-workspace/src/main/kotlin/sample/workspace/App.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package sample.workspace
22

33
class App {
4+
private val member: String = "test"
45
val greeting: String
56
get() {
7+
val local: Int = 123
68
return "Hello world."
79
}
810
}

0 commit comments

Comments
 (0)