From adab52019f41f8b878d672eb83287413734b20d6 Mon Sep 17 00:00:00 2001 From: Jackson Moore Date: Sat, 5 Sep 2026 14:56:44 +0200 Subject: Add canonical prompt field tests --- tests/prompt_specification_test.lua | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/prompt_specification_test.lua (limited to 'tests') diff --git a/tests/prompt_specification_test.lua b/tests/prompt_specification_test.lua new file mode 100644 index 0000000..9262e85 --- /dev/null +++ b/tests/prompt_specification_test.lua @@ -0,0 +1,97 @@ +local entry_path = "yazi-enter.yazi/main.lua" + +local file = assert(io.open(entry_path, "rb")) +local source = assert(file:read("*a")) +file:close() + +local capture = [[ +capture_prompt_specification { + new = new_prompt_specification, + forward = DIRECTION_FORWARD, + backward = DIRECTION_BACKWARD, +} +]] + +local instrumented_source, replacement_count = source:gsub( + "\nreturn {\n\tentry = function", + "\n" .. capture .. "\nreturn {\n\tentry = function", + 1 +) +assert(replacement_count == 1, "prompt specification capture point must occur once") + +local model +local environment = { + error = error, + getmetatable = getmetatable, + pairs = pairs, + pcall = pcall, + type = type, + capture_prompt_specification = function(value) + model = value + end, + ya = { + sync = function(callback) + return callback + end, + }, +} + +local chunk = assert(load(instrumented_source, "@" .. entry_path, "t", environment)) +chunk() + +local function assert_equal(actual, expected, label) + assert(actual == expected, ("%s: expected %s, got %s"):format(label, tostring(expected), tostring(actual))) +end + +local function assert_prompt(prompt, expected_title, label) + assert_equal(prompt.name, "find", label .. " name") + assert_equal(prompt.history, "shared", label .. " history") + assert_equal(prompt.value, "", label .. " initial value") + assert_equal(prompt.obscure, false, label .. " obscure") + assert_equal(prompt.realtime, true, label .. " realtime") + assert_equal(prompt.debounce, 0.05, label .. " debounce") + assert_equal(prompt.title, expected_title, label .. " title") + assert_equal(prompt.pos[1], "top-center", label .. " position anchor") + assert_equal(prompt.pos.x, 0, label .. " position x") + assert_equal(prompt.pos.y, 2, label .. " position y") + assert_equal(prompt.pos.w, 50, label .. " position width") + assert_equal(prompt.pos.h, 3, label .. " position height") + + local expected_keys = { + name = true, + history = true, + value = true, + obscure = true, + realtime = true, + debounce = true, + pos = true, + title = true, + } + local count = 0 + for key in pairs(prompt) do + assert(expected_keys[key], label .. " unexpected field " .. tostring(key)) + count = count + 1 + end + assert_equal(count, 8, label .. " field count") + + for _, style_field in ipairs({ "border", "cursor", "normal", "selected", "theme", "keymap" }) do + assert(prompt[style_field] == nil, label .. " must leave " .. style_field .. " to native input") + end +end + +assert(type(model) == "table", "prompt specification was not captured") +assert(type(model.new) == "function", "prompt specification constructor must be a function") + +local forward = model.new(model.forward) +local backward = model.new(model.backward) +assert_prompt(forward, "Find next:", "forward prompt") +assert_prompt(backward, "Find previous:", "backward prompt") +assert(forward ~= backward, "each prompt specification must be a new table") +assert(forward.pos ~= backward.pos, "each prompt position must be a new table") + +forward.name = "changed" +forward.pos.x = 99 +local fresh = model.new(model.forward) +assert_prompt(fresh, "Find next:", "fresh forward prompt") + +print("prompt specification checks passed") -- cgit v1.2.3