Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,34 @@ require('java').dap.config_dap()

**Test**

- `run_current_test_class` - Run the test class in the active buffer
- `run_current_class` - Run the test class in the active buffer

```lua
require('java').test.run_current_test_class()
require('java').test.run_current_class()
```

- `debug_current_test_class` - Debug the test class in the active buffer
- `debug_current_class` - Debug the test class in the active buffer

```lua
require('java').test.debug_current_test_class()
require('java').test.debug_current_class()
```

- `run_current_method` - Run the test method on the cursor

```lua
require('java').test.run_current_method()
```

- `debug_current_method` - Debug the test method on the cursor

```lua
require('java').test.debug_current_method()
```

- `view_report` - Open the last test report in a popup window

```lua
require('java').test.view_report()
```

</details>
Expand Down
9 changes: 7 additions & 2 deletions lua/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ M.dap.config_dap = dap.config_dap
-- Test APIs --
----------------------------------------------------------------------
M.test = {}
M.test.run_current_test_class = test.run_current_test_class
M.test.debug_current_test_class = test.debug_current_test_class
M.test.run_current_class = test.run_current_class
M.test.debug_current_class = test.debug_current_class

M.test.run_current_method = test.run_current_method
M.test.debug_current_method = test.debug_current_method

M.test.view_report = test.view_report

----------------------------------------------------------------------
-- Manipulate --
Expand Down
41 changes: 32 additions & 9 deletions lua/java/api/test.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
local JavaDap = require('java.dap')

local log = require('java.utils.log')
local get_error_handler = require('java.handlers.error')
local jdtls = require('java.utils.jdtls')
local get_error_handler = require('java.handlers.error')

local async = require('java-core.utils.async').sync

local M = {}
local JUnitReport = require('java-test.reports.junit')
local ResultParserFactory = require('java-test.results.result-parser-factory')
local ReportViewer = require('java-test.ui.floating-report-viewer')

---Setup dap config & adapter on jdtls attach event
local M = {
---@type java_test.JUnitTestReport
last_report = nil,
}

function M.run_current_test_class()
---Setup dap config & adapter on jdtls attach event
function M.run_current_class()
log.info('run current test class')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
return JavaDap:new(jdtls())
:execute_current_test_class(M.get_report(), { noDebug = true })
end)
.catch(get_error_handler('failed to run the current test class'))
.run()
end

function M.debug_current_test_class()
function M.debug_current_class()
log.info('debug current test class')

return async(function()
JavaDap:new(jdtls()):execute_current_test_class({})
JavaDap:new(jdtls()):execute_current_test_class(M.get_report(), {})
end)
.catch(get_error_handler('failed to debug the current test class'))
.run()
Expand All @@ -33,7 +41,8 @@ function M.debug_current_method()
log.info('debug current test method')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_method()
return JavaDap:new(jdtls())
:execute_current_test_method(M.get_report(), {})
end)
.catch(get_error_handler('failed to run the current test method'))
.run()
Expand All @@ -44,12 +53,19 @@ function M.run_current_method()

return async(function()
return JavaDap:new(jdtls())
:execute_current_test_method({ noDebug = true })
:execute_current_test_method(M.get_report(), { noDebug = true })
end)
.catch(get_error_handler('failed to run the current test method'))
.run()
end

function M.view_report()
if M.last_report then
M.last_report:show_report()
end
end

---@private
function M.config_dap()
log.info('configuring dap')

Expand All @@ -60,4 +76,11 @@ function M.config_dap()
.run()
end

---@private
function M.get_report()
local report = JUnitReport(ResultParserFactory(), ReportViewer())
M.last_report = report
return report
end

return M
20 changes: 14 additions & 6 deletions lua/java/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
local log = require('java.utils.log')
local async = require('java-core.utils.async').sync
local buf_util = require('java.utils.buffer')
local win_util = require('java.utils.window')

local async = require('java-core.utils.async').sync
local notify = require('java-core.utils.notify')

local JavaCoreDap = require('java-core.dap')
local JavaCoreTestApi = require('java-core.api.test')
local JavaCoreTestClient = require('java-core.ls.clients.java-test-client')
local JavaCoreDapRunner = require('java-core.dap.runner')

---@class JavaDap
---@field private client LspClient
---@field private dap JavaCoreDap
---@field private test_api JavaCoreTestApi
---@field private test_api java_core.TestApi
---@field private test_client java_core.TestClient
local M = {}

Expand All @@ -24,6 +26,7 @@ function M:new(args)

o.test_api = JavaCoreTestApi:new({
client = args.client,
runner = JavaCoreDapRunner:new(),
})

o.test_client = JavaCoreTestClient:new({
Expand All @@ -40,14 +43,19 @@ function M:new(args)
end

---Run the current test class
---@param report java_test.JUnitTestReport
---@param config JavaCoreDapLauncherConfigOverridable
function M:execute_current_test_class(config)
function M:execute_current_test_class(report, config)
log.debug('running the current class')

return self.test_api:run_class_by_buffer(buf_util.get_curr_buf(), config)
return self.test_api:run_class_by_buffer(
buf_util.get_curr_buf(),
report,
config
)
end

function M:execute_current_test_method(config)
function M:execute_current_test_method(report, config)
log.debug('running the current method')

local method = self:find_current_test_method()
Expand All @@ -57,7 +65,7 @@ function M:execute_current_test_method(config)
return
end

self.test_api:run_test({ method }, config)
self.test_api:run_test({ method }, report, config)
end

function M:find_current_test_method()
Expand Down
7 changes: 7 additions & 0 deletions tests/prepare-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ vim.opt.rtp:prepend(lazypath)

local temp_path = './.test_plugins'
local java_core_path = vim.fn.expand('~/Workspace/nvim-java-core')
local java_test_path = vim.fn.expand('~/Workspace/nvim-java-test')

require('lazy').setup({
{
'nvim-lua/plenary.nvim',
lazy = false,
},
{
'nvim-java/nvim-java-test',
---@diagnostic disable-next-line: assign-type-mismatch
dir = vim.fn.isdirectory(java_test_path) == 1 and java_test_path or nil,
lazy = false,
},
{
'nvim-java/nvim-java-core',
---@diagnostic disable-next-line: assign-type-mismatch
Expand Down
6 changes: 6 additions & 0 deletions tests/test-config.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
local java_core_path = vim.fn.expand('~/Workspace/nvim-java-core')
local java_test_path = vim.fn.expand('~/Workspace/nvim-java-test')

local plug_path = './.test_plugins'

java_core_path = (vim.fn.isdirectory(java_core_path) == 1) and java_core_path
or (plug_path .. '/nvim-java-core')

java_test_path = (vim.fn.isdirectory(java_test_path) == 1) and java_test_path
or (plug_path .. '/nvim-java-test')

vim.opt.rtp:append(plug_path .. '/plenary.nvim')
vim.opt.rtp:append(plug_path .. '/nvim-lspconfig')
vim.opt.rtp:append(plug_path .. '/mason.nvim')
vim.opt.rtp:append(java_core_path)
vim.opt.rtp:append(java_test_path)