Skip to content

Commit

Permalink
feat: handle parametrized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atm1020 committed Jun 25, 2024
1 parent 01ee80c commit 5485396
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 42 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.tests
.idea
1 change: 0 additions & 1 deletion .tests/site/pack/deps/start/neotest
Submodule neotest deleted from 6f35d7
1 change: 0 additions & 1 deletion .tests/site/pack/deps/start/nvim-java-core
Submodule nvim-java-core deleted from 469aab
1 change: 0 additions & 1 deletion .tests/site/pack/deps/start/plenary.nvim
Submodule plenary.nvim deleted from a3e3bc
Empty file removed .tests/state/nvim/neotest.log
Empty file.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# neotest-jdtls (Under Development)
* This plugin provides a jdtls adapter for the [Neotest](https://github.com/rcarriga/neotest) framework.
* Currently depends on the nvim-java-core and nvim-java-test projects.
* Integration with [nvim-java](https://github.com/nvim-java/nvim-java) project is tested.

### Limitations
- No support for multi-module projects.
- Only supports running all tests in a file or running a single test method (no directory support).
- Compiler errors are not recognized as errors (the tests appear to pass).
* Depends on the nvim-java-core and nvim-java-test projects.

### Installation

Expand Down
14 changes: 4 additions & 10 deletions lua/neotest-jdtls/impl/excute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,10 @@ local function get_java_test_item(test_file_uri)
arguments = { test_file_uri },
}).result
log.debug('java_test_items', vim.inspect(java_test_items))
if java_test_items == nil then
if #java_test_items ~= 1 then
log.info('Unexpected number of test items found: ', #java_test_items)
return {}
end
assert(
#java_test_items == 1,
'Too many test items found: '
.. #java_test_items
.. ' for '
.. test_file_uri
)
return java_test_items
end

Expand All @@ -91,9 +85,9 @@ local function handle_test(data, test_file_uri)
local java_test_item = java_test_items[1]
---@type JavaTestItem
local closest_item = nil
local start_line = data.range[1] + 1
local end_line = data.range[3]
for _, children in ipairs(java_test_item.children) do
if children.range.start.line == start_line then
if children.range['end'].line == end_line then
closest_item = children
break
end
Expand Down
5 changes: 2 additions & 3 deletions lua/neotest-jdtls/impl/is_test_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ local log = require('neotest-jdtls.log')
local M = {}

M.is_test_file = function(file_path)
local is_test_file = string.find(file_path, 'test') ~= nil
and string.find(file_path, '.java') ~= nil
and string.find(file_path, '.class') == nil
local is_test_file = vim.endswith(file_path, 'Test.java')
or vim.endswith(file_path, 'Tests.java')
log.debug('is_test_file: ', is_test_file)
return is_test_file
end
Expand Down
85 changes: 70 additions & 15 deletions lua/neotest-jdtls/impl/results.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,46 @@ local TestStatus = {
Passed = 'passed',
}

local function get_result_from_ch_node(ch)
if ch.result.status == TestStatus.Failed then
local function get_short_error_message(result)
if result.actual and result.expected then
return string.format(
'Expected: [%s] but was [%s]',
result.expected[1],
result.actual[1]
)
end
local trace_result = ''
for idx, trace in ipairs(result.trace) do
trace_result = trace_result .. trace
if idx > 3 then
break
end
end
return trace_result
end

local function map_to_neotest_result_item(item)
if item.result.status == TestStatus.Failed then
local results_path = async.fn.tempname()
lib.files.write(results_path, table.concat(ch.result.trace, '\n'))
log.debug('stream_path: ', results_path)
lib.files.write(results_path, table.concat(item.result.trace, '\n'))
local short_message = get_short_error_message(item.result)
return {
status = TestStatus.Failed,
errors = {
{ message = ch.result.trace[1] },
{ message = short_message },
},
output = results_path,
short = ch.result.trace[1],
short = short_message,
}
elseif ch.result.status == TestStatus.Skipped then
elseif item.result.status == TestStatus.Skipped then
return {
status = TestStatus.Skipped,
}
else
local results_path = async.fn.tempname()
local log_data
if ch.result.trace then
log_data = table.concat(ch.result.trace, '\n')
if item.result.trace then
log_data = table.concat(item.result.trace, '\n')
else
log_data = 'Test passed (There is no output available)'
end
Expand All @@ -43,23 +61,60 @@ local function get_result_from_ch_node(ch)
end
end

local function group_and_map_test_results(test_result_lookup, suite)
for _, ch in ipairs(suite.children) do
local key = vim.split(ch.test_name, '%(')[1]
if not ch.is_suite then
if test_result_lookup[key] == nil then
test_result_lookup[key] = {}
end
table.insert(test_result_lookup[key], map_to_neotest_result_item(ch))
else
group_and_map_test_results(test_result_lookup, ch)
end
end
end

local function merge_neotest_results(test_result_lookup, node_data)
if test_result_lookup[node_data.name] == nil then
log.debug('No test results found for', node_data.name)
return nil -- Maybe "status = Skipped" would be better with some message
end

if #test_result_lookup[node_data.name] == 1 then
return test_result_lookup[node_data.name][1]
end

local dynamic_test_result = {
status = TestStatus.Passed,
}
for _, result in ipairs(test_result_lookup[node_data.name]) do
-- TODO merge stack traces
if result.status == TestStatus.Failed then
dynamic_test_result.status = TestStatus.Failed
dynamic_test_result.errors = result.errors
dynamic_test_result.output = result.output
break
end
end
return dynamic_test_result
end

---@param spec neotest.RunSpec
---@param result neotest.StrategyResult
---@param tree neotest.Tree
function M.results(spec, _, tree)
local result_map = {}
local test_result_lookup = {}
local report = spec.context.report:get_results()
for _, item in ipairs(report) do
log.debug('item: ', item)
for _, ch in ipairs(item.children) do
local key = vim.split(ch.display_name, '%(')[1]
result_map[key] = get_result_from_ch_node(ch)
if item.children then
group_and_map_test_results(test_result_lookup, item)
end
end
local results = {}
for _, node in tree:iter_nodes() do
local node_data = node:data()
local node_result = result_map[node_data.name]
local node_result = merge_neotest_results(test_result_lookup, node_data)
if node_result then
results[node_data.id] = node_result
end
Expand Down
5 changes: 2 additions & 3 deletions tests/is_test_file_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ local neotest = require('neotest-jdtls')
describe('is_test_file', function()
it('return=true', function()
local files = {
'src/test/java/mock/Mocktest.java',
'src/test/java/mock/MockRepository.java',
'src/test/java/mock/MockTest.java',
'src/test/java/mock/MockRepositoryTest.java',
}

Expand All @@ -15,7 +14,7 @@ describe('is_test_file', function()

it('return=false', function()
local files = {
'src/main/java/mock/MockTest.java',
'src/main/java/mock/Mocktest.java',
'src/main/java/mock/MockRepository.java',
'resources/application.properties',
'pom.xml',
Expand Down

0 comments on commit 5485396

Please sign in to comment.