1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
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_parser(parse_invocation_intent)
]]
local instrumented_source, replacement_count = source:gsub(
"\nreturn {\n\tentry = function",
"\n" .. capture .. "\nreturn {\n\tentry = function",
1
)
assert(replacement_count == 1, "parser capture point must occur once")
local prompt_calls = 0
local parser
local environment = {
error = error,
pairs = pairs,
type = type,
capture_parser = function(value)
parser = value
end,
ya = {
sync = function(callback)
return callback
end,
input = function()
prompt_calls = prompt_calls + 1
error("prompt boundary called during argument validation", 0)
end,
},
}
assert(load(instrumented_source, "@" .. entry_path, "t", environment))()
assert(type(parser) == "function", "invocation parser was not captured")
local function parse(args)
return parser(args)
end
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_default_intent(intent, mode)
assert_equal(intent.mode, mode, "mode")
assert_equal(intent.previous, false, "default previous")
assert_equal(intent.smart, false, "default smart")
assert_equal(intent.insensitive, false, "default insensitive")
assert_equal(intent.direction, "forward", "default direction")
assert_equal(intent.effective_case, "sensitive", "default effective case")
local fields = 0
for _ in pairs(intent) do
fields = fields + 1
end
assert_equal(fields, 6, "intent field count")
end
local function expect_argument_error(args, expected_text)
local calls_before = prompt_calls
local ok, message = pcall(parse, args)
assert(not ok, "expected argument validation to fail")
assert(type(message) == "string", "argument error must be a string")
assert(message:find("yazi-enter:", 1, true) == 1, "argument error must identify the plugin")
assert(message:find(expected_text, 1, true), "unexpected argument error: " .. message)
assert_equal(prompt_calls, calls_before, "prompt calls after argument error")
end
for _, mode in ipairs({ "directory", "file", "either" }) do
assert_default_intent(parse({ mode }), mode)
end
local boolean_forms = {
{ label = "Boolean true", value = true, expected = true },
{ label = "Boolean false", value = false, expected = false },
{ label = "yes", value = "yes", expected = true },
{ label = "no", value = "no", expected = false },
}
for _, option in ipairs({ "previous", "smart", "insensitive" }) do
for _, form in ipairs(boolean_forms) do
local args = { "either" }
args[option] = form.value
local intent = parse(args)
assert_equal(intent[option], form.expected, option .. " from " .. form.label)
if option == "previous" then
local direction = form.expected and "backward" or "forward"
assert_equal(intent.direction, direction, "direction from " .. form.label)
elseif option == "smart" then
local effective_case = form.expected and "smart" or "sensitive"
assert_equal(intent.effective_case, effective_case, "case from smart " .. form.label)
else
local effective_case = form.expected and "insensitive" or "sensitive"
assert_equal(intent.effective_case, effective_case, "case from insensitive " .. form.label)
end
end
end
local case_combinations = {
{ smart = false, insensitive = false, expected = "sensitive" },
{ smart = false, insensitive = true, expected = "insensitive" },
{ smart = true, insensitive = false, expected = "smart" },
{ smart = true, insensitive = true, expected = "smart" },
}
for _, case in ipairs(case_combinations) do
local intent = parse({ "file", smart = case.smart, insensitive = case.insensitive })
assert_equal(intent.smart, case.smart, "preserved smart flag")
assert_equal(intent.insensitive, case.insensitive, "preserved insensitive flag")
assert_equal(intent.effective_case, case.expected, "effective case precedence")
end
local raw_args = { "either", previous = "yes", smart = false, insensitive = "no" }
local first_intent = parse(raw_args)
local second_intent = parse(raw_args)
assert(first_intent ~= second_intent, "each parse must return an invocation-local intent")
assert_equal(raw_args[1], "either", "raw mode after parsing")
assert_equal(raw_args.previous, "yes", "raw previous after parsing")
assert_equal(raw_args.smart, false, "raw smart after parsing")
assert_equal(raw_args.insensitive, "no", "raw insensitive after parsing")
expect_argument_error({}, "mode must be directory, file, or either")
expect_argument_error({ "folder" }, "mode must be directory, file, or either")
expect_argument_error({ "Directory" }, "mode must be directory, file, or either")
expect_argument_error({ "FILE" }, "mode must be directory, file, or either")
expect_argument_error({ "Either" }, "mode must be directory, file, or either")
expect_argument_error({ true }, "mode must be directory, file, or either")
expect_argument_error({ "directory", "extra" }, "unexpected positional argument at index 2")
expect_argument_error({ [1] = "file", [4] = "extra" }, "unexpected positional argument at index 4")
expect_argument_error({ "either", unknown = true }, "unknown option --unknown")
expect_argument_error({ "either", Smart = true }, "unknown option --Smart")
local rejected_values = {
{ label = "string true", value = "true" },
{ label = "string false", value = "false" },
{ label = "uppercase yes", value = "YES" },
{ label = "uppercase no", value = "NO" },
{ label = "empty string", value = "" },
{ label = "zero", value = 0 },
{ label = "one", value = 1 },
{ label = "table", value = {} },
}
for _, option in ipairs({ "previous", "smart", "insensitive" }) do
for _, rejected in ipairs(rejected_values) do
local args = { "either" }
args[option] = rejected.value
expect_argument_error(args, "option --" .. option .. " must be true, false, 'yes', or 'no'")
end
end
assert_equal(prompt_calls, 0, "total prompt calls")
print("invocation intent checks passed")
|