|
| 1 | +local utils = require('CopilotChat.utils') |
| 2 | + |
| 3 | +describe('CopilotChat.utils', function() |
| 4 | + local cases = { |
| 5 | + { glob = '', expected = '^$' }, |
| 6 | + { glob = 'abc', expected = '^abc$' }, |
| 7 | + { glob = 'ab#/.', expected = '^ab%#%/%.$' }, |
| 8 | + { glob = '\\\\\\ab\\c\\', expected = '^%\\abc\\$' }, |
| 9 | + |
| 10 | + { glob = 'abc.*', expected = '^abc%..*$', matches = { 'abc.txt', 'abc.' }, not_matches = { 'abc' } }, |
| 11 | + { glob = '??.txt', expected = '^..%.txt$' }, |
| 12 | + |
| 13 | + { glob = 'a[]', expected = '[^]' }, |
| 14 | + { glob = 'a[^]b', expected = '^ab$' }, |
| 15 | + { glob = 'a[!]b', expected = '^ab$' }, |
| 16 | + { glob = 'a[a][b]z', expected = '^a[a][b]z$' }, |
| 17 | + { glob = 'a[a-f]z', expected = '^a[a-f]z$' }, |
| 18 | + { glob = 'a[a-f0-9]z', expected = '^a[a-f0-9]z$' }, |
| 19 | + { glob = 'a[a-f0-]z', expected = '^a[a-f0%-]z$' }, |
| 20 | + { glob = 'a[!a-f]z', expected = '^a[^a-f]z$' }, |
| 21 | + { glob = 'a[^a-f]z', expected = '^a[^a-f]z$' }, |
| 22 | + { glob = 'a[\\!\\^\\-z\\]]z', expected = '^a[%!%^%-z%]]z$' }, |
| 23 | + { glob = 'a[\\a-\\f]z', expected = '^a[a-f]z$' }, |
| 24 | + |
| 25 | + { glob = 'a[', expected = '[^]' }, |
| 26 | + { glob = 'a[a-', expected = '[^]' }, |
| 27 | + { glob = 'a[a-b', expected = '[^]' }, |
| 28 | + { glob = 'a[!', expected = '[^]' }, |
| 29 | + { glob = 'a[!a', expected = '[^]' }, |
| 30 | + { glob = 'a[!a-', expected = '[^]' }, |
| 31 | + { glob = 'a[!a-b', expected = '[^]' }, |
| 32 | + { glob = 'a[!a-b\\]', expected = '[^]' }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, case in ipairs(cases) do |
| 36 | + it('glob_to_pattern: ' .. case.glob, function() |
| 37 | + local pattern = utils.glob_to_pattern(case.glob) |
| 38 | + assert.equals(case.expected, pattern) |
| 39 | + if case.matches then |
| 40 | + for _, str in ipairs(case.matches) do |
| 41 | + assert.is_true(str:match(pattern) ~= nil) |
| 42 | + end |
| 43 | + end |
| 44 | + if case.not_matches then |
| 45 | + for _, str in ipairs(case.not_matches) do |
| 46 | + assert.is_false(str:match(pattern) ~= nil) |
| 47 | + end |
| 48 | + end |
| 49 | + end) |
| 50 | + end |
| 51 | + |
| 52 | + it('empty', function() |
| 53 | + assert.is_true(utils.empty(nil)) |
| 54 | + assert.is_true(utils.empty('')) |
| 55 | + assert.is_true(utils.empty(' ')) |
| 56 | + assert.is_true(utils.empty({})) |
| 57 | + assert.is_false(utils.empty({ 1 })) |
| 58 | + assert.is_false(utils.empty('abc')) |
| 59 | + assert.is_false(utils.empty(0)) |
| 60 | + end) |
| 61 | + |
| 62 | + it('split_lines', function() |
| 63 | + assert.are.same(utils.split_lines(''), {}) |
| 64 | + assert.are.same(utils.split_lines('a\nb'), { 'a', 'b' }) |
| 65 | + assert.are.same(utils.split_lines('a\r\nb'), { 'a', 'b' }) |
| 66 | + assert.are.same(utils.split_lines('a\nb\n'), { 'a', 'b', '' }) |
| 67 | + end) |
| 68 | + |
| 69 | + it('make_string', function() |
| 70 | + assert.equals('a b 1', utils.make_string('a', 'b', 1)) |
| 71 | + assert.equals(vim.inspect({ x = 1 }), utils.make_string({ x = 1 })) |
| 72 | + assert.equals('msg', utils.make_string('error:1: msg')) |
| 73 | + end) |
| 74 | + |
| 75 | + it('uuid', function() |
| 76 | + local uuid1 = utils.uuid() |
| 77 | + local uuid2 = utils.uuid() |
| 78 | + assert.equals('string', type(uuid1)) |
| 79 | + assert.not_equals(uuid1, uuid2) |
| 80 | + assert.equals(36, #uuid1) |
| 81 | + end) |
| 82 | + |
| 83 | + it('to_table', function() |
| 84 | + assert.are.same({ 1, 2, 3 }, utils.to_table(1, 2, 3)) |
| 85 | + assert.are.same({ 1, 2, 3 }, utils.to_table({ 1, 2 }, 3)) |
| 86 | + assert.are.same({ 1 }, utils.to_table(nil, 1)) |
| 87 | + end) |
| 88 | +end) |
0 commit comments