|
| 1 | +local class = require('java-core.utils.class') |
| 2 | + |
| 3 | +local MessageId = require('java-test.results.message-id') |
| 4 | +local TestStatus = require('java-test.results.result-status') |
| 5 | +local TestExecStatus = require('java-test.results.execution-status') |
| 6 | + |
| 7 | +---@class java_test.TestParser |
| 8 | +---@field private test_details java_test.TestResults[] |
| 9 | +local TestParser = class() |
| 10 | + |
| 11 | +---Init |
| 12 | +---@private |
| 13 | +function TestParser:_init() |
| 14 | + self.test_details = {} |
| 15 | +end |
| 16 | + |
| 17 | +---@private |
| 18 | +TestParser.node_parsers = { |
| 19 | + [MessageId.TestTree] = 'parse_test_tree', |
| 20 | + [MessageId.TestStart] = 'parse_test_start', |
| 21 | + [MessageId.TestEnd] = 'parse_test_end', |
| 22 | + [MessageId.TestFailed] = 'parse_test_failed', |
| 23 | +} |
| 24 | + |
| 25 | +---@private |
| 26 | +TestParser.strtobool = { |
| 27 | + ['true'] = true, |
| 28 | + ['false'] = false, |
| 29 | +} |
| 30 | + |
| 31 | +---Parse a given text into test details |
| 32 | +---@param text string test result buffer |
| 33 | +function TestParser:parse(text) |
| 34 | + if text:sub(-1) ~= '\n' then |
| 35 | + text = text .. '\n' |
| 36 | + end |
| 37 | + |
| 38 | + local line_iter = text:gmatch('(.-)\n') |
| 39 | + |
| 40 | + local line = line_iter() |
| 41 | + |
| 42 | + while line ~= nil do |
| 43 | + local message_id = line:sub(1, 8):gsub('%s+', '') |
| 44 | + local content = line:sub(9) |
| 45 | + |
| 46 | + local node_parser = TestParser.node_parsers[message_id] |
| 47 | + |
| 48 | + if node_parser then |
| 49 | + local data = vim.split(content, ',', { plain = true, trimempty = true }) |
| 50 | + |
| 51 | + if self[TestParser.node_parsers[message_id]] then |
| 52 | + self[TestParser.node_parsers[message_id]](self, data, line_iter) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + line = line_iter() |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +---Returns the parsed test details |
| 61 | +---@return java_test.TestResults # parsed test details |
| 62 | +function TestParser:get_test_details() |
| 63 | + return self.test_details |
| 64 | +end |
| 65 | + |
| 66 | +---@private |
| 67 | +function TestParser:parse_test_tree(data) |
| 68 | + local node = { |
| 69 | + test_id = tonumber(data[1]), |
| 70 | + test_name = data[2], |
| 71 | + is_suite = TestParser.strtobool[data[3]], |
| 72 | + test_count = tonumber(data[4]), |
| 73 | + is_dynamic_test = TestParser.strtobool[data[5]], |
| 74 | + parent_id = tonumber(data[6]), |
| 75 | + display_name = data[7], |
| 76 | + parameter_types = data[8], |
| 77 | + unique_id = data[9], |
| 78 | + } |
| 79 | + |
| 80 | + local parent = self:find_result_node(node.parent_id) |
| 81 | + |
| 82 | + if not parent then |
| 83 | + table.insert(self.test_details, node) |
| 84 | + else |
| 85 | + parent.children = parent.children or {} |
| 86 | + table.insert(parent.children, node) |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +---@private |
| 91 | +function TestParser:parse_test_start(data) |
| 92 | + local test_id = tonumber(data[1]) |
| 93 | + local node = self:find_result_node(test_id) |
| 94 | + assert(node) |
| 95 | + node.result = {} |
| 96 | + node.result.execution = TestExecStatus.Started |
| 97 | +end |
| 98 | + |
| 99 | +---@private |
| 100 | +function TestParser:parse_test_end(data) |
| 101 | + local test_id = tonumber(data[1]) |
| 102 | + local node = self:find_result_node(test_id) |
| 103 | + assert(node) |
| 104 | + node.result.execution = TestExecStatus.Ended |
| 105 | +end |
| 106 | + |
| 107 | +---@private |
| 108 | +function TestParser:parse_test_failed(data, line_iter) |
| 109 | + local test_id = tonumber(data[1]) |
| 110 | + local node = self:find_result_node(test_id) |
| 111 | + assert(node) |
| 112 | + |
| 113 | + node.result.status = TestStatus.Failed |
| 114 | + |
| 115 | + while true do |
| 116 | + local line = line_iter() |
| 117 | + |
| 118 | + if line == nil then |
| 119 | + break |
| 120 | + end |
| 121 | + |
| 122 | + -- EXPECTED |
| 123 | + if vim.startswith(line, MessageId.ExpectStart) then |
| 124 | + node.result.expected = |
| 125 | + self:get_content_until_end_tag(MessageId.ExpectEnd, line_iter) |
| 126 | + |
| 127 | + -- ACTUAL |
| 128 | + elseif vim.startswith(line, MessageId.ActualStart) then |
| 129 | + node.result.actual = |
| 130 | + self:get_content_until_end_tag(MessageId.ActualEnd, line_iter) |
| 131 | + |
| 132 | + -- TRACE |
| 133 | + elseif vim.startswith(line, MessageId.TraceStart) then |
| 134 | + node.result.trace = |
| 135 | + self:get_content_until_end_tag(MessageId.TraceEnd, line_iter) |
| 136 | + end |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +---@private |
| 141 | +function TestParser:get_content_until_end_tag(end_tag, line_iter) |
| 142 | + local content = {} |
| 143 | + |
| 144 | + while true do |
| 145 | + local line = line_iter() |
| 146 | + |
| 147 | + if line == nil or vim.startswith(line, end_tag) then |
| 148 | + break |
| 149 | + end |
| 150 | + |
| 151 | + table.insert(content, line) |
| 152 | + end |
| 153 | + |
| 154 | + return content |
| 155 | +end |
| 156 | + |
| 157 | +---@private |
| 158 | +function TestParser:find_result_node(id) |
| 159 | + local function find_node(nodes) |
| 160 | + if not nodes or #nodes == 0 then |
| 161 | + return |
| 162 | + end |
| 163 | + |
| 164 | + for _, node in ipairs(nodes) do |
| 165 | + if node.test_id == id then |
| 166 | + return node |
| 167 | + end |
| 168 | + |
| 169 | + local _node = find_node(node.children) |
| 170 | + |
| 171 | + if _node then |
| 172 | + return _node |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + return find_node(self.test_details) |
| 178 | +end |
| 179 | + |
| 180 | +return TestParser |
| 181 | + |
| 182 | +---@class java_test.TestResultExecutionDetails |
| 183 | +---@field actual string[] lines |
| 184 | +---@field expected string[] lines |
| 185 | +---@field status java_test.TestStatus |
| 186 | +---@field execution java_test.TestExecutionStatus |
| 187 | +---@field trace string[] lines |
| 188 | + |
| 189 | +---@class java_test.TestResults |
| 190 | +---@field display_name string |
| 191 | +---@field is_dynamic_test boolean |
| 192 | +---@field is_suite boolean |
| 193 | +---@field parameter_types string |
| 194 | +---@field parent_id integer |
| 195 | +---@field test_count integer |
| 196 | +---@field test_id integer |
| 197 | +---@field test_name string |
| 198 | +---@field unique_id string |
| 199 | +---@field result java_test.TestResultExecutionDetails |
| 200 | +---@field children java_test.TestResults[] |
0 commit comments