Skip to content
Open
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
37 changes: 36 additions & 1 deletion src/reporters/lune_console_reporter.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-- -- run tests

local stdio = require("@lune/stdio")
local fs = require("@lune/fs")
local types = require("../types")
local utils = require("../utils")
local inspect = require("../lib/inspect")
Expand All @@ -15,6 +16,8 @@ type TestError = types.TestError

local reporter = {}

reporter.line_preview_range = 3

local _state = {}
_state.indent = 0
_state.current_suite = nil :: types.Suite?
Expand All @@ -35,6 +38,10 @@ function color(c: stdio.Color, s: string)
return `{stdio.color(c)}{s}{stdio.color("reset")}`
end

function style(sty: stdio.Style, s: string)
return `{stdio.style(sty)}{s}{stdio.style("reset")}`
end

function print_test_info_once()
if _state.current_suite ~= nil and _state.suite_printed == false then
_state.suite_printed = true
Expand Down Expand Up @@ -113,9 +120,37 @@ function handle_complex_info(type: string, data: any): string?
return nil
end

function make_padded_line(line: number, longest: number): string
return `{line}{string.rep(" ", longest - #tostring(line))}`
end

function print_preview_dim_line(source_lines: {string}, line: number, longest: number)
if (source_lines[line] == nil) then
return
end
print_indent(style("dim", color("white", `{make_padded_line(line, longest)} | {source_lines[line]}`)))
end

function reporter.print_failure(type: "CHECK" | "REQUIRE", f: Failure)
print_test_info_once()
print_indent(`{color("red", `{type} FAILED:`)} {f.location.file}:{f.location.line}`)
local line = tonumber(f.location.line)
print_indent(`{color("red", `{type} FAILED:`)} {f.location.file}:{line}`)
if (line) then
local range = reporter.line_preview_range
local possible_lines = {}
for i = -range, range do
table.insert(possible_lines, #tostring(line + i))
end
local longest_line_len = math.max(unpack(possible_lines))
local source_lines = fs.readFile(f.location.file):split("\n")
for i = range, 1, -1 do
print_preview_dim_line(source_lines, line - i, longest_line_len)
end
print_indent(style("bold", color("blue", `{make_padded_line(line, longest_line_len)} | {source_lines[f.location.line]}`)))
for i = 1, range, 1 do
print_preview_dim_line(source_lines, line + i, longest_line_len)
end
end
print_indent(`{color("yellow", f.message)}`, 1)
if f.complex_info ~= nil then
local complex_msg = handle_complex_info(f.complex_info.type, f.complex_info.data)
Expand Down