-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathapi.lua
More file actions
194 lines (150 loc) · 5.64 KB
/
api.lua
File metadata and controls
194 lines (150 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
local log = require('java-core.utils.log2')
local notify = require('java-core.utils.notify')
local test_adapters = require('java-test.adapters')
local dap_adapters = require('java-dap.data-adapters')
local buf_util = require('java-core.utils.buffer')
local win_util = require('java.utils.window')
local DebugClient = require('java-core.ls.clients.java-debug-client')
local TestClient = require('java-core.ls.clients.java-test-client')
---@class java_test.TestApi
---@field private client java-core.JdtlsClient
---@field private debug_client java-core.DebugClient
---@field private test_client java-core.TestClient
---@field private runner java-dap.DapRunner
local M = {}
---Returns a new test helper client
---@param args { client: vim.lsp.Client, runner: java-dap.DapRunner }
---@return java_test.TestApi
function M:new(args)
local o = {
client = args.client,
}
o.debug_client = DebugClient(args.client)
o.test_client = TestClient(args.client)
o.runner = args.runner
setmetatable(o, self)
self.__index = self
return o
end
---Returns a list of test methods
---@param file_uri string uri of the class
---@return java-core.TestDetailsWithRange[] # list of test methods
function M:get_test_methods(file_uri)
log.debug('finding test methods for uri: ' .. file_uri)
local classes = self.test_client:find_test_types_and_methods(file_uri)
local methods = {}
for _, class in ipairs(classes) do
for _, method in ipairs(class.children) do
---@diagnostic disable-next-line: inject-field
method.class = class
table.insert(methods, method)
end
end
log.debug('found ' .. #methods .. ' test methods')
return methods
end
---comment
---@param buffer number
---@param report java-test.JUnitTestReport
---@param config? java-dap.DapLauncherConfigOverridable config to override the default values in test launcher config
function M:run_class_by_buffer(buffer, report, config)
log.debug('running test class from buffer: ' .. buffer)
local tests = self:get_test_class_by_buffer(buffer)
if #tests < 1 then
notify.warn('No tests found in the current buffer')
return
end
log.debug('found ' .. #tests .. ' test classes')
self:run_test(tests, report, config)
end
---Returns test classes in the given buffer
---@private
---@param buffer integer
---@return java-core.TestDetailsWithChildrenAndRange # get test class details
function M:get_test_class_by_buffer(buffer)
log.debug('finding test class by buffer')
local uri = vim.uri_from_bufnr(buffer)
return self.test_client:find_test_types_and_methods(uri)
end
---Run the given test
---@param tests java-core.TestDetails[]
---@param report java-test.JUnitTestReport
---@param config? java-dap.DapLauncherConfigOverridable config to override the default values in test launcher config
function M:run_test(tests, report, config)
log.debug('running ' .. #tests .. ' tests')
local launch_args = self.test_client:resolve_junit_launch_arguments(test_adapters.tests_to_junit_launch_params(tests))
log.debug(
'resolved launch args - mainClass: ' .. launch_args.mainClass .. ', projectName: ' .. launch_args.projectName
)
local java_exec = self.debug_client:resolve_java_executable(launch_args.mainClass, launch_args.projectName)
log.debug('java executable', java_exec)
local dap_launcher_config = dap_adapters.junit_launch_args_to_dap_config(launch_args, java_exec, {
debug = true,
label = 'Launch All Java Tests',
})
dap_launcher_config = vim.tbl_deep_extend('force', dap_launcher_config, config or {})
log.debug('launching tests with config', dap_launcher_config)
self.runner:run_by_config(dap_launcher_config, report)
end
---Run the current test class
---@param report java-test.JUnitTestReport
---@param config java-dap.DapLauncherConfigOverridable
function M:execute_current_test_class(report, config)
log.debug('running the current class')
return self:run_class_by_buffer(buf_util.get_curr_buf(), report, config)
end
---Run the current test method
---@param report java-test.JUnitTestReport
---@param config java-dap.DapLauncherConfigOverridable
function M:execute_current_test_method(report, config)
log.debug('running the current method')
local method = self:find_current_test_method()
if not method then
notify.warn('cursor is not on a test method')
return
end
self:run_test({ method }, report, config)
end
---Find the test method at the current cursor position
---@return java-core.TestDetailsWithRange | nil
function M:find_current_test_method()
log.debug('finding the current test method')
local cursor = win_util.get_cursor()
local methods = self:get_test_methods(buf_util.get_curr_uri())
for _, method in ipairs(methods) do
local line_start = method.range.start.line
local line_end = method.range['end'].line
if cursor.line >= line_start and cursor.line <= line_end then
return method
end
end
end
---Run all tests in the workspace
---@param report java-test.JUnitTestReport
---@param config java-dap.DapLauncherConfigOverridable
function M:execute_all_tests(report, config)
log.debug('running all tests')
local projects = self.test_client:find_java_projects()
if #projects < 1 then
notify.warn('No Java projects found')
return
end
-- Discover test classes from all projects
local all_tests = {}
for _, project in ipairs(projects) do
local packages = self.test_client:find_test_packages_and_types(project.jdtHandler)
for _, pkg in ipairs(packages or {}) do
-- Package children are the test classes
for _, test_class in ipairs(pkg.children or {}) do
table.insert(all_tests, test_class)
end
end
end
if #all_tests < 1 then
notify.warn('No tests found in workspace')
return
end
log.debug('found ' .. #all_tests .. ' test classes')
self:run_test(all_tests, report, config)
end
return M