|
| 1 | +---@diagnostic disable: undefined-field |
| 2 | + |
| 3 | +local locations = require 'neocrush.locations' |
| 4 | + |
| 5 | +describe('neocrush.locations', function() |
| 6 | + describe('show_quickfix', function() |
| 7 | + it('should set quickfix list with items', function() |
| 8 | + local items = { |
| 9 | + { filename = 'test.lua', lnum = 10, text = 'some code', note = 'relevant because X' }, |
| 10 | + { filename = 'other.lua', lnum = 20, col = 5, text = 'other code' }, |
| 11 | + } |
| 12 | + |
| 13 | + locations.show_quickfix('Test Locations', items) |
| 14 | + |
| 15 | + local qf = vim.fn.getqflist { title = true, items = true } |
| 16 | + assert.are.same('Test Locations', qf.title) |
| 17 | + assert.are.same(2, #qf.items) |
| 18 | + |
| 19 | + vim.cmd 'cclose' |
| 20 | + end) |
| 21 | + |
| 22 | + it('should use default title when nil', function() |
| 23 | + locations.show_quickfix(nil, { |
| 24 | + { filename = 'a.lua', lnum = 1 }, |
| 25 | + }) |
| 26 | + |
| 27 | + local qf = vim.fn.getqflist { title = true } |
| 28 | + assert.are.same('AI Locations', qf.title) |
| 29 | + |
| 30 | + vim.cmd 'cclose' |
| 31 | + end) |
| 32 | + |
| 33 | + it('should combine note and text in quickfix text field', function() |
| 34 | + locations.show_quickfix('Test', { |
| 35 | + { filename = 'a.lua', lnum = 1, text = 'code here', note = 'because reasons' }, |
| 36 | + }) |
| 37 | + |
| 38 | + local qf = vim.fn.getqflist { items = true } |
| 39 | + local text = qf.items[1].text |
| 40 | + assert.truthy(text:find 'because reasons') |
| 41 | + assert.truthy(text:find 'code here') |
| 42 | + |
| 43 | + vim.cmd 'cclose' |
| 44 | + end) |
| 45 | + |
| 46 | + it('should handle items with only note', function() |
| 47 | + locations.show_quickfix('Test', { |
| 48 | + { filename = 'a.lua', lnum = 1, note = 'just a note' }, |
| 49 | + }) |
| 50 | + |
| 51 | + local qf = vim.fn.getqflist { items = true } |
| 52 | + assert.are.same('just a note', qf.items[1].text) |
| 53 | + |
| 54 | + vim.cmd 'cclose' |
| 55 | + end) |
| 56 | + |
| 57 | + it('should handle items with only text', function() |
| 58 | + locations.show_quickfix('Test', { |
| 59 | + { filename = 'a.lua', lnum = 1, text = 'just text' }, |
| 60 | + }) |
| 61 | + |
| 62 | + local qf = vim.fn.getqflist { items = true } |
| 63 | + assert.are.same('just text', qf.items[1].text) |
| 64 | + |
| 65 | + vim.cmd 'cclose' |
| 66 | + end) |
| 67 | + end) |
| 68 | + |
| 69 | + describe('handler', function() |
| 70 | + it('should handle error parameter', function() |
| 71 | + -- Should not crash when called with error |
| 72 | + assert.has_no.errors(function() |
| 73 | + locations.handler('some error', nil, nil, nil) |
| 74 | + end) |
| 75 | + end) |
| 76 | + |
| 77 | + it('should handle nil params', function() |
| 78 | + assert.has_no.errors(function() |
| 79 | + locations.handler(nil, nil, nil, nil) |
| 80 | + end) |
| 81 | + end) |
| 82 | + |
| 83 | + it('should handle empty items', function() |
| 84 | + assert.has_no.errors(function() |
| 85 | + locations.handler(nil, { items = {} }, nil, nil) |
| 86 | + end) |
| 87 | + end) |
| 88 | + |
| 89 | + it('should fall back to quickfix when telescope is not available', function() |
| 90 | + -- In test env telescope is not available, so it should use quickfix |
| 91 | + locations.handler(nil, { |
| 92 | + title = 'Handler Test', |
| 93 | + items = { |
| 94 | + { filename = 'test.lua', lnum = 5, text = 'hello' }, |
| 95 | + }, |
| 96 | + }, nil, nil) |
| 97 | + |
| 98 | + local qf = vim.fn.getqflist { title = true, items = true } |
| 99 | + assert.are.same('Handler Test', qf.title) |
| 100 | + assert.are.same(1, #qf.items) |
| 101 | + |
| 102 | + vim.cmd 'cclose' |
| 103 | + end) |
| 104 | + end) |
| 105 | +end) |
0 commit comments