feat: :CP test refactor
This commit is contained in:
parent
b2e1ea2c58
commit
fb240fd501
10 changed files with 97 additions and 200 deletions
|
|
@ -15,7 +15,8 @@
|
|||
---@class CachedTestCase
|
||||
---@field index? number
|
||||
---@field input string
|
||||
---@field output string
|
||||
---@field expected? string
|
||||
---@field output? string
|
||||
|
||||
local M = {}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@
|
|||
---@field hooks Hooks
|
||||
---@field debug boolean
|
||||
---@field scrapers table<string, boolean>
|
||||
---@field tile? fun(source_buf: number, input_buf: number, output_buf: number)
|
||||
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
|
||||
|
||||
---@class cp.UserConfig
|
||||
|
|
@ -46,7 +45,6 @@
|
|||
---@field hooks? Hooks
|
||||
---@field debug? boolean
|
||||
---@field scrapers? table<string, boolean>
|
||||
---@field tile? fun(source_buf: number, input_buf: number, output_buf: number)
|
||||
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
|
||||
|
||||
local M = {}
|
||||
|
|
@ -67,7 +65,6 @@ M.defaults = {
|
|||
return platform, true
|
||||
end)
|
||||
:totable(),
|
||||
tile = nil,
|
||||
filename = nil,
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +82,6 @@ function M.setup(user_config)
|
|||
hooks = { user_config.hooks, { "table", "nil" }, true },
|
||||
debug = { user_config.debug, { "boolean", "nil" }, true },
|
||||
scrapers = { user_config.scrapers, { "table", "nil" }, true },
|
||||
tile = { user_config.tile, { "function", "nil" }, true },
|
||||
filename = { user_config.filename, { "function", "nil" }, true },
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
local M = {}
|
||||
|
||||
M.PLATFORMS = { "atcoder", "codeforces", "cses" }
|
||||
M.ACTIONS = { "run", "debug", "test", "next", "prev" }
|
||||
M.ACTIONS = { "test", "next", "prev" }
|
||||
|
||||
M.CPP = "cpp"
|
||||
M.PYTHON = "python"
|
||||
|
|
|
|||
|
|
@ -193,8 +193,9 @@ end
|
|||
|
||||
---@param ctx ProblemContext
|
||||
---@param contest_config ContestConfig
|
||||
---@param is_debug? boolean
|
||||
---@return boolean success
|
||||
function M.compile_problem(ctx, contest_config)
|
||||
function M.compile_problem(ctx, contest_config, is_debug)
|
||||
vim.validate({
|
||||
ctx = { ctx, "table" },
|
||||
contest_config = { contest_config, "table" },
|
||||
|
|
@ -214,13 +215,15 @@ function M.compile_problem(ctx, contest_config)
|
|||
version = tostring(language_config.version),
|
||||
}
|
||||
|
||||
if language_config.compile then
|
||||
local compile_cmd = (is_debug and language_config.debug) and language_config.debug or language_config.compile
|
||||
if compile_cmd then
|
||||
language_config.compile = compile_cmd
|
||||
local compile_result = M.compile_generic(language_config, substitutions)
|
||||
if compile_result.code ~= 0 then
|
||||
logger.log("compilation failed: " .. (compile_result.stderr or "unknown error"), vim.log.levels.ERROR)
|
||||
return false
|
||||
end
|
||||
logger.log("compilation successful")
|
||||
logger.log(("compilation successful (%s)"):format(is_debug and "debug mode" or "test mode"))
|
||||
end
|
||||
|
||||
return true
|
||||
|
|
|
|||
134
lua/cp/init.lua
134
lua/cp/init.lua
|
|
@ -4,7 +4,6 @@ local config_module = require("cp.config")
|
|||
local snippets = require("cp.snippets")
|
||||
local execute = require("cp.execute")
|
||||
local scrape = require("cp.scrape")
|
||||
local window = require("cp.window")
|
||||
local logger = require("cp.log")
|
||||
local problem = require("cp.problem")
|
||||
local cache = require("cp.cache")
|
||||
|
|
@ -133,13 +132,6 @@ local function setup_problem(contest_id, problem_id, language)
|
|||
config.hooks.setup_code(ctx)
|
||||
end
|
||||
|
||||
local src_buf = vim.api.nvim_get_current_buf()
|
||||
local input_buf = vim.fn.bufnr(ctx.input_file, true)
|
||||
local output_buf = vim.fn.bufnr(ctx.output_file, true)
|
||||
|
||||
local tile_fn = config.tile or window.default_tile
|
||||
tile_fn(src_buf, input_buf, output_buf)
|
||||
|
||||
logger.log(("switched to problem %s"):format(ctx.problem_name))
|
||||
end
|
||||
|
||||
|
|
@ -152,60 +144,7 @@ local function get_current_problem()
|
|||
return filename
|
||||
end
|
||||
|
||||
local function run_problem()
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
end
|
||||
|
||||
logger.log(("running problem: %s"):format(problem_id))
|
||||
|
||||
if not state.platform then
|
||||
logger.log(
|
||||
"No contest configured. Use :CP <platform> <contest> <problem> to set up first.",
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[state.platform]
|
||||
local ctx = problem.create_context(state.platform, state.contest_id, state.problem_id, config)
|
||||
|
||||
if config.hooks and config.hooks.before_run then
|
||||
config.hooks.before_run(ctx)
|
||||
end
|
||||
|
||||
vim.schedule(function()
|
||||
execute.run_problem(ctx, contest_config, false)
|
||||
vim.cmd.checktime()
|
||||
end)
|
||||
end
|
||||
|
||||
local function debug_problem()
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
end
|
||||
|
||||
if not state.platform then
|
||||
logger.log("no platform set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[state.platform]
|
||||
local ctx = problem.create_context(state.platform, state.contest_id, state.problem_id, config)
|
||||
|
||||
if config.hooks and config.hooks.before_debug then
|
||||
config.hooks.before_debug(ctx)
|
||||
end
|
||||
|
||||
vim.schedule(function()
|
||||
execute.run_problem(ctx, contest_config, true)
|
||||
vim.cmd.checktime()
|
||||
end)
|
||||
end
|
||||
|
||||
local function toggle_test_panel()
|
||||
local function toggle_test_panel(is_debug)
|
||||
if state.test_panel_active then
|
||||
if state.saved_session then
|
||||
vim.cmd(("source %s"):format(state.saved_session))
|
||||
|
|
@ -225,11 +164,6 @@ local function toggle_test_panel()
|
|||
return
|
||||
end
|
||||
|
||||
if state.platform == "codeforces" then
|
||||
logger.log("test panel not yet supported for codeforces", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
|
|
@ -347,14 +281,14 @@ local function toggle_test_panel()
|
|||
return
|
||||
end
|
||||
|
||||
local expected_lines = {}
|
||||
|
||||
local expected_text = current_test.expected
|
||||
for _, line in ipairs(vim.split(expected_text, "\n", { plain = true, trimempty = true })) do
|
||||
table.insert(expected_lines, line)
|
||||
end
|
||||
local expected_lines = vim.split(expected_text, "\n", { plain = true, trimempty = true })
|
||||
|
||||
vim.api.nvim_buf_set_lines(test_buffers.expected_buf, 0, -1, false, expected_lines)
|
||||
|
||||
if vim.fn.has("nvim-0.8.0") == 1 then
|
||||
vim.api.nvim_set_option_value("winbar", "Expected", { win = test_windows.expected_win })
|
||||
end
|
||||
end
|
||||
|
||||
local function update_actual_pane()
|
||||
|
|
@ -366,27 +300,32 @@ local function toggle_test_panel()
|
|||
end
|
||||
|
||||
local actual_lines = {}
|
||||
local enable_diff = false
|
||||
|
||||
if current_test.actual then
|
||||
for _, line in ipairs(vim.split(current_test.actual, "\n", { plain = true, trimempty = true })) do
|
||||
table.insert(actual_lines, line)
|
||||
end
|
||||
|
||||
if current_test.status == "fail" then
|
||||
vim.api.nvim_set_option_value("diff", true, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", true, { win = test_windows.actual_win })
|
||||
else
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.actual_win })
|
||||
end
|
||||
actual_lines = vim.split(current_test.actual, "\n", { plain = true, trimempty = true })
|
||||
enable_diff = current_test.status == "fail"
|
||||
else
|
||||
table.insert(actual_lines, "(not run yet)")
|
||||
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.actual_win })
|
||||
actual_lines = { "(not run yet)" }
|
||||
end
|
||||
|
||||
vim.api.nvim_buf_set_lines(test_buffers.actual_buf, 0, -1, false, actual_lines)
|
||||
|
||||
if vim.fn.has("nvim-0.8.0") == 1 then
|
||||
vim.api.nvim_set_option_value("winbar", "Actual", { win = test_windows.actual_win })
|
||||
end
|
||||
|
||||
vim.api.nvim_set_option_value("diff", enable_diff, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", enable_diff, { win = test_windows.actual_win })
|
||||
|
||||
if enable_diff then
|
||||
vim.api.nvim_win_call(test_windows.expected_win, function()
|
||||
vim.cmd("diffthis")
|
||||
end)
|
||||
vim.api.nvim_win_call(test_windows.actual_win, function()
|
||||
vim.cmd("diffthis")
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
local function refresh_test_panel()
|
||||
|
|
@ -430,9 +369,13 @@ local function toggle_test_panel()
|
|||
end, { buffer = buf, silent = true })
|
||||
end
|
||||
|
||||
if is_debug and config.hooks and config.hooks.before_debug then
|
||||
config.hooks.before_debug(ctx)
|
||||
end
|
||||
|
||||
local execute_module = require("cp.execute")
|
||||
local contest_config = config.contests[state.platform]
|
||||
if execute_module.compile_problem(ctx, contest_config) then
|
||||
if execute_module.compile_problem(ctx, contest_config, is_debug) then
|
||||
test_module.run_all_test_cases(ctx, contest_config)
|
||||
end
|
||||
|
||||
|
|
@ -515,6 +458,7 @@ local function parse_command(args)
|
|||
end
|
||||
|
||||
local language = nil
|
||||
local debug = false
|
||||
|
||||
for i, arg in ipairs(args) do
|
||||
local lang_match = arg:match("^--lang=(.+)$")
|
||||
|
|
@ -526,17 +470,19 @@ local function parse_command(args)
|
|||
else
|
||||
return { type = "error", message = "--lang requires a value" }
|
||||
end
|
||||
elseif arg == "--debug" then
|
||||
debug = true
|
||||
end
|
||||
end
|
||||
|
||||
local filtered_args = vim.tbl_filter(function(arg)
|
||||
return not (arg:match("^--lang") or arg == language)
|
||||
return not (arg:match("^--lang") or arg == language or arg == "--debug")
|
||||
end, args)
|
||||
|
||||
local first = filtered_args[1]
|
||||
|
||||
if vim.tbl_contains(actions, first) then
|
||||
return { type = "action", action = first, language = language }
|
||||
return { type = "action", action = first, language = language, debug = debug }
|
||||
end
|
||||
|
||||
if vim.tbl_contains(platforms, first) then
|
||||
|
|
@ -604,12 +550,8 @@ function M.handle_command(opts)
|
|||
end
|
||||
|
||||
if cmd.type == "action" then
|
||||
if cmd.action == "run" then
|
||||
run_problem()
|
||||
elseif cmd.action == "debug" then
|
||||
debug_problem()
|
||||
elseif cmd.action == "test" then
|
||||
toggle_test_panel()
|
||||
if cmd.action == "test" then
|
||||
toggle_test_panel(cmd.debug)
|
||||
elseif cmd.action == "next" then
|
||||
navigate_problem(1, cmd.language)
|
||||
elseif cmd.action == "prev" then
|
||||
|
|
|
|||
|
|
@ -1,3 +1,14 @@
|
|||
---@class ScraperTestCase
|
||||
---@field input string
|
||||
---@field expected string
|
||||
|
||||
---@class ScraperResult
|
||||
---@field success boolean
|
||||
---@field problem_id string
|
||||
---@field url? string
|
||||
---@field tests? ScraperTestCase[]
|
||||
---@field error? string
|
||||
|
||||
local M = {}
|
||||
local logger = require("cp.log")
|
||||
local cache = require("cp.cache")
|
||||
|
|
@ -139,7 +150,7 @@ function M.scrape_contest_metadata(platform, contest_id)
|
|||
end
|
||||
|
||||
---@param ctx ProblemContext
|
||||
---@return {success: boolean, problem_id: string, test_count?: number, test_cases?: table[], url?: string, error?: string}
|
||||
---@return {success: boolean, problem_id: string, test_count?: number, test_cases?: ScraperTestCase[], url?: string, error?: string}
|
||||
function M.scrape_problem(ctx)
|
||||
vim.validate({
|
||||
ctx = { ctx, "table" },
|
||||
|
|
@ -249,40 +260,32 @@ function M.scrape_problem(ctx)
|
|||
return data
|
||||
end
|
||||
|
||||
if data.test_cases and #data.test_cases > 0 then
|
||||
if data.tests and #data.tests > 0 then
|
||||
local base_name = vim.fn.fnamemodify(ctx.input_file, ":r")
|
||||
|
||||
for i, test_case in ipairs(data.test_cases) do
|
||||
for i, test_case in ipairs(data.tests) do
|
||||
local input_file = base_name .. "." .. i .. ".cpin"
|
||||
local expected_file = base_name .. "." .. i .. ".cpout"
|
||||
|
||||
local input_content = test_case.input:gsub("\r", "")
|
||||
local expected_content = test_case.output:gsub("\r", "")
|
||||
local expected_content = test_case.expected:gsub("\r", "")
|
||||
|
||||
vim.fn.writefile(vim.split(input_content, "\n", true), input_file)
|
||||
vim.fn.writefile(vim.split(expected_content, "\n", true), expected_file)
|
||||
end
|
||||
|
||||
local combined_input = data.combined and data.combined.input:gsub("\r", "")
|
||||
or table.concat(
|
||||
vim.tbl_map(function(tc)
|
||||
return tc.input
|
||||
end, data.test_cases),
|
||||
"\n"
|
||||
)
|
||||
local combined_output = data.combined and data.combined.output:gsub("\r", "")
|
||||
or table.concat(
|
||||
vim.tbl_map(function(tc)
|
||||
return tc.output
|
||||
end, data.test_cases),
|
||||
"\n"
|
||||
)
|
||||
|
||||
-- with atcoder, we combine together multiple test cases
|
||||
-- TODO: per-platform settings to do this (i.e. do we stitch?)
|
||||
if ctx.contest == "atcoder" then
|
||||
combined_input = tostring(#data.test_cases) .. "\n" .. combined_input
|
||||
end
|
||||
local combined_input = table.concat(
|
||||
vim.tbl_map(function(tc)
|
||||
return tc.input
|
||||
end, data.tests),
|
||||
"\n"
|
||||
)
|
||||
local combined_output = table.concat(
|
||||
vim.tbl_map(function(tc)
|
||||
return tc.expected
|
||||
end, data.tests),
|
||||
"\n"
|
||||
)
|
||||
|
||||
vim.fn.writefile(vim.split(combined_input, "\n", true), ctx.input_file)
|
||||
vim.fn.writefile(vim.split(combined_output, "\n", true), ctx.expected_file)
|
||||
|
|
@ -291,8 +294,8 @@ function M.scrape_problem(ctx)
|
|||
return {
|
||||
success = true,
|
||||
problem_id = ctx.problem_name,
|
||||
test_count = data.test_cases and #data.test_cases or 0,
|
||||
test_cases = data.test_cases,
|
||||
test_count = data.tests and #data.tests or 0,
|
||||
test_cases = data.tests,
|
||||
url = data.url,
|
||||
}
|
||||
end
|
||||
|
|
|
|||
|
|
@ -68,7 +68,8 @@ local function parse_test_cases_from_cache(platform, contest_id, problem_id)
|
|||
|
||||
for i, test_case in ipairs(cached_test_cases) do
|
||||
local index = test_case.index or i
|
||||
table.insert(test_cases, create_test_case(index, test_case.input, test_case.output))
|
||||
local expected = test_case.expected or test_case.output
|
||||
table.insert(test_cases, create_test_case(index, test_case.input, expected))
|
||||
end
|
||||
|
||||
return test_cases
|
||||
|
|
@ -171,9 +172,6 @@ local function run_single_test_case(ctx, contest_config, test_case)
|
|||
local run_cmd = build_command(language_config.run, language_config.executable, substitutions)
|
||||
|
||||
local stdin_content = test_case.input .. "\n"
|
||||
if ctx.contest == "atcoder" then
|
||||
stdin_content = "1\n" .. stdin_content
|
||||
end
|
||||
|
||||
local start_time = vim.uv.hrtime()
|
||||
local result = vim.system(run_cmd, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue