@@ -146,6 +146,190 @@ T['get_gutter_width()']['respects `win_id` argument'] = function()
146146 eq (child .lua_get (' MiniMisc.get_gutter_width(...)' , { windows [2 ] }), 0 )
147147end
148148
149+ T [' log_add()' ] = new_set ()
150+
151+ local validate_log = function (ref_log )
152+ local log = child .lua_get (' MiniMisc.log_get()' )
153+ eq (# log , # ref_log )
154+
155+ -- Validate timestamp and non-timestamp data separately
156+ local log_small , prev_timestamp = {}, 0
157+ for i , l in ipairs (log ) do
158+ log_small [i ] = vim .deepcopy (l )
159+ log_small [i ].timestamp = nil
160+
161+ eq (type (l .timestamp ), ' number' )
162+ eq (prev_timestamp < l .timestamp , true )
163+ prev_timestamp = l .timestamp
164+ end
165+ eq (log_small , ref_log )
166+ end
167+
168+ T [' log_add()' ][' works' ] = function ()
169+ child .lua ([[
170+ local t = { a = 1 }
171+ MiniMisc.log_add('before', t)
172+ MiniMisc.log_add('before nodeepcopy', t, { deepcopy = false })
173+ t.a = t.a + 1
174+ MiniMisc.log_add('after', t)
175+
176+ MiniMisc.log_add('types 1', { 1, 'text', { x = true } })
177+ MiniMisc.log_add('types 2', true)
178+ MiniMisc.log_add('types 3', nil)
179+
180+ MiniMisc.log_add(1, 'number desc')
181+ MiniMisc.log_add(nil, 'no desc')
182+ ]] )
183+
184+ validate_log ({
185+ { desc = ' before' , state = { a = 1 } },
186+ { desc = ' before nodeepcopy' , state = { a = 2 } },
187+ { desc = ' after' , state = { a = 2 } },
188+ { desc = ' types 1' , state = { 1 , ' text' , { x = true } } },
189+ { desc = ' types 2' , state = true },
190+ { desc = ' types 3' },
191+ { desc = 1 , state = ' number desc' },
192+ { state = ' no desc' },
193+ })
194+
195+ -- Should allow function in log entry
196+ local fun_in_log = child .lua ([[
197+ MiniMisc.log_add('func 1', function() return 1 end)
198+ MiniMisc.log_add('func 2', { f = function() return 2 end })
199+
200+ local log = MiniMisc.log_get()
201+ return { log[#log - 1].state(), log[#log].state.f() }
202+ ]] )
203+ eq (fun_in_log , { 1 , 2 })
204+
205+ -- Should properly set timestamps
206+ child .lua (' _G.small_time = ' .. vim .inspect (small_time ))
207+ local diff = child .lua ([[
208+ MiniMisc.log_add('ts 1', 1)
209+ vim.loop.sleep(10 * _G.small_time)
210+ MiniMisc.log_add('ts 2', 2)
211+
212+ local log = MiniMisc.log_get()
213+ return log[#log].timestamp - log[#log - 1].timestamp
214+ ]] )
215+ eq ((9 * small_time ) < diff and diff < (11 * small_time ), true )
216+ end
217+
218+ T [' log_get()' ] = new_set ()
219+
220+ T [' log_get()' ][' works' ] = function ()
221+ -- Most of the testing is done in tests for other functions
222+ local log = child .lua ([[
223+ MiniMisc.log_add('desc', { a = 1 })
224+ return MiniMisc.log_get()
225+ ]] )
226+ eq (type (log ), ' table' )
227+ eq (vim .tbl_count (log ), 1 )
228+ local entry_names = vim .tbl_keys (log [1 ])
229+
230+ table.sort (entry_names )
231+ eq (entry_names , { ' desc' , ' state' , ' timestamp' })
232+
233+ eq (log [1 ].desc , ' desc' )
234+ eq (log [1 ].state , { a = 1 })
235+ eq (type (log [1 ].timestamp ), ' number' )
236+ eq (log [1 ].timestamp > 0 , true )
237+ end
238+
239+ T [' log_show()' ] = new_set ()
240+
241+ T [' log_show()' ][' works' ] = function ()
242+ -- Set up windows
243+ local buf_id_other = child .api .nvim_get_current_buf ()
244+ local win_id_other = child .api .nvim_get_current_win ()
245+ child .cmd (' vert split' )
246+ local win_id = child .api .nvim_get_current_win ()
247+
248+ -- Should start showing log in a new scratch buffer in the current window
249+ child .lua (' MiniMisc.log_add("desc", { a = 1 })' )
250+ child .lua (' MiniMisc.log_show()' )
251+ local buf_id_log = child .api .nvim_get_current_buf ()
252+ eq (child .api .nvim_buf_get_name (buf_id_log ), ' minimisc://' .. buf_id_log .. ' /log' )
253+
254+ local validate_wins = function ()
255+ eq (child .api .nvim_win_get_buf (win_id ), buf_id_log )
256+ eq (child .api .nvim_win_get_buf (win_id_other ), buf_id_other )
257+ eq (child .api .nvim_get_current_win (), win_id )
258+ eq (buf_id_log == buf_id_other , false )
259+ end
260+
261+ local validate_lines = function (ref_lines )
262+ local log_lines = child .api .nvim_buf_get_lines (buf_id_log , 0 , - 1 , false )
263+ local mock_ts = 12.33
264+ for i = 1 , # log_lines do
265+ log_lines [i ] = log_lines [i ]:gsub (' timestamp = %d+%.%d+' , function ()
266+ mock_ts = mock_ts + 0.01
267+ return ' timestamp = ' .. mock_ts
268+ end )
269+ end
270+ eq (log_lines , ref_lines )
271+ end
272+
273+ validate_wins ()
274+
275+ local ref_lines = {
276+ ' { {' ,
277+ ' desc = "desc",' ,
278+ ' state = {' ,
279+ ' a = 1' ,
280+ ' },' ,
281+ ' timestamp = 12.34' ,
282+ ' } }' ,
283+ }
284+ validate_lines (ref_lines )
285+
286+ -- Should reuse buffer and window
287+ child .api .nvim_set_current_win (win_id_other )
288+ child .lua (' MiniMisc.log_add("desc", { b = 2 })' )
289+ child .lua (' MiniMisc.log_show()' )
290+ validate_wins ()
291+
292+ ref_lines = {
293+ ' { {' ,
294+ ' desc = "desc",' ,
295+ ' state = {' ,
296+ ' a = 1' ,
297+ ' },' ,
298+ ' timestamp = 12.34' ,
299+ ' }, {' ,
300+ ' desc = "desc",' ,
301+ ' state = {' ,
302+ ' b = 2' ,
303+ ' },' ,
304+ ' timestamp = 12.35' ,
305+ ' } }' ,
306+ }
307+ validate_lines (ref_lines )
308+ end
309+
310+ T [' log_clear()' ] = new_set ()
311+
312+ T [' log_clear()' ][' works' ] = function ()
313+ child .lua (' _G.notify_log = {}; vim.notify = function(...) table.insert(_G.notify_log, { ... }) end' )
314+ child .lua (' _G.small_time = ' .. vim .inspect (small_time ))
315+ local log = child .lua ([[
316+ MiniMisc.log_add('desc 1', 1)
317+ vim.loop.sleep(10 * _G.small_time)
318+ MiniMisc.log_add('desc 2', 2)
319+
320+ MiniMisc.log_clear()
321+ MiniMisc.log_add('after clear', 3)
322+
323+ return MiniMisc.log_get()
324+ ]] )
325+
326+ validate_log ({ { desc = ' after clear' , state = 3 } })
327+ -- Should restart start value for timestamps
328+ eq (log [1 ].timestamp < small_time , true )
329+
330+ eq (child .lua_get (' _G.notify_log' ), { { ' (mini.misc) Cleared log' } })
331+ end
332+
149333local validate_put = {
150334 put = function (args , reference_output )
151335 local capture = child .cmd_capture ((' lua MiniMisc.put(%s)' ):format (args ))
0 commit comments