robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.7 KiB87 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4--{:cimport, :internalize, :eq, :neq, :ffi, :lib, :cstr, :to_cstr} = require 'test.unit.testutil'
5
6local eq = t.eq
7local ffi = t.ffi
8local to_cstr = t.to_cstr
9local NULL = t.NULL
10
11local fileio = t.cimport('./src/nvim/fileio.h')
12
13describe('file_pat functions', function()
14  describe('file_pat_to_reg_pat', function()
15    local file_pat_to_reg_pat = function(pat)
16      local res = fileio.file_pat_to_reg_pat(to_cstr(pat), NULL, NULL, 0)
17      return ffi.string(res)
18    end
19
20    itp('returns ^path$ regex for literal path input', function()
21      eq('^path$', file_pat_to_reg_pat('path'))
22    end)
23
24    itp('does not prepend ^ when there is a starting glob (*)', function()
25      eq('path$', file_pat_to_reg_pat('*path'))
26    end)
27
28    itp('does not append $ when there is an ending glob (*)', function()
29      eq('^path', file_pat_to_reg_pat('path*'))
30    end)
31
32    itp('does not include ^ or $ when surrounded by globs (*)', function()
33      eq('path', file_pat_to_reg_pat('*path*'))
34    end)
35
36    itp('replaces the bash any character (?) with the regex any character (.)', function()
37      eq('^foo.bar$', file_pat_to_reg_pat('foo?bar'))
38    end)
39
40    itp(
41      'replaces a glob (*) in the middle of a path with regex multiple any character (.*)',
42      function()
43        eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar'))
44      end
45    )
46
47    itp([[unescapes \? to ?]], function()
48      eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]]))
49    end)
50
51    itp([[unescapes \% to %]], function()
52      eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]]))
53    end)
54
55    itp([[unescapes \, to ,]], function()
56      eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]]))
57    end)
58
59    itp([[unescapes '\ ' to ' ']], function()
60      eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]]))
61    end)
62
63    itp([[escapes . to \.]], function()
64      eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar'))
65    end)
66
67    itp('Converts bash brace expansion {a,b} to regex options (a|b)', function()
68      eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}'))
69    end)
70
71    itp('Collapses multiple consecutive * into a single character', function()
72      eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar'))
73      eq([[foobar$]], file_pat_to_reg_pat('********foobar'))
74      eq([[^foobar]], file_pat_to_reg_pat('foobar********'))
75    end)
76
77    itp('Does not escape ^', function()
78      eq([[^^blah$]], file_pat_to_reg_pat('^blah'))
79      eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar'))
80    end)
81
82    itp('Does not escape $', function()
83      eq([[^blah$$]], file_pat_to_reg_pat('blah$'))
84      eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar'))
85    end)
86  end)
87end)