summaryrefslogtreecommitdiff
path: root/tests/prompt_specification_test.lua
blob: 9262e8533e3ca10b3931a15ddb6f6ba1fb3fc601 (plain)
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
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")