|
| 1 | +local H = require("tests.helpers") |
| 2 | +local MiniTest = require("mini.test") |
| 3 | +local autocmds = require("tiny-inline-diagnostic.autocmds") |
| 4 | + |
| 5 | +local T = MiniTest.new_set() |
| 6 | + |
| 7 | +local function noop() end |
| 8 | + |
| 9 | +local function make_buf_opts() |
| 10 | + return H.make_opts({ |
| 11 | + options = { |
| 12 | + enable_on_insert = false, |
| 13 | + experimental = { use_window_local_extmarks = false }, |
| 14 | + }, |
| 15 | + }) |
| 16 | +end |
| 17 | + |
| 18 | +local function count_buffer_autocmds(augroup, bufnr) |
| 19 | + return #vim.api.nvim_get_autocmds({ group = augroup, buffer = bufnr }) |
| 20 | +end |
| 21 | + |
| 22 | +local function count_event_autocmds(augroup, event) |
| 23 | + return #vim.api.nvim_get_autocmds({ group = augroup, event = event }) |
| 24 | +end |
| 25 | + |
| 26 | +T["detach"] = MiniTest.new_set() |
| 27 | + |
| 28 | +T["detach"]["clears buffer-scoped autocmds"] = function() |
| 29 | + H.with_buf({ "line" }, function(buf) |
| 30 | + local augroup = autocmds.create_augroup() |
| 31 | + autocmds.setup_buffer_autocmds(augroup, make_buf_opts(), buf, noop, noop, noop, noop) |
| 32 | + autocmds.setup_cursor_autocmds(augroup, make_buf_opts(), buf, noop, noop) |
| 33 | + autocmds.setup_mode_change_autocmds(augroup, buf, noop) |
| 34 | + |
| 35 | + MiniTest.expect.equality(count_buffer_autocmds(augroup, buf) > 0, true) |
| 36 | + MiniTest.expect.equality(autocmds.is_attached(buf), true) |
| 37 | + |
| 38 | + autocmds.detach(buf) |
| 39 | + |
| 40 | + MiniTest.expect.equality(count_buffer_autocmds(augroup, buf), 0) |
| 41 | + MiniTest.expect.equality(autocmds.is_attached(buf), false) |
| 42 | + end) |
| 43 | +end |
| 44 | + |
| 45 | +T["detach"]["invokes cleanup_callback with bufnr"] = function() |
| 46 | + H.with_buf({ "line" }, function(buf) |
| 47 | + autocmds.create_augroup() |
| 48 | + |
| 49 | + local received |
| 50 | + autocmds.detach(buf, function(b) |
| 51 | + received = b |
| 52 | + end) |
| 53 | + |
| 54 | + MiniTest.expect.equality(received, buf) |
| 55 | + end) |
| 56 | +end |
| 57 | + |
| 58 | +T["setup_buffer_autocmds"] = MiniTest.new_set() |
| 59 | + |
| 60 | +T["setup_buffer_autocmds"]["re-attach after detach does not duplicate autocmds"] = function() |
| 61 | + H.with_buf({ "line" }, function(buf) |
| 62 | + local augroup = autocmds.create_augroup() |
| 63 | + local opts = make_buf_opts() |
| 64 | + |
| 65 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 66 | + autocmds.setup_cursor_autocmds(augroup, opts, buf, noop, noop) |
| 67 | + autocmds.setup_mode_change_autocmds(augroup, buf, noop) |
| 68 | + local first_count = count_buffer_autocmds(augroup, buf) |
| 69 | + |
| 70 | + autocmds.detach(buf) |
| 71 | + MiniTest.expect.equality(count_buffer_autocmds(augroup, buf), 0) |
| 72 | + |
| 73 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 74 | + autocmds.setup_cursor_autocmds(augroup, opts, buf, noop, noop) |
| 75 | + autocmds.setup_mode_change_autocmds(augroup, buf, noop) |
| 76 | + local second_count = count_buffer_autocmds(augroup, buf) |
| 77 | + |
| 78 | + MiniTest.expect.equality(second_count, first_count) |
| 79 | + end) |
| 80 | +end |
| 81 | + |
| 82 | +T["setup_buffer_autocmds"]["guards against double setup without detach"] = function() |
| 83 | + H.with_buf({ "line" }, function(buf) |
| 84 | + local augroup = autocmds.create_augroup() |
| 85 | + local opts = make_buf_opts() |
| 86 | + |
| 87 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 88 | + local first_count = count_buffer_autocmds(augroup, buf) |
| 89 | + |
| 90 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 91 | + local second_count = count_buffer_autocmds(augroup, buf) |
| 92 | + |
| 93 | + MiniTest.expect.equality(second_count, first_count) |
| 94 | + end) |
| 95 | +end |
| 96 | + |
| 97 | +T["setup_buffer_autocmds"]["multiple attach/detach cycles do not leak autocmds"] = function() |
| 98 | + H.with_buf({ "line" }, function(buf) |
| 99 | + local augroup = autocmds.create_augroup() |
| 100 | + local opts = make_buf_opts() |
| 101 | + |
| 102 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 103 | + autocmds.setup_cursor_autocmds(augroup, opts, buf, noop, noop) |
| 104 | + autocmds.setup_mode_change_autocmds(augroup, buf, noop) |
| 105 | + local baseline = count_buffer_autocmds(augroup, buf) |
| 106 | + |
| 107 | + for _ = 1, 5 do |
| 108 | + autocmds.detach(buf) |
| 109 | + autocmds.setup_buffer_autocmds(augroup, opts, buf, noop, noop, noop, noop) |
| 110 | + autocmds.setup_cursor_autocmds(augroup, opts, buf, noop, noop) |
| 111 | + autocmds.setup_mode_change_autocmds(augroup, buf, noop) |
| 112 | + end |
| 113 | + |
| 114 | + MiniTest.expect.equality(count_buffer_autocmds(augroup, buf), baseline) |
| 115 | + end) |
| 116 | +end |
| 117 | + |
| 118 | +T["setup_global_autocmds"] = MiniTest.new_set() |
| 119 | + |
| 120 | +T["setup_global_autocmds"]["creates one resize autocmd regardless of attached buffers"] = function() |
| 121 | + local augroup = autocmds.create_augroup() |
| 122 | + local opts = make_buf_opts() |
| 123 | + |
| 124 | + autocmds.setup_global_autocmds(augroup, opts, noop, noop) |
| 125 | + |
| 126 | + local resize_count = count_event_autocmds(augroup, "VimResized") |
| 127 | + + count_event_autocmds(augroup, "WinResized") |
| 128 | + |
| 129 | + H.with_buf({ "a" }, function(buf_a) |
| 130 | + autocmds.setup_buffer_autocmds(augroup, opts, buf_a, noop, noop, noop, noop) |
| 131 | + H.with_buf({ "b" }, function(buf_b) |
| 132 | + autocmds.setup_buffer_autocmds(augroup, opts, buf_b, noop, noop, noop, noop) |
| 133 | + |
| 134 | + local after = count_event_autocmds(augroup, "VimResized") |
| 135 | + + count_event_autocmds(augroup, "WinResized") |
| 136 | + MiniTest.expect.equality(after, resize_count) |
| 137 | + end) |
| 138 | + end) |
| 139 | +end |
| 140 | + |
| 141 | +T["setup_global_autocmds"]["creates WinEnter only when experimental flag is set"] = function() |
| 142 | + local augroup_off = autocmds.create_augroup() |
| 143 | + autocmds.setup_global_autocmds(augroup_off, make_buf_opts(), noop, noop) |
| 144 | + MiniTest.expect.equality(count_event_autocmds(augroup_off, "WinEnter"), 0) |
| 145 | + |
| 146 | + local augroup_on = autocmds.create_augroup() |
| 147 | + local opts_on = H.make_opts({ |
| 148 | + options = { experimental = { use_window_local_extmarks = true } }, |
| 149 | + }) |
| 150 | + autocmds.setup_global_autocmds(augroup_on, opts_on, noop, noop) |
| 151 | + MiniTest.expect.equality(count_event_autocmds(augroup_on, "WinEnter") >= 1, true) |
| 152 | +end |
| 153 | + |
| 154 | +T["create_augroup"] = MiniTest.new_set() |
| 155 | + |
| 156 | +T["create_augroup"]["resets attached buffer tracking"] = function() |
| 157 | + H.with_buf({ "line" }, function(buf) |
| 158 | + local augroup = autocmds.create_augroup() |
| 159 | + autocmds.setup_buffer_autocmds(augroup, make_buf_opts(), buf, noop, noop, noop, noop) |
| 160 | + MiniTest.expect.equality(autocmds.is_attached(buf), true) |
| 161 | + |
| 162 | + autocmds.create_augroup() |
| 163 | + |
| 164 | + MiniTest.expect.equality(autocmds.is_attached(buf), false) |
| 165 | + end) |
| 166 | +end |
| 167 | + |
| 168 | +return T |
0 commit comments