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
161
162
163
164
165
166
167
168
169
|
local script = debug.getinfo(1, "S").source:sub(2)
local root = script:match("^(.*)/tests/host_adapter_contract%.lua$") or "."
package.path = table.concat({
root .. "/lua/?.lua",
root .. "/lua/?/init.lua",
package.path,
}, ";")
local capabilities = require("clever_tee.capabilities")
local composition_root = require("clever_tee.composition_root")
local domain = require("clever_tee.domain")
local host_adapter = require("clever_tee.host_adapter")
local assertions = dofile(root .. "/tests/assertions.lua")
local same = assertions.same
local truthy = assertions.truthy
local exercised = {}
local host = host_adapter.new()
capabilities.assert_implements(host)
local function call(name, ...)
exercised[name] = true
return host[name](host, ...)
end
vim.api.nvim_buf_set_lines(0, 0, -1, true, { "ababa" })
vim.api.nvim_win_set_cursor(0, { 1, 0 })
same("ababa", call("read_text"):line(1))
same(vim.api.nvim_get_current_buf(), call("read_buffer"))
same(vim.api.nvim_get_current_win(), call("read_window"))
same(domain.Position.new(1, 1), call("read_cursor"))
same("n", call("read_mode"))
truthy(not call("read_selection").active)
same(1, call("read_count").value)
local ignore_case_global = host_adapter.configuration_global("ignore_case")
vim.g[ignore_case_global] = 0
truthy(call("configuration_present", "ignore_case"))
same(false, call("read_configuration", "ignore_case"))
call("write_configuration", "ignore_case", true)
same(true, call("read_configuration", "ignore_case"))
same("utf-8", call("read_encoding"))
same(vim.fn.tolower("AbC"), call("lowercase", "AbC"))
truthy(not call("read_macro_state").executing)
truthy(domain.FoldState.is(call("read_fold_state")))
truthy(call("read_time_ms") > 0)
same("", call("read_pending_operator"))
call("apply_cursor", domain.Position.new(1, 2))
same(domain.Position.new(1, 2), call("read_cursor"))
call("apply_selection", domain.Position.new(1, 3), domain.SelectionKind.CHARACTER)
same(domain.Position.new(1, 3), call("read_cursor"))
call("set_operator_inclusive", false)
vim.api.nvim_feedkeys("z", "n", false)
local packet = call("read_input")
same(domain.InputPacketKind.TEXT, packet.kind)
same("z", packet.text)
same(false, call("open_fold", domain.Position.new(1, 3)))
call("show_prompt", "clever-tee: ")
call("redraw", "screen")
call("redraw", "full")
same(false, call("redraw", "suppressed"))
local notifications = {}
vim.notify = function(text, level, options)
notifications[#notifications + 1] = {
text = text,
level = level,
title = options.title,
}
end
call("emit_diagnostic", "info", "adapter contract")
same("adapter contract", notifications[1].text)
same("clever-tee", notifications[1].title)
call("define_highlight_group", "CleverTeeContract", {
guifg = "red",
guibg = "NONE",
gui = { bold = true },
}, { force = true })
truthy(call("read_highlight_group", "CleverTeeContract") ~= nil)
local highlight = call("create_highlight", {
group = "CleverTeeContract",
window = call("read_window"),
positions = { domain.Position.new(1, 1), domain.Position.new(1, 3) },
priority = "high",
})
truthy(call("remove_highlight", highlight))
truthy(call("supports_timers"))
local timer = call("start_timer", 100000, function() end)
truthy(timer ~= nil)
truthy(call("stop_timer", timer))
local prior_guicursor = vim.o.guicursor
local prior_terminal_cursor = vim.fn.eval("&t_ve")
local presentation_supported = call("supports_cursor_presentation")
local lease = call("suppress_cursor_presentation")
if presentation_supported then
truthy(lease ~= nil)
truthy(call("restore_cursor_presentation", lease))
same(prior_guicursor, vim.o.guicursor)
same(prior_terminal_cursor, vim.fn.eval("&t_ve"))
else
same(nil, lease)
same(false, call("restore_cursor_presentation", lease))
end
local delivered
local registration = call("register_events", "CursorMoved", function(name, payload)
delivered = { name = name, payload = payload }
end)
local transition = call("begin_action_transition")
call("deliver_event", "CursorMoved", {
buffer = call("read_buffer"),
window = call("read_window"),
})
same(nil, delivered)
call("commit_action_transition", transition)
same("CursorMoved", delivered.name)
truthy(call("remove_event_registration", registration))
local neutral_position = call("read_cursor")
call("register_action", "ContractNeutral", function()
return domain.ActionOutcome.neutral(neutral_position)
end)
local mapping = call(
"register_mapping",
{ "n" },
"<Plug>(CleverTeeContract)",
"ContractNeutral",
{ silent = true, remap = false, preserve_count = true }
)
truthy(mapping ~= nil)
local mapping_info = vim.fn.maparg("<Plug>(CleverTeeContract)", "n", false, true)
same(1, mapping_info.silent)
same(1, mapping_info.noremap)
local target = domain.TargetValue.character("a", string.byte("a"))
local payload = domain.DotPayload.new("f", target)
same(payload, call("register_dot_repeat", payload, function()
return domain.ActionOutcome.neutral(neutral_position)
end))
for _, method_name in ipairs(capabilities.required_methods()) do
truthy(exercised[method_name], "adapter capability was not exercised: " .. method_name)
end
local suppression_global = host_adapter.configuration_global(
"suppress_default_mappings"
)
same("clever_tee_not_overwrites_standard_mappings", suppression_global)
for _, value in ipairs({ false, 0 }) do
vim.g[suppression_global] = value
local suppressed = composition_root.new({
host = host_adapter.new(),
}):activate()
same(false, suppressed.setup.install_default_mappings)
same(nil, next(suppressed.mappings))
end
vim.g[suppression_global] = nil
io.stdout:write(string.format(
"Phase 15 adapter contract: %d capabilities passed\n",
#capabilities.required_methods()
))
|