Revert the bespoke __prompt()/__prompt2() function mechanism from issue #09 and return to stock Lua's _PROMPT/_PROMPT2 globals (which already support dynamic prompts via __tostring metatables). Set default _PROMPT = "lush> " in doREPL if not already defined by config.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
-- testes/lush/prompt.lua
|
||||
-- Tests for programmable prompt (issue #09).
|
||||
-- Tests for prompt system (issue #21).
|
||||
|
||||
print "testing programmable prompt"
|
||||
print "testing prompt system"
|
||||
|
||||
-- helper: get a unique temp directory
|
||||
local tmpbase = os.tmpname()
|
||||
@@ -39,95 +39,47 @@ local function run_lua(input, config_content)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 1: Default __prompt is installed =====
|
||||
-- After starting the REPL, __prompt should be a function
|
||||
-- ===== TEST 1: Default _PROMPT is "lush> " =====
|
||||
do
|
||||
local out = run_lua('"print(type(__prompt))"')
|
||||
assert(out:find("function"),
|
||||
"default __prompt should be installed as function: " .. out)
|
||||
local out = run_lua('"print(_PROMPT)"')
|
||||
assert(out:find("lush> ", 1, true),
|
||||
"default _PROMPT should be 'lush> ': " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 2: Default __prompt returns cwd =====
|
||||
-- ===== TEST 2: _PROMPT can be overridden by config =====
|
||||
do
|
||||
local cwd = os.getenv("PWD") or ""
|
||||
local home = os.getenv("HOME") or ""
|
||||
local expected
|
||||
if home ~= "" and cwd:sub(1, #home) == home then
|
||||
expected = "~" .. cwd:sub(#home + 1)
|
||||
else
|
||||
expected = cwd
|
||||
end
|
||||
local out = run_lua('"print(__prompt(0))"')
|
||||
assert(out:find(expected, 1, true),
|
||||
"default __prompt should return cwd: expected '" ..
|
||||
expected .. "' in: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 3: Custom __prompt() overrides default =====
|
||||
do
|
||||
local out = run_lua('"print(__prompt(0))"',
|
||||
'function __prompt() return "custom> " end\n')
|
||||
local out = run_lua('"print(_PROMPT)"',
|
||||
'_PROMPT = "custom> "\n')
|
||||
assert(out:find("custom> ", 1, true),
|
||||
"custom __prompt should be used: " .. out)
|
||||
"config should override _PROMPT: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 4: __prompt() receives exit code =====
|
||||
-- ===== TEST 3: _PROMPT with __tostring metatable (dynamic prompt) =====
|
||||
do
|
||||
local out = run_lua(
|
||||
'"error()" "print(__prompt())" "os.exit()"',
|
||||
'function __prompt(code)\n'
|
||||
.. ' return "exit:" .. tostring(code) .. "> "\n'
|
||||
.. 'end\n')
|
||||
-- After error(), the next prompt call should receive non-zero code.
|
||||
-- But since we're calling __prompt() manually via print, we test
|
||||
-- the mechanism differently: call it with a code directly.
|
||||
local out2 = run_lua('"print(__prompt(42))"',
|
||||
'function __prompt(code)\n'
|
||||
.. ' return "exit:" .. tostring(code) .. "> "\n'
|
||||
.. 'end\n')
|
||||
assert(out2:find("exit:42> ", 1, true),
|
||||
"exit code should be passed to __prompt: " .. out2)
|
||||
local out = run_lua('"print(_PROMPT)"',
|
||||
'_PROMPT = setmetatable({}, { __tostring = function() return "dynamic> " end })\n')
|
||||
assert(out:find("dynamic> ", 1, true),
|
||||
"_PROMPT with __tostring should work: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 5: __prompt2 for continuation =====
|
||||
-- ===== TEST 4: _PROMPT2 works for continuation =====
|
||||
do
|
||||
local out = run_lua('"print(type(__prompt2))"')
|
||||
-- __prompt2 is not installed by default
|
||||
assert(out:find("nil") or out:find("function"),
|
||||
"__prompt2 should be nil or function: " .. out)
|
||||
local out = run_lua('"print(_PROMPT2)"',
|
||||
'_PROMPT2 = "...> "\n')
|
||||
assert(out:find("...> ", 1, true),
|
||||
"_PROMPT2 should be settable: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 6: Broken __prompt() falls back gracefully =====
|
||||
-- ===== TEST 5: _PROMPT = nil falls back gracefully =====
|
||||
do
|
||||
local out = run_lua('"print(42)"',
|
||||
'function __prompt() error("boom") end\n')
|
||||
-- Set _PROMPT to nil during the REPL; get_prompt falls back to LUA_PROMPT.
|
||||
local out = run_lua('"_PROMPT = nil" "print(42)"')
|
||||
assert(out:find("42"),
|
||||
"broken __prompt should still allow REPL to work: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 7: _PROMPT still works (backward compat) =====
|
||||
do
|
||||
-- When __prompt is defined (default), it takes priority.
|
||||
-- To test _PROMPT fallback, unset __prompt.
|
||||
local out = run_lua('"print(42)"',
|
||||
'__prompt = nil\n_PROMPT = "old> "\n')
|
||||
assert(out:find("42"),
|
||||
"_PROMPT fallback should still work: " .. out)
|
||||
end
|
||||
|
||||
|
||||
-- ===== TEST 8: Config-defined __prompt is not overwritten =====
|
||||
do
|
||||
local out = run_lua('"print(__prompt(0))"',
|
||||
'function __prompt() return "from_config> " end\n')
|
||||
assert(out:find("from_config> ", 1, true),
|
||||
"config __prompt should not be overwritten: " .. out)
|
||||
"_PROMPT = nil should still allow REPL to work: " .. out)
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user